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
| Prop | Type | Default | What it does |
|---|---|---|---|
search | (value: string) => Promise<RoleOption[]> | required | Looks up roles |
multiple | boolean | false | number[] instead of number, with chips |
label | ReactNode | - | Field label |
description | ReactNode | - | Help text under the control |
placeholder | string | Select an option | Shown while nothing is chosen |
searchPlaceholder | string | Search... | Placeholder inside the search box |
selected | RoleOption[] | [] | Roles the field already knows |
excludeIds | number[] | [] | Roles the picker must not offer |
disabled | boolean | false | Blocks opening the picker and removing chips |
RoleOption is { id, color, name }, with name as
{ languageCode, name }[].