@dax-ui/select
v0.5.0
Published
dax-ui Select and MultiSelect: single and multiple choice dropdowns with variants and animations
Maintainers
Readme
@dax-ui/select
Select picks one value and MultiSelect picks several. Both come in seven field
variants, support option icons, descriptions, groups and disabled options, and
animate the menu, options, check marks and chips (motion is off for viewers who ask
for reduced motion). Both take sx, menuSx and optionSx for custom CSS, and
MultiSelect also takes chipSx.
Installation
pnpm add @dax-ui/selectMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeQuick start
import { MultiSelect, Select } from '@dax-ui/select';
<Select
label="Role"
placeholder="Choose a role"
options={[
{ value: 'admin', label: 'Admin' },
{ value: 'editor', label: 'Editor' },
{ value: 'viewer', label: 'Viewer' },
]}
/>
<MultiSelect
label="Teams"
options={[
{ value: 'frontend', label: 'Frontend' },
{ value: 'backend', label: 'Backend' },
]}
selectAll
/>Variants
| variant | Look |
| --- | --- |
| outlined (default) | Bordered box |
| filled | Shaded box with an underline |
| standard | Underline only, for dense forms |
| soft | Tinted box without a border; turns white with a border on focus |
| pill | Fully rounded border, for toolbars and filters |
| ghost | No border until hovered or focused, for inline editing |
| elevated | White box with a shadow, for fields on colored backgrounds |
| modern | Label above a rounded box, grey chips, clear button and chevron split by a divider, "Select all" pinned to the top of the menu |
MultiSelect shows its choices as display="chips" (default), "text" or "count", and
chips come as chipVariant="soft" (default), "neutral", "solid" or "outlined". With
variant="modern" chips default to "neutral".
<MultiSelect
variant="modern"
label="Auto Status"
placeholder="All statuses"
options={statusOptions}
value={statuses}
onChange={setStatuses}
selectAll
clearable
menuMaxHeight={240}
/>Recipes
Type-safe values
Both components are generic over the value type. Define options with a union type and
onChange gives you that type back, with no casting:
import { useState } from 'react';
import { Select, type SelectOption } from '@dax-ui/select';
type Role = 'owner' | 'admin' | 'viewer';
const ROLE_OPTIONS: SelectOption<Role>[] = [
{ value: 'owner', label: 'Owner', description: 'Full access, including billing' },
{ value: 'admin', label: 'Admin', description: 'Manage members and settings' },
{ value: 'viewer', label: 'Viewer', description: 'Read-only' },
];
export function RolePicker() {
const [role, setRole] = useState<Role | null>(null);
return <Select label="Role" options={ROLE_OPTIONS} value={role} onChange={setRole} clearable />;
}Keep option arrays outside the component (or in useMemo), so they aren't rebuilt on
every render.
Controlled vs uncontrolled
Pass value + onChange when the parent owns the state (forms, URL filters). Pass
defaultValue when the field can own it and you only read it on submit (through name
and FormData). Don't switch between the two for the same field. A single Select uses
null for "nothing chosen"; a MultiSelect uses [].
React Hook Form
Wrap the field in a Controller. Hand field.ref to inputRef (not ref, which is the
wrapper div) so setFocus and focus-on-error land on the field:
import { Controller, useForm } from 'react-hook-form';
import { MultiSelect, Select } from '@dax-ui/select';
type FormValues = { role: Role | null; teams: string[] };
export function InviteForm() {
const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { role: null, teams: [] } });
return (
<form onSubmit={handleSubmit(console.log)}>
<Controller
name="role"
control={control}
rules={{ required: 'Choose a role' }}
render={({ field: { ref, ...field }, fieldState }) => (
<Select
{...field}
inputRef={ref}
label="Role"
options={ROLE_OPTIONS}
error={!!fieldState.error}
helperText={fieldState.error?.message}
required
/>
)}
/>
<Controller
name="teams"
control={control}
rules={{ validate: (v) => v.length > 0 || 'Pick at least one team' }}
render={({ field: { ref, ...field }, fieldState }) => (
<MultiSelect
{...field}
inputRef={ref}
label="Teams"
options={TEAM_OPTIONS}
selectAll
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
/>
</form>
);
}field.onChange receives the value directly (not an event), which is what both
components pass as the first argument.
Options from an API
Map the response to options once with useMemo, and disable the field while it loads:
const { data: users = [], isLoading } = useQuery({ queryKey: ['users'], queryFn: fetchUsers });
const userOptions = useMemo<SelectOption<number>[]>(
() =>
users.map((u) => ({
value: u.id,
label: u.name,
description: u.email,
icon: <Avatar src={u.avatarUrl} />,
disabled: !u.active,
})),
[users],
);
<Select
label="Assignee"
placeholder={isLoading ? 'Loading users…' : 'Unassigned'}
options={userOptions}
disabled={isLoading}
noOptionsText="No users in this workspace"
value={assigneeId}
onChange={setAssigneeId}
clearable
/>Grouped options
Give options a group. Keep each group's options next to each other: a heading is
added whenever the group changes.
const COUNTRY_OPTIONS: SelectOption<string>[] = [
{ value: 'pk', label: 'Pakistan', group: 'Asia' },
{ value: 'ae', label: 'United Arab Emirates', group: 'Asia' },
{ value: 'de', label: 'Germany', group: 'Europe' },
];Filters bar
pill plus display="count" keeps a toolbar on one line however many values are picked:
<Stack direction="row" spacing={1.5}>
<MultiSelect
variant="pill"
label="Status"
display="count"
countLabel={(n) => `${n} statuses`}
options={STATUS_OPTIONS}
value={filters.status}
onChange={(status) => setFilters((f) => ({ ...f, status }))}
fullWidth={false}
sx={{ minWidth: 180 }}
/>
<Select
variant="pill"
label="Owner"
options={OWNER_OPTIONS}
value={filters.owner}
onChange={(owner) => setFilters((f) => ({ ...f, owner }))}
clearable
fullWidth={false}
sx={{ minWidth: 180 }}
/>
</Stack>Custom CSS
Every part takes MUI's sx: the field (sx), the dropdown (menuSx), each option
(optionSx) and each chip (chipSx). Theme values work too ('primary.main', p: 2).
<MultiSelect
label="Tags"
options={TAG_OPTIONS}
sx={{
maxWidth: 420,
'& .MuiOutlinedInput-root': { borderRadius: '14px', backgroundColor: '#f8fafc' },
'& .MuiOutlinedInput-notchedOutline': { borderColor: '#cbd5e1' },
}}
menuSx={{ borderRadius: '14px', maxHeight: 400 }}
optionSx={{
fontSize: 15,
'&.Mui-selected': { backgroundColor: '#ecfeff', color: '#0e7490' },
}}
chipSx={{ borderRadius: '999px', backgroundColor: '#0e7490', color: '#fff' }}
/>Useful selectors inside sx: .MuiOutlinedInput-root, .MuiFilledInput-root,
.MuiInput-root (the box for each MUI variant), .MuiOutlinedInput-notchedOutline
(the border), .MuiSelect-select (the value area), .MuiInputLabel-root,
.MuiFormHelperText-root, .dax-select-chevron (the arrow). Inside optionSx:
&.Mui-selected, &:hover, &.Mui-disabled, &.Mui-focusVisible.
Need something sx doesn't reach? slotProps.select, slotProps.input and
slotProps.inputLabel are passed to MUI and merged over the component's own.
Accessibility
- Always set
label: it is the field's accessible name.placeholderis not a label. - The menu is a
listboxwithoptions, fully keyboard operable: arrows to move, Enter or Space to choose, Escape to close, and type-ahead to jump to an option. - Disabled options are announced as unavailable. Chip remove buttons and the clear button have accessible names.
- Pass
errortogether with ahelperTextthat says how to fix it.
Props
Select
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| options (required) | SelectOption<V>[] | | The choices. |
| animate | boolean | true | Animates the menu, its options and chips. Always off for viewers who ask for reduced motion. |
| clearable | boolean | false | Shows a button in the field that clears the choice. |
| defaultValue | SelectValue \| null | | The initially chosen value, for an uncontrolled field. |
| disabled | boolean | false | Disables the field. |
| error | boolean | false | Shows the field in its error state. Pair it with helperText explaining what is wrong. |
| fullWidth | boolean | true | Stretches the field to the full width of its container. |
| helperText | ReactNode | | Text under the field: a hint normally, the error message when error is set. |
| label | ReactNode | | Label above the field. Also its accessible name, so always set it. |
| menuMaxHeight | number | 320 | Tallest the menu grows before it scrolls, in px. |
| menuSx | SxProps<Theme> | | Custom styles for the dropdown menu, e.g. { borderRadius: '4px' }. |
| noOptionsText | ReactNode | No options | Text shown in the menu when options is empty. |
| onChange | ((value: V \| null, event: Event \| SyntheticEvent<Element, Event>) => void) | | Called with the new value, or null when cleared. |
| onClose | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu closes. |
| onOpen | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu opens. |
| optionSx | SxProps<Theme> | | Custom styles for every option in the menu. Use &.Mui-selected for the chosen ones. |
| placeholder | string | | Shown in the field while nothing is chosen. |
| ref | Ref<HTMLDivElement> | | Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref). @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs} |
| required | boolean | false | Marks the field as required: an asterisk on the label and required on the input. |
| size | "small" \| "medium" | small | Field height: small (40px) or medium (56px). |
| slotProps | { root?: SlotProps<ElementType<FormControlProps, keyof IntrinsicElements>, TextFieldRootSlotPropsOverrides, BaseTextFieldProps>; ... 4 more ...; select?: SlotProps<...>; } \| undefined | | Props for the parts MUI's TextField is built from: input, inputLabel, select, htmlInput, formHelperText. Merged over the component's own. |
| sx | SxProps<Theme> | | Custom styles for the field, e.g. { maxWidth: 280 }. |
| value | SelectValue \| null | | The chosen value, for a controlled field. null when nothing is chosen. Use with onChange. |
| variant | "soft" \| "outlined" \| "modern" \| "filled" \| "standard" \| "pill" \| "ghost" \| "elevated" | outlined | Field style: outlined (bordered box with the label on its border), filled (shaded box with an underline), standard (underline only), soft (tinted box, no border), pill (fully rounded border), ghost (no border until hovered or focused), elevated (white box with a shadow), modern (label above a rounded box, neutral chips, clear button and chevron split by a divider, pinned "Select all"). |
MultiSelect
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| options (required) | SelectOption<V>[] | | The choices. |
| animate | boolean | true | Animates the menu, its options and chips. Always off for viewers who ask for reduced motion. |
| checkboxes | boolean | true | Shows a checkbox beside each option in the menu. |
| chipSx | SxProps<Theme> | | Custom styles for each chip, with display="chips". |
| chipVariant | "neutral" \| "soft" \| "solid" \| "outlined" | 'soft', or 'neutral' with variant="modern" | Chip style, with `display="chips"`: `neutral` (grey), `soft` (tinted with the primary color), `solid` (filled with the primary color) or `outlined`. |
| `clearable` | `boolean` | `false` | Shows a button in the field that clears the choice. |
| `countLabel` | `((count: number) => ReactNode)` | `(count) => `${count} selected | Text for display="count". |
| defaultValue | V[] | | The initially chosen values, for an uncontrolled field. |
| disabled | boolean | false | Disables the field. |
| display | "text" \| "chips" \| "count" | chips | How the chosen values show in the field: removable chips, comma-separated text, or a count like "3 selected". |
| error | boolean | false | Shows the field in its error state. Pair it with helperText explaining what is wrong. |
| fullWidth | boolean | true | Stretches the field to the full width of its container. |
| helperText | ReactNode | | Text under the field: a hint normally, the error message when error is set. |
| label | ReactNode | | Label above the field. Also its accessible name, so always set it. |
| maxChips | number | | With display="chips", how many chips show before the rest collapse into "+N". |
| menuMaxHeight | number | 320 | Tallest the menu grows before it scrolls, in px. |
| menuSx | SxProps<Theme> | | Custom styles for the dropdown menu, e.g. { borderRadius: '4px' }. |
| noOptionsText | ReactNode | No options | Text shown in the menu when options is empty. |
| onChange | ((value: V[], event: Event \| SyntheticEvent<Element, Event>) => void) | | Called with the new values, in the order of options. |
| onClose | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu closes. |
| onOpen | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu opens. |
| optionSx | SxProps<Theme> | | Custom styles for every option in the menu. Use &.Mui-selected for the chosen ones. |
| placeholder | string | | Shown in the field while nothing is chosen. |
| ref | Ref<HTMLDivElement> | | Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref). @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs} |
| required | boolean | false | Marks the field as required: an asterisk on the label and required on the input. |
| selectAll | boolean | false | Adds a "Select all" option at the top of the menu. |
| selectAllLabel | ReactNode | Select all | Label of the "Select all" option. |
| size | "small" \| "medium" | small | Field height: small (40px) or medium (56px). |
| slotProps | { root?: SlotProps<ElementType<FormControlProps, keyof IntrinsicElements>, TextFieldRootSlotPropsOverrides, BaseTextFieldProps>; ... 4 more ...; select?: SlotProps<...>; } \| undefined | | Props for the parts MUI's TextField is built from: input, inputLabel, select, htmlInput, formHelperText. Merged over the component's own. |
| sx | SxProps<Theme> | | Custom styles for the field, e.g. { maxWidth: 280 }. |
| value | V[] | | The chosen values, for a controlled field. Use with onChange. |
| variant | "soft" \| "outlined" \| "modern" \| "filled" \| "standard" \| "pill" \| "ghost" \| "elevated" | outlined | Field style: outlined (bordered box with the label on its border), filled (shaded box with an underline), standard (underline only), soft (tinted box, no border), pill (fully rounded border), ghost (no border until hovered or focused), elevated (white box with a shadow), modern (label above a rounded box, neutral chips, clear button and chevron split by a divider, pinned "Select all"). |
