Select
A list that opens to choose one option. Unlike the device’s own list, each option can carry an icon and a second line that helps to choose.
import { Select } from '@sypnia/ui/react'
One choice
Each region has a second line with its code. The hint changes with the choice.
Choose the one closest to your customers. It cannot be changed later.
Code
import { useState } from 'react';
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const regions: Option[] = [
{ value: 'us-east-1', label: 'Virginia', description: 'us-east-1 · North America' },
{ value: 'sa-east-1', label: 'São Paulo', description: 'sa-east-1 · South America' },
{ value: 'eu-central-1', label: 'Frankfurt', description: 'eu-central-1 · Europe' },
{ value: 'eu-west-1', label: 'Dublin', description: 'eu-west-1 · Europe' },
{ value: 'ap-southeast-1', label: 'Singapore', description: 'ap-southeast-1 · Asia', disabled: true },
];
export default function Basic() {
const [region, setRegion] = useState<string | null>(null);
return (
<FormGrid>
<Field label="Region" hint={region ? `The database will live in ${region}.` : 'Choose the one closest to your customers. It cannot be changed later.'}>
<Select options={regions} value={region} onChange={setRegion} placeholder="Choose a region" />
</Field>
</FormGrid>
);
} Options with an icon
A role for a new member of the project. The icon of the chosen option stays in the control.
It can be changed later from the team page.
Code
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const roles: Option[] = [
{ value: 'owner', label: 'Owner', description: 'Everything, including billing and deleting the project.', icon: 'key' },
{ value: 'editor', label: 'Editor', description: 'Changes data and tables, not the team.', icon: 'edit' },
{ value: 'viewer', label: 'Viewer', description: 'Reads data, and nothing else.', icon: 'watch' },
];
export default function WithIcons() {
return (
<FormGrid>
<Field label="Role" hint="It can be changed later from the team page.">
<Select options={roles} defaultValue="editor" />
</Field>
</FormGrid>
);
} Sizes and states
Small for toolbars, with an error that says how to fix it, and disabled.
Choose who owns this key: they get the notice when it expires.
The plan decides this value.
Code
import { Field, FormGrid, Select, type Option } from '@sypnia/ui/react';
const periods: Option[] = [
{ value: '1', label: 'Last 24 hours' },
{ value: '7', label: 'Last 7 days' },
{ value: '30', label: 'Last 30 days' },
];
const members: Option[] = [
{ value: 'laura', label: 'Laura Méndez', description: 'laura@northwind.dev' },
{ value: 'diego', label: 'Diego Ruiz', description: 'diego@northwind.dev' },
];
export default function States() {
return (
<FormGrid>
<Field label="Small, for toolbars">
<Select size="sm" options={periods} defaultValue="7" />
</Field>
<Field label="Owner of the key" error="Choose who owns this key: they get the notice when it expires.">
<Select options={members} placeholder="Choose a member" />
</Field>
<Field label="Backups" hint="The plan decides this value.">
<Select disabled options={[{ value: '7', label: 'Kept for 7 days' }]} defaultValue="7" />
</Field>
</FormGrid>
);
} When to use it
Use it when
- One choice from five to fifteen options that need an icon or a line of explanation: a region, a role, a plan.
- The choice is saved with the rest of a form.
smin toolbars and filters.
Do not use it when
- There are two to four options and room to show them: RadioGroup or RadioCards keep them all in sight.
- The list is long and the person knows what they are looking for: Combobox lets them type.
- Options are plain words and the form is used mostly on phones: NativeSelect opens the picker people already know.
- Several can be chosen: MultiSelect or CheckboxGroup.
For everyone
- Enter, Space or the down arrow open the list; arrows move; typing jumps to the option that starts with those letters; Enter chooses; Escape closes and returns the focus.
- It is announced as a button that opens a list (
listboxwithoptions), with the chosen value as part of its name. - Inside a Field it takes the label, the hint and the error by itself; alone, give it an
aria-label. - The list floats in the top layer, is as wide as the control and never wider than the window.
Props
| Select | Type | Default |
|---|---|---|
optionsrequired | Option[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
defaultValue | string | |
disabled | boolean | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
nameThe name of the value when it travels in a form. | string | |
onChange | ((value: string) => void) | |
placeholderShown while nothing is chosen. | string | |
size | "sm" | "md" | "lg" | |
valueThe chosen value. Leave it out (and use `defaultValue`) to let the component keep it. | string | null |