Logo VitNode
Documentation

Roles

Search and pick one role or many in an Auto Form field, with each role's color and name in the reader's language.

Preview

Each role shows a color dot (when it has a color) and its name.

Usage

import { z } from 'zod'
import { AutoForm } from '@vitnode/core/components/form/auto-form'
import { AutoFormRoles } from '@vitnode/core/components/form/fields/input-roles'
import { searchAdminRolesInBrowser } from '@vitnode/core/tanstack/admin/roles'

search is required. In the AdminCP, use searchAdminRolesInBrowser: it reads the roles list (AdminCP session required) and skips the guest role.

const formSchema = z.object({ roleId: z.number() })

<AutoForm
  formSchema={formSchema}
  fields={[
    {
      id: 'roleId',
      component: props => (
        <AutoFormRoles
          {...props}
          label="Primary role"
          placeholder="Select a role"
          search={searchAdminRolesInBrowser}
        />
      ),
    },
  ]}
/>

The value is one id. Picking again replaces it.

Editing an existing record

Pass the saved roles as selected, or the chips show raw ids.

<AutoFormRoles {...props} multiple search={search} selected={user.roles} />

Excluding roles

excludeIds hides roles another field already owns.

<AutoFormRoles
  {...props}
  excludeIds={[primaryRoleId]}
  multiple
  search={search}
/>

Names per language

The field shows the active locale's name, then the first translation, then the id.

import { roleOptionName } from '@vitnode/core/components/form/fields/input-roles'

roleOptionName(role, 'pl')

Props

PropTypeDefaultWhat it does
search(value: string) => Promise<RoleOption[]>requiredLooks up roles
multiplebooleanfalsenumber[] instead of number, with chips
labelReactNode-Field label
descriptionReactNode-Help text under the control
placeholderstringSelect an optionShown while nothing is chosen
searchPlaceholderstringSearch...Placeholder inside the search box
selectedRoleOption[][]Roles the field already knows
excludeIdsnumber[][]Roles the picker must not offer
disabledbooleanfalseBlocks opening the picker and removing chips

RoleOption is { id, color, name }, with name as { languageCode, name }[].

See also

  • User - the same idea for people.
  • Combobox - when the options are strings, not records.