# Combobox **📖 Live documentation:** https://cds.coinbase.com/components/inputs/Combobox/ A flexible combobox component for both single and multi-selection, built for web applications with comprehensive accessibility support. ## Import ```tsx import { Combobox } from '@coinbase/cds-web/alpha/combobox' ``` ## Examples ### Basics To start, you can provide a label, an array of options, control state. ```tsx live function SingleSelect() { const singleSelectOptions = [ { value: null, label: 'Remove selection' }, { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, ]; const [value, setValue] = useState('apple'); return ( ); } ``` #### Multiple Selections You can also allow users to select multiple options with `type="multi"`. ```tsx live function MultiSelect() { const fruitOptions: SelectOption[] = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, { value: 'elderberry', label: 'Elderberry' }, { value: 'fig', label: 'Fig' }, { value: 'grape', label: 'Grape' }, { value: 'honeydew', label: 'Honeydew' }, { value: 'kiwi', label: 'Kiwi' }, { value: 'lemon', label: 'Lemon' }, { value: 'mango', label: 'Mango' }, { value: 'orange', label: 'Orange' }, { value: 'papaya', label: 'Papaya' }, { value: 'raspberry', label: 'Raspberry' }, { value: 'strawberry', label: 'Strawberry' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` ### Search We use [fuse.js](https://www.fusejs.io/) for fuzzy search by default. You can override with `filterFunction`. ```tsx live function CustomFilter() { const cryptoOptions: SelectOption[] = [ { value: 'btc', label: 'Bitcoin', description: 'BTC • Digital Gold' }, { value: 'eth', label: 'Ethereum', description: 'ETH • Smart Contracts' }, { value: 'usdc', label: 'USD Coin', description: 'USDC • Stablecoin' }, { value: 'sol', label: 'Solana', description: 'SOL • High Performance' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); const filterFunction = useCallback((options: SelectOption[], searchText: string) => { const search = searchText.toLowerCase().trim(); if (!search) return options; return options.filter((option) => { const label = typeof option.label === 'string' ? option.label.toLowerCase() : ''; const description = typeof option.description === 'string' ? option.description.toLowerCase() : ''; return label.startsWith(search) || description.startsWith(search); }); }, []); return ( ); } ``` ### Grouped Display options under headers using `label` and `options`. Sort options by the same dimension you group by. ```tsx live function GroupedOptions() { const groupedOptions = [ { label: 'Fruits', options: [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, ], }, { label: 'Vegetables', options: [ { value: 'carrot', label: 'Carrot' }, { value: 'broccoli', label: 'Broccoli' }, { value: 'spinach', label: 'Spinach' }, ], }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` ### Accessibility Use accessibility labels to provide clear control and dropdown context. For multi-select, add remove and hidden-selection labels so screen readers can describe chip actions and +X summaries. ```tsx live function AccessibilityProps() { const priorityOptions: SelectOption[] = [ { value: 'high', label: 'High Priority' }, { value: 'medium', label: 'Medium Priority' }, { value: 'low', label: 'Low Priority' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` ### Styling #### Selection Display Limit Cap visible chips with `maxSelectedOptionsToShow`; the rest show as +X more. Pair with `hiddenSelectedOptionsLabel` for screen readers. ```tsx live function LimitDisplayedSelections() { const countryOptions: SelectOption[] = [ { value: 'us', label: 'United States', description: 'North America' }, { value: 'ca', label: 'Canada', description: 'North America' }, { value: 'mx', label: 'Mexico', description: 'North America' }, { value: 'uk', label: 'United Kingdom', description: 'Europe' }, { value: 'fr', label: 'France', description: 'Europe' }, { value: 'de', label: 'Germany', description: 'Europe' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` #### Alignment Align selected values with the `align` prop. ```tsx live function AlignmentExample() { const fruitOptions: SelectOption[] = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` #### Borderless Remove the border with `bordered={false}`. ```tsx live function BorderlessExample() { const fruitOptions = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, ]; const [value, setValue] = useState('apple'); return ( ); } ``` #### Compact Use smaller sizing with `compact`. ```tsx live function CompactExample() { const fruitOptions = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` #### Helper Text Add guidance with `helperText`. ```tsx live function HelperTextExample() { const { value, onChange } = useMultiSelect({ initialValue: [] }); const fruitOptions: SelectOption[] = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, ]; return ( ); } ``` ### Composed Examples #### Country Selection You can include flag emoji in labels to create a country selector. ```tsx live function CountrySelectionExample() { const getFlagEmoji = (cc) => cc .toUpperCase() .split('') .map((c) => String.fromCodePoint(0x1f1e6 - 65 + c.charCodeAt(0))) .join(''); const countryOptions = [ { label: 'North America', options: [ { value: 'us', label: `${getFlagEmoji('us')} United States` }, { value: 'ca', label: `${getFlagEmoji('ca')} Canada` }, { value: 'mx', label: `${getFlagEmoji('mx')} Mexico` }, ], }, { label: 'Europe', options: [ { value: 'uk', label: `${getFlagEmoji('gb')} United Kingdom` }, { value: 'fr', label: `${getFlagEmoji('fr')} France` }, { value: 'de', label: `${getFlagEmoji('de')} Germany` }, ], }, { label: 'Asia', options: [ { value: 'jp', label: `${getFlagEmoji('jp')} Japan` }, { value: 'cn', label: `${getFlagEmoji('cn')} China` }, { value: 'in', label: `${getFlagEmoji('in')} India` }, ], }, ]; const { value, onChange } = useMultiSelect({ initialValue: [] }); return ( ); } ``` #### Free Solo You can add a dynamic option to Combobox to enable free solo where users can provide their own value. ```tsx live function FreeSoloExample() { const CREATE_OPTION_PREFIX = '__create__'; const FreeSoloCombobox = useMemo(() => { function StableFreeSoloCombobox({ freeSolo = false, options: initialOptions, value, onChange, placeholder = 'Search or type to add...', ...comboboxProps }) { const [searchText, setSearchText] = useState(''); const [options, setOptions] = useState(initialOptions); useEffect(() => { if (!freeSolo) return; const initialSet = new Set(initialOptions.map((option) => option.value)); const valueSet = new Set(Array.isArray(value) ? value : value != null ? [value] : []); setOptions((prevOptions) => { const addedStillSelected = prevOptions.filter( (option) => !initialSet.has(option.value) && valueSet.has(option.value), ); return [...initialOptions, ...addedStillSelected]; }); }, [freeSolo, initialOptions, value]); const optionsWithCreate = useMemo(() => { if (!freeSolo) return options; const trimmedSearch = searchText.trim(); if (!trimmedSearch) return options; const alreadyExists = options.some( (option) => typeof option.label === 'string' && option.label.toLowerCase() === trimmedSearch.toLowerCase(), ); if (alreadyExists) return options; return [ ...options, { value: `${CREATE_OPTION_PREFIX}${trimmedSearch}`, label: `Add "${trimmedSearch}"` }, ]; }, [freeSolo, options, searchText]); const handleChange = useCallback( (newValue) => { if (!freeSolo) { onChange(newValue); return; } const values = Array.isArray(newValue) ? newValue : newValue ? [newValue] : []; const createValue = values.find((optionValue) => String(optionValue).startsWith(CREATE_OPTION_PREFIX), ); if (!createValue) { onChange(newValue); return; } const newLabel = String(createValue).slice(CREATE_OPTION_PREFIX.length); const normalizedValue = newLabel.toLowerCase(); const newOption = { value: normalizedValue, label: newLabel }; setOptions((prevOptions) => [...prevOptions, newOption]); const updatedValues = values .filter((optionValue) => !String(optionValue).startsWith(CREATE_OPTION_PREFIX)) .concat(normalizedValue); onChange(comboboxProps.type === 'multi' ? updatedValues : normalizedValue); setSearchText(''); }, [comboboxProps.type, freeSolo, onChange], ); return ( ); } return StableFreeSoloCombobox; }, [CREATE_OPTION_PREFIX]); const fruitOptions = [ { value: 'apple', label: 'Apple' }, { value: 'banana', label: 'Banana' }, { value: 'cherry', label: 'Cherry' }, { value: 'date', label: 'Date' }, { value: 'elderberry', label: 'Elderberry' }, { value: 'fig', label: 'Fig' }, ]; const [standardSingleValue, setStandardSingleValue] = useState(null); const [freeSoloSingleValue, setFreeSoloSingleValue] = useState(null); const standardMulti = useMultiSelect({ initialValue: [] }); const freeSoloMulti = useMultiSelect({ initialValue: [] }); return ( ); } ``` ## Props | Prop | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | `onChange` | `(value: Type extends multi ? SelectOptionValue \| SelectOptionValue[] \| null : SelectOptionValue \| null) => void` | Yes | `-` | - | | `options` | `SelectOptionList` | Yes | `-` | Array of options to display in the select dropdown. Can be individual options or groups with label and options | | `value` | `string \| SelectOptionValue[] \| null` | Yes | `-` | - | | `ComboboxControlComponent` | `ComboboxControlComponent` | No | `-` | Custom ComboboxControlComponent to wrap SelectControlComponent. This component must be a stable reference | | `SelectAllOptionComponent` | `SelectOptionComponent` | No | `-` | Custom component to render the Select All option | | `SelectControlComponent` | `SelectControlComponent` | No | `-` | Custom component to render the select control | | `SelectDropdownComponent` | `SelectDropdownComponent` | No | `-` | Custom component to render the dropdown container | | `SelectEmptyDropdownContentsComponent` | `SelectEmptyDropdownContentComponent` | No | `-` | Custom component to render when no options are available | | `SelectOptionComponent` | `SelectOptionComponent` | No | `-` | Custom component to render individual options | | `SelectOptionGroupComponent` | `SelectOptionGroupComponent` | No | `-` | Custom component to render group headers | | `accessibilityRoles` | `{ dropdown?: AriaHasPopupType; option?: string \| undefined; } \| undefined` | No | `-` | Accessibility roles for dropdown and option elements | | `accessory` | `ReactElement>` | No | `-` | Accessory element rendered at the end of the cell (e.g., chevron). | | `align` | `center \| start \| end` | No | `'start'` | Alignment of the value node. | | `bordered` | `boolean` | No | `true` | Add a border around all sides of the box. Determines if the control should have a default border. | | `className` | `string` | No | `-` | CSS class name for the root element | | `classNames` | `{ root?: string; control?: string \| undefined; controlStartNode?: string \| undefined; controlInputNode?: string \| undefined; controlValueNode?: string \| undefined; controlLabelNode?: string \| undefined; controlHelperTextNode?: string \| undefined; controlEndNode?: string \| undefined; dropdown?: string \| undefined; option?: string \| undefined; optionCell?: string \| undefined; optionContent?: string \| undefined; optionLabel?: string \| undefined; optionDescription?: string \| undefined; selectAllDivider?: string \| undefined; emptyContentsContainer?: string \| undefined; emptyContentsText?: string \| undefined; optionGroup?: string \| undefined; } \| undefined` | No | `-` | Custom class names for individual elements of the Select component | | `clearAllLabel` | `string` | No | `-` | Label for the Clear All option in multi-select mode | | `compact` | `boolean` | No | `-` | Whether to use compact styling for the select | | `controlAccessibilityLabel` | `string` | No | `-` | Accessibility label for the control | | `defaultOpen` | `boolean` | No | `-` | Initial open state when component mounts (uncontrolled mode) | | `defaultSearchText` | `string` | No | `-` | Default search text value for uncontrolled mode | | `disableClickOutsideClose` | `boolean` | No | `-` | Whether clicking outside the dropdown should close it | | `disabled` | `boolean` | No | `false` | Toggles input interactability and opacity | | `emptyOptionsLabel` | `string` | No | `-` | Label displayed when there are no options available | | `end` | `ReactNode` | No | `-` | End-aligned content (e.g., value, status). Replaces the deprecated detail prop. | | `endNode` | `ReactNode` | No | `-` | Adds content to the end of the inner input. Refer to diagram for location of endNode in InputStack component | | `filterFunction` | `((options: SelectOptionList, searchText: string) => SelectOption[])` | No | `-` | Custom filter function for searching options | | `font` | `ResponsiveProp` | No | `-` | - | | `helperText` | `ReactNode` | No | `-` | Helper text displayed below the select | | `hiddenSelectedOptionsLabel` | `string` | No | `-` | Label to show for showcasing count of hidden selected options | | `hideSearchInput` | `boolean` | No | `-` | Hide the search input | | `hideSelectAll` | `boolean` | No | `-` | Whether to hide the Select All option in multi-select mode | | `label` | `ReactNode` | No | `-` | Label displayed above the control | | `labelVariant` | `inside \| outside` | No | `'outside'` | The variant of the label. Only used when compact is not true. | | `maxSelectedOptionsToShow` | `number` | No | `-` | Maximum number of selected options to show before truncating | | `media` | `ReactElement` | No | `-` | Media rendered at the start of the cell (icon, avatar, image, etc). | | `onSearch` | `((searchText: string) => void)` | No | `-` | Search text change handler | | `open` | `boolean` | No | `-` | Controlled open state of the dropdown | | `placeholder` | `ReactNode` | No | `-` | Placeholder text displayed when no option is selected | | `ref` | `Ref` | No | `-` | - | | `removeSelectedOptionAccessibilityLabel` | `string` | No | `-` | Accessibility label for each chip in a multi-select | | `searchText` | `string` | No | `-` | Controlled search text value | | `selectAllLabel` | `string` | No | `-` | Label for the Select All option in multi-select mode | | `setOpen` | `((open: boolean \| ((open: boolean) => boolean)) => void)` | No | `-` | Callback to update the open state | | `startNode` | `ReactNode` | No | `-` | Adds content to the start of the inner input. Refer to diagram for location of startNode in InputStack component | | `style` | `CSSProperties` | No | `-` | Inline styles for the root element | | `styles` | `{ root?: CSSProperties; control?: CSSProperties \| undefined; controlStartNode?: CSSProperties \| undefined; controlInputNode?: CSSProperties \| undefined; controlValueNode?: CSSProperties \| undefined; controlLabelNode?: CSSProperties \| undefined; controlHelperTextNode?: CSSProperties \| undefined; controlEndNode?: CSSProperties \| undefined; controlBlendStyles?: InteractableBlendStyles \| undefined; dropdown?: CSSProperties \| undefined; option?: CSSProperties \| undefined; optionCell?: CSSProperties \| undefined; optionContent?: CSSProperties \| undefined; optionLabel?: CSSProperties \| undefined; optionDescription?: CSSProperties \| undefined; optionBlendStyles?: InteractableBlendStyles \| undefined; selectAllDivider?: CSSProperties \| undefined; emptyContentsContainer?: CSSProperties \| undefined; emptyContentsText?: CSSProperties \| undefined; optionGroup?: CSSProperties \| undefined; } \| undefined` | No | `-` | Custom styles for individual elements of the Select component | | `testID` | `string` | No | `-` | Test ID for the root element | | `type` | `SelectType` | No | `-` | Whether the select allows single or multiple selections | | `variant` | `InputVariant` | No | `foregroundMuted` | Determines the sentiment of the input. Because we allow startContent and endContent to be custom ReactNode, the content placed inside these slots will not change colors according to the variant. You will have to add that yourself | ## Styles | Selector | Static class name | Description | | --- | --- | --- | | `root` | `-` | Root container element | | `control` | `-` | Control element | | `controlStartNode` | `-` | Start node element | | `controlInputNode` | `-` | Input node element | | `controlValueNode` | `-` | Value node element | | `controlLabelNode` | `-` | Label node element | | `controlHelperTextNode` | `-` | Helper text node element | | `controlEndNode` | `-` | End node element | | `controlBlendStyles` | `-` | Blend styles for control interactivity | | `dropdown` | `-` | Dropdown container element | | `option` | `-` | Option element | | `optionCell` | `-` | Option cell element | | `optionContent` | `-` | Option content wrapper | | `optionLabel` | `-` | Option label element | | `optionDescription` | `-` | Option description element | | `optionBlendStyles` | `-` | Option blend styles for interactivity | | `selectAllDivider` | `-` | Select all divider element | | `emptyContentsContainer` | `-` | Empty contents container element | | `emptyContentsText` | `-` | Empty contents text element | | `optionGroup` | `-` | Option group element |