Components
Select
Picks one answer from a short, fixed list, such as a notice period.
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const NOTICE_PERIODS = [
{ value: 'immediately', label: 'Immediately' },
{ value: '2-weeks', label: '2 weeks' },
{ value: '1-month', label: '1 month' },
{ value: '3-months', label: '3 months' },
{ value: '6-months', label: '6 months or more' },
]
export function SelectDefault() {
return (
<div className="w-full max-w-sm">
<Select items={NOTICE_PERIODS}>
<SelectTrigger aria-label="Notice period">
<SelectValue placeholder="Select an option" />
</SelectTrigger>
<SelectContent>
{NOTICE_PERIODS.map((period) => (
<SelectItem key={period.value} value={period.value}>
{period.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
How long you have to work after you resign.
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const NOTICE_PERIODS = [
{ value: 'immediately', label: 'Immediately' },
{ value: '2-weeks', label: '2 weeks' },
{ value: '1-month', label: '1 month' },
{ value: '3-months', label: '3 months' },
{ value: '6-months', label: '6 months or more' },
]
export function SelectInAField() {
return (
<Field className="max-w-sm">
<FieldLabel>Notice period</FieldLabel>
<Select items={NOTICE_PERIODS} defaultValue="1-month">
<FieldControl>
<SelectTrigger>
<SelectValue placeholder="Select an option" />
</SelectTrigger>
</FieldControl>
<SelectContent>
{NOTICE_PERIODS.map((period) => (
<SelectItem key={period.value} value={period.value}>
{period.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FieldMessage description="How long you have to work after you resign." />
</Field>
)
}
'use client'
import { useState } from 'react'
import { Button } from '@dex/shared/components/button'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const LEVELS = { senior: 'Senior', staff: 'Staff', principal: 'Principal' }
export function SelectControlled() {
const [level, setLevel] = useState<string | null>(null)
return (
<>
<div className="w-full max-w-sm">
<Select items={LEVELS} value={level} onValueChange={setLevel}>
<SelectTrigger aria-label="Level">
<SelectValue placeholder="Choose a level" />
</SelectTrigger>
<SelectContent>
{Object.entries(LEVELS).map(([value, label]) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<Button variant="ghost" onClick={() => setLevel(null)}>
Clear
</Button>
</>
)
}
Choose a notice period.
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const NOTICE_PERIODS = [
{ value: 'immediately', label: 'Immediately' },
{ value: '2-weeks', label: '2 weeks' },
{ value: '1-month', label: '1 month' },
{ value: '3-months', label: '3 months' },
{ value: '6-months', label: '6 months or more' },
]
export function SelectInvalid() {
return (
<Field invalid className="max-w-sm">
<FieldLabel>Notice period</FieldLabel>
<Select items={NOTICE_PERIODS}>
<FieldControl>
<SelectTrigger>
<SelectValue placeholder="Select an option" />
</SelectTrigger>
</FieldControl>
<SelectContent>
{NOTICE_PERIODS.map((period) => (
<SelectItem key={period.value} value={period.value}>
{period.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FieldMessage error="Choose a notice period." />
</Field>
)
}
import { Field, FieldControl, FieldLabel } from '@dex/shared/components/field'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const NOTICE_PERIODS = { '1-month': '1 month', '3-months': '3 months' }
export function SelectDisabled() {
return (
<Field disabled className="max-w-sm">
<FieldLabel>Notice period</FieldLabel>
<Select items={NOTICE_PERIODS} defaultValue="1-month" disabled>
<FieldControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FieldControl>
<SelectContent>
<SelectItem value="1-month">1 month</SelectItem>
<SelectItem value="3-months">3 months</SelectItem>
</SelectContent>
</Select>
</Field>
)
}
Set by the role.
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
export function SelectReadOnly() {
return (
<Field className="max-w-sm">
<FieldLabel>Employment type</FieldLabel>
<Select items={{ permanent: 'Full-time, permanent' }} value="permanent" readOnly>
<FieldControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FieldControl>
<SelectContent>
<SelectItem value="permanent">Full-time, permanent</SelectItem>
</SelectContent>
</Select>
<FieldMessage description="Set by the role." />
</Field>
)
}
- Use a select for one answer from a short, fixed list, such as a notice period or a level. For more options than a short list can hold, use a combobox, which can be searched.
- Pass
items, either a map of value to label or a list of{ value, label }. The trigger then shows the chosen option's label. Without it, the trigger shows the raw value. - A controlled select with nothing chosen has the value
null, not an empty string. - In a field,
FieldControlgoes round theSelectTrigger, not theSelect. - The list opens above dialogs and sheets on its own: its positioner sits at
z-[90]. To stack it differently, passpositionerClassNametoSelectContent; az-class inclassNamedoes nothing, because the positioner is what stacks. - A read-only select keeps its value and does not open. Give it helper text saying why.