Skip to content

useSelect

Edit on GitHub
@volvo-cars/react-headless v0.27.2

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.

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.

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.

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>
);

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.

  • mode: 'single' or 'multiple' (default: 'single')
  • value: the selected string, or an array of selected strings in multiple mode
  • onChange: callback for single selection; receives the selected value or undefined
  • onValueChange: callback for multiple selection; receives the complete selection
  • disabled: prevents opening and selection (default: false)
  • locale: BCP 47 locale used for type-ahead matching
  • placement, offset, matchTriggerWidth, minWidth, anchored, onToggle: forwarded to useListbox
  • isOpen: whether the listbox is open
  • context: value to provide through ListboxContext
  • triggerProps: props to spread on the focusable trigger
  • listboxProps: props to spread on the listbox element

useOption({ value, label?, disabled? }) returns optionProps and the current selection mode. Spread optionProps on the element representing the option.

useOptionGroup() returns groupProps for the group container and groupNameProps for its visible label.