useSelect
useSelect combines useListbox with selection
state, keyboard navigation, and type-ahead search. It returns props for a
trigger and listbox plus the context used by useOption and useOptionGroup.
Single selection
Section titled “Single selection”The default mode is 'single'. Pass the selected value and update it from
onChange.
import { useState } from 'react';import { ListboxContext, useOption, useSelect,} from '@volvo-cars/react-headless';
function Option({ value, children }: { value: string; children: string }) { const { optionProps } = useOption({ value }); return <div {...optionProps}>{children}</div>;}
function Select() { const [value, setValue] = useState<string>(); const { context, triggerProps, listboxProps } = useSelect({ value, onChange: setValue, });
return ( <ListboxContext.Provider value={context}> <button type="button" {...triggerProps}> {value ?? 'Choose an option'} </button> <div {...listboxProps}> <Option value="ex30">EX30</Option> <Option value="ex90">EX90</Option> </div> </ListboxContext.Provider> );}The trigger follows the combobox pattern and opens with Enter, Space, or an Arrow key. When the listbox is open, Arrow keys move between enabled options and Enter or Space selects the active option. Printable characters use type-ahead matching.
Multiple selection
Section titled “Multiple selection”Set mode to 'multiple', pass an array of selected values, and handle the
complete array from onValueChange.
const { context, triggerProps, listboxProps } = useSelect({ mode: 'multiple', value: selectedValues, onValueChange: setSelectedValues,});Multiple selection keeps the listbox open after an option is selected. The
listbox receives aria-multiselectable="true" and each option reports its
selection through aria-selected.
Options and groups
Section titled “Options and groups”Use useOption inside the nearest ListboxContext.Provider. Pass label when
the option contains icons, badges, or other markup so type-ahead matching uses
plain text. Set disabled to keep an option visible but unavailable for
selection and keyboard navigation.
Use useOptionGroup to connect a visible group name to a group of options:
const { groupProps, groupNameProps } = useOptionGroup();
return ( <div {...groupProps}> <div {...groupNameProps}>Electric</div> {/* Options using useOption */} </div>);Locale-aware type-ahead
Section titled “Locale-aware type-ahead”Pass a BCP 47 locale tag when matching needs a specific language. For
example, locale: 'tr' makes type-ahead handle Turkish casing correctly. If
locale is omitted, matching uses the runtime locale.
Options
Section titled “Options”mode:'single'or'multiple'(default:'single')value: the selected string, or an array of selected strings in multiple modeonChange: callback for single selection; receives the selected value orundefinedonValueChange: callback for multiple selection; receives the complete selectiondisabled: prevents opening and selection (default:false)locale: BCP 47 locale used for type-ahead matchingplacement,offset,matchTriggerWidth,minWidth,anchored,onToggle: forwarded touseListbox
Returns
Section titled “Returns”isOpen: whether the listbox is opencontext: value to provide throughListboxContexttriggerProps: props to spread on the focusable triggerlistboxProps: props to spread on the listbox element
useOption
Section titled “useOption”useOption({ value, label?, disabled? }) returns optionProps and the
current selection mode. Spread optionProps on the element representing the
option.
useOptionGroup
Section titled “useOptionGroup”useOptionGroup() returns groupProps for the group container and
groupNameProps for its visible label.