import { Select } from '@coinbase/cds-web/alpha/select'
Avoid using options with duplicate values. Each option's value should be unique within the options array to ensure proper selection behavior.
Single Select
Basic single selection with predefined options.
function SingleSelectExample() { const [value, setValue] = useState('1'); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, { value: '4', label: 'Option 4' }, ]; return ( <Select label="Choose an option" value={value} onChange={setValue} options={options} placeholder="Select an option" /> ); }
Multi-Select
Multi-selection mode allows users to select multiple options from the list.
Disabled options and options inside disabled groups will be skipped when "Select all" is pressed. Only enabled options will be selected.
function MultiSelectExample() { const { value, onChange } = useMultiSelect({ initialValue: ['1', '3', '7', '8', '9', '10'], }); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, { value: '4', label: 'Option 4' }, { value: '5', label: 'Option 5' }, { value: '6', label: 'Option 6' }, { value: '7', label: 'Option 7' }, { value: '8', label: 'Option 8' }, { value: '9', label: 'Option 9' }, { value: '10', label: 'Option 10' }, ]; return ( <Select type="multi" label="Choose multiple options" value={value} onChange={onChange} options={options} placeholder="Select options" selectAllLabel="Select all options" clearAllLabel="Clear all selections" maxSelectedOptionsToShow={4} /> ); }
Single Select with Groups
Organize options into logical groups for better organization.
function SingleSelectWithGroupsExample() { const [value, setValue] = useState(null); const groupedOptions = [ { label: 'Group A', options: [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ], }, { label: 'Group B', options: [ { value: '4', label: 'Option 4' }, { value: '5', label: 'Option 5' }, ], }, { label: 'Group C', options: [{ value: '6', label: 'Option 6' }], }, ]; return ( <Select label="Choose an option" value={value} onChange={setValue} options={groupedOptions} placeholder="Select an option" /> ); }
Multi-Select with Groups
Use groups in multi-select mode to organize selections.
function MultiSelectWithGroupsExample() { const { value, onChange } = useMultiSelect({ initialValue: [], }); const groupedOptions = [ { label: 'Group A', options: [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ], }, { label: 'Group B', options: [ { value: '4', label: 'Option 4' }, { value: '5', label: 'Option 5' }, ], }, { label: 'Group C', options: [{ value: '6', label: 'Option 6' }], }, ]; return ( <Select type="multi" label="Choose multiple options" value={value} onChange={onChange} options={groupedOptions} placeholder="Select options" selectAllLabel="Select all options" clearAllLabel="Clear all selections" /> ); }
Alignment
The Select component supports aligning the selected value(s) using the align prop.
Left / right alignment is preferred for styling.
function AlignmentExample() { const exampleOptions = [ { value: null, label: 'Remove selection' }, { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ]; const [value, setValue] = useState('1'); const { value: multiValue, onChange: onMultiChange } = useMultiSelect({ initialValue: ['1'], }); return ( <VStack gap={2}> <Select label="Default align - start" onChange={setValue} options={exampleOptions} placeholder="Empty value" value={value} /> <Select align="end" label="End align" onChange={setValue} options={exampleOptions} placeholder="Empty value" value={value} /> <Select label="Default align - start" onChange={onMultiChange} options={exampleOptions} placeholder="Empty value" type="multi" value={multiValue} /> <Select align="end" label="End align" onChange={onMultiChange} options={exampleOptions} placeholder="Empty value" type="multi" value={multiValue} /> </VStack> ); }
Accessibility Props
The Select component supports comprehensive accessibility features including custom labels and roles.
function AccessibilityExample() { const [value, setValue] = useState('2'); const options = [ { value: '1', label: 'High Priority' }, { value: '2', label: 'Medium Priority' }, { value: '3', label: 'Low Priority' }, ]; return ( <Select label="Task Priority" value={value} onChange={setValue} options={options} accessibilityLabel="Task priority level options" controlAccessibilityLabel="Select task priority level" accessibilityRoles={{ dropdown: 'listbox', option: 'option', }} placeholder="Choose priority level" helperText="Select the appropriate priority for this task" /> ); }
Variant Props
The Select component supports different visual variants for various states and contexts.
function VariantExample() { const [positiveValue, setPositiveValue] = useState('success'); const [negativeValue, setNegativeValue] = useState(''); const positiveOptions = [ { value: 'success', label: 'Success' }, { value: 'completed', label: 'Completed' }, { value: 'approved', label: 'Approved' }, ]; const negativeOptions = [ { value: 'error', label: 'Error' }, { value: 'failed', label: 'Failed' }, { value: 'rejected', label: 'Rejected' }, ]; return ( <VStack gap={4}> <Select label="Positive Status" value={positiveValue} onChange={setPositiveValue} options={positiveOptions} variant="positive" helperText="This shows a positive state" placeholder="Select positive status" /> <Select label="Negative Status" value={negativeValue} onChange={setNegativeValue} options={negativeOptions} variant="negative" helperText="This shows an error state" placeholder="Select negative status" /> </VStack> ); }
With Disabled Option Group
Disable entire groups to prevent selection of those options.
function DisabledGroupExample() { const [value, setValue] = useState(null); const groupedOptions = [ { label: 'Available Options', options: [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ], }, { label: 'Unavailable Options (Group Disabled)', disabled: true, options: [ { value: '4', label: 'Option 4' }, { value: '5', label: 'Option 5' }, { value: '6', label: 'Option 6' }, ], }, { label: 'More Available Options', options: [ { value: '7', label: 'Option 7' }, { value: '8', label: 'Option 8' }, ], }, ]; return ( <Select label="Choose an option" value={value} onChange={setValue} options={groupedOptions} placeholder="Select an option" /> ); }
Compact Mode
The Select component can be rendered in a compact size for denser UIs.
function CompactExample() { const [value, setValue] = useState('1'); const options = [ { value: '1', label: 'Small Option 1' }, { value: '2', label: 'Small Option 2' }, { value: '3', label: 'Small Option 3' }, ]; return ( <Select compact label="Compact Select" value={value} onChange={setValue} options={options} placeholder="Select an option" helperText="This is a compact select component" /> ); }
You can also use multi-selection mode while in a compact size.
function CompactMultiSelectExample() { const { value, onChange } = useMultiSelect({ initialValue: ['1', '3', '7', '8', '9', '10'], }); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, { value: '4', label: 'Option 4' }, { value: '5', label: 'Option 5' }, { value: '6', label: 'Option 6' }, { value: '7', label: 'Option 7' }, { value: '8', label: 'Option 8' }, { value: '9', label: 'Option 9' }, { value: '10', label: 'Option 10' }, ]; return ( <Select compact type="multi" label="Choose multiple options" value={value} onChange={onChange} options={options} placeholder="Select options" selectAllLabel="Select all options" clearAllLabel="Clear all selections" maxSelectedOptionsToShow={2} /> ); }
Disabled States
Components can be disabled entirely or have individual options disabled.
function DisabledExample() { const [value1, setValue1] = useState('2'); const [value2, setValue2] = useState('2'); const optionsWithDisabled = [ { value: '1', label: 'Option 1', disabled: true }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, { value: '4', label: 'Option 4', disabled: true }, ]; return ( <VStack gap={4}> <Select label="Disabled Select" value={value1} onChange={setValue1} options={optionsWithDisabled} disabled placeholder="This select is disabled" /> <Select label="Select with Disabled Options" value={value2} onChange={setValue2} options={optionsWithDisabled} placeholder="Some options are disabled" /> </VStack> ); }
Options with Descriptions
Options can include descriptions for additional context.
function DescriptionExample() { const [value, setValue] = useState('1'); const optionsWithDescriptions = [ { value: '1', label: 'Bitcoin', description: 'The first cryptocurrency' }, { value: '2', label: 'Ethereum', description: 'Smart contract platform' }, { value: '3', label: 'USDC', description: 'USD-backed stablecoin' }, { value: '4', label: 'Solana', description: 'High-performance blockchain' }, ]; return ( <Select label="Select Cryptocurrency" value={value} onChange={setValue} options={optionsWithDescriptions} placeholder="Choose a cryptocurrency" /> ); }
Start Node
Add an icon or element at the start of the select control.
function StartNodeExample() { const [value, setValue] = useState('1'); const options = [ { value: '1', label: 'Search Result 1' }, { value: '2', label: 'Search Result 2' }, { value: '3', label: 'Search Result 3' }, ]; return ( <Select label="Search" value={value} onChange={setValue} options={options} startNode={<Icon color="fg" name="search" />} placeholder="Search for options" /> ); }
End Node
Add an icon or element at the end of the select control.
function EndNodeExample() { const [value, setValue] = useState('1'); const options = [ { value: '1', label: 'Search Result 1' }, { value: '2', label: 'Search Result 2' }, { value: '3', label: 'Search Result 3' }, ]; return ( <Select endNode={<Icon alignItems="center" color="fg" name="search" />} label="Single select - end node" onChange={setValue} options={options} placeholder="Empty value" value={value} /> ); }
Custom Icons
Add custom icons as accessories or media to options.
function CustomIconsExample() { const [value, setValue] = useState('1'); const optionsWithIcons = [ { value: '1', label: 'Favorites', accessory: <Icon color="fg" name="star" />, media: <Icon color="fg" name="heart" />, }, { value: '2', label: 'Verified', accessory: <Icon color="fg" name="checkmark" />, media: <Icon color="fg" name="shield" />, }, { value: '3', label: 'Settings', accessory: <Icon color="fg" name="caretRight" />, media: <Icon color="fg" name="gear" />, }, ]; return ( <Select label="Choose Action" value={value} onChange={setValue} options={optionsWithIcons} placeholder="Select an action" /> ); }
Empty State
Handle empty option lists with custom messages or components.
function EmptyStateExample() { const [searchTerm, setSearchTerm] = useState(''); const [value, setValue] = useState(null); const allOptions = []; const filteredOptions = searchTerm ? allOptions.filter((opt) => opt.label.toLowerCase().includes(searchTerm.toLowerCase())) : allOptions; return ( <Select label="Filtered Select" value={value} onChange={setValue} options={filteredOptions} emptyOptionsLabel="No matching options found" placeholder="Select an option" /> ); }
Controlled Open State
Control the open/closed state of the dropdown programmatically.
function ControlledOpenExample() { const [value, setValue] = useState('1'); const [open, setOpen] = useState(false); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ]; return ( <VStack gap={4}> <Button onClick={() => setOpen(!open)}>{open ? 'Close Dropdown' : 'Open Dropdown'}</Button> <Select label="Controlled Select" value={value} onChange={setValue} options={options} open={open} setOpen={setOpen} placeholder="Select an option" disableClickOutsideClose /> </VStack> ); }
Multi-Select with Max Display
Limit the number of selected items shown when using multi-select.
function MaxDisplayExample() { const { value, onChange } = useMultiSelect({ initialValue: ['1', '2', '3', '4', '5'], }); const options = Array.from({ length: 20 }, (_, i) => ({ value: (i + 1).toString(), label: `Option ${i + 1}`, })); return ( <Select type="multi" label="Select Multiple Items" value={value} onChange={onChange} options={options} maxSelectedOptionsToShow={3} placeholder="Select options" helperText="Only showing first 3 selected items" /> ); }
Custom Select All labels
Customize the select all functionality in multi-select mode.
function CustomSelectAllExample() { const { value, onChange } = useMultiSelect({ initialValue: ['1'], }); const options = [ { value: '1', label: 'Monday' }, { value: '2', label: 'Tuesday' }, { value: '3', label: 'Wednesday' }, { value: '4', label: 'Thursday' }, { value: '5', label: 'Friday' }, { value: '6', label: 'Saturday' }, { value: '7', label: 'Sunday' }, ]; return ( <Select type="multi" label="Select Days" value={value} onChange={onChange} options={options} selectAllLabel="Select all days of the week" clearAllLabel="Clear all days" placeholder="Choose days" /> ); }
Label Variants
Different label positioning options for the Select component.
function LabelVariantExample() { const [value1, setValue1] = useState(''); const [value2, setValue2] = useState(''); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ]; return ( <VStack gap={4}> <Select label="Default Label Position" value={value1} onChange={setValue1} options={options} placeholder="Select an option" /> <Select label="Inside Label" labelVariant="inside" value={value2} onChange={setValue2} options={options} placeholder="Select an option" /> </VStack> ); }
Very Long Labels
Handle extremely long option labels that may need special treatment.
function VeryLongLabelsExample() { const [value, setValue] = useState('1'); const longOptions = [ { value: '1', label: 'This is an extremely long option label that should test how the component handles very long text content that might overflow or wrap', }, { value: '2', label: 'Another super long option label with even more text to see how it wraps or truncates in the UI when space is limited', }, { value: '3', label: 'Short', }, { value: '4', label: 'A moderately long label that is somewhere between short and extremely long', }, ]; return ( <Select label="Options with Very Long Labels" value={value} onChange={setValue} options={longOptions} placeholder="Select an option" /> ); }
Custom Long Placeholder
Use extended placeholder text for more descriptive empty states.
function LongPlaceholderExample() { const [value, setValue] = useState(null); const options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ]; return ( <Select label="Select with Long Placeholder" value={value} onChange={setValue} options={options} placeholder="This is a very long placeholder text that provides detailed instructions about what the user should select" /> ); }