Introducing PrimeReact v11-alpha 🎉Discover Now

useListbox

Hook that manages listbox selection state, keyboard navigation, and option search.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
basic-demo

Usage#

import { useListbox, useListboxOption } from '@primereact/headless/listbox';
 
const listbox = useListbox({ options: cities, optionLabel: 'name' });
 
return (
    <div {...listbox.rootProps}>
        <ul {...listbox.listProps}>
            {listbox.getOptions().map((option, index) => {
                const { optionProps, state } = useListboxOption({ option, index, context: listbox });
                return <li {...optionProps}>{listbox.getOptionLabel(option)}</li>;
            })}
        </ul>
    </div>
);

useListbox manages value state, keyboard focus cycling, type-ahead search, and ARIA attributes for option lists — see Primitive for a component-based API.

Features#

  • rootProps, listProps, and filterProps return spread-ready props including ref for each container element
  • useListboxOption child hook returns optionProps with ARIA attributes, data-selected/data-focused/data-disabled states, and mouse/touch handlers
  • getOptionLabel(option), getOptionValue(option), isSelected(option), isOptionDisabled(option) for reading option metadata
  • Type-ahead search: typing characters moves focus to the matching option
  • onOptionSelect(event, option, index) and onClearClick(event) for imperative selection control

Behavior#

Default Value#

Set defaultValue for uncontrolled selection.

const listbox = useListbox({ options: cities, optionLabel: 'name', defaultValue: 'NY' });

Controlled#

Pass value and onValueChange for controlled usage.

const [selected, setSelected] = React.useState(null);
 
const listbox = useListbox({
    options: cities,
    optionLabel: 'name',
    value: selected,
    onValueChange: (e) => setSelected(e.value)
});

Multiple Selection#

Set multiple to allow selecting more than one item. The value becomes an array.

const listbox = useListbox({ options: cities, optionLabel: 'name', multiple: true });

Meta Key Selection#

Enable metaKeySelection with multiple to require Ctrl/Cmd for additive selection.

const listbox = useListbox({ options: cities, optionLabel: 'name', multiple: true, metaKeySelection: true });

Focus Behavior#

Use autoOptionFocus to automatically focus the first option, selectOnFocus to select while navigating, and focusOnHover to move focus with mouse hover.

const listbox = useListbox({ options: cities, optionLabel: 'name', autoOptionFocus: true, selectOnFocus: true });

Option Groups#

Set optionGroupLabel and optionGroupChildren to organize options into groups. Use useListboxOption with group to detect group headers.

const listbox = useListbox({
    options: groupedCities,
    optionLabel: 'label',
    optionGroupLabel: 'label',
    optionGroupChildren: 'items'
});
 
// Inside map:
const { optionProps, groupProps, state } = useListboxOption({ option, index, context: listbox });
if (state.group) return <div {...groupProps}>{listbox.getOptionGroupLabel(option)}</div>;

Disabled#

Set disabled to prevent all interaction.

const listbox = useListbox({ options: cities, optionLabel: 'name', disabled: true });

Custom Styling with Data Attributes#

[data-scope='listbox'][data-part='option'][data-selected] {
    background-color: #eff6ff;
    color: #1d4ed8;
}
[data-scope='listbox'][data-part='option'][data-focused] {
    outline: 1px solid #3b82f6;
}
[data-scope='listbox'][data-part='option'][data-disabled] {
    opacity: 0.6;
    pointer-events: none;
}

API#

useListbox#

NameTypeDefault
valueunknown—
The current value of the listbox. For single selection, this is a single value. For multiple selection, this is an array of values.
defaultValueunknown—
The default value of the listbox when used in uncontrolled mode.
optionsT[]—
An array of options to display in the listbox.
optionKeystring—
Unique key for each option.
optionLabelstring—
Label field for each option.
optionValuestring—
Value field for each option.
optionDisabledstring—
Field to check if an option is disabled.
optionGroupLabelstring—
Label field for option groups.
optionGroupChildrenstring—
Field that contains the children options in a group.
disabledboolean—
When present, it specifies that the listbox should be disabled.
invalidboolean—
When present, it specifies that the component should have invalid state style.
localestring—
The locale to use for localization. Used for accessibility labels.
multipleboolean—
When present, allows selecting multiple options.
metaKeySelectionboolean—
When enabled, requires holding the meta key (Ctrl/Cmd) to select/deselect items in multiple selection mode.
autoOptionFocusboolean—
When enabled, the focused option is automatically highlighted.
selectOnFocusboolean—
When enabled, the focused option is automatically selected.
focusOnHoverboolean—
When enabled, the focused option changes on hover.
tabIndexnumber—
Index of the element in tabbing order.
onValueChange(event: useListboxValueChangeEvent) => void—
Callback to invoke when the value changes.

useListboxOption#

NameTypeDefault
optionunknown—
The option data object.
indexnumber—
The index of the option in the list.
groupbooleanfalse
Whether the option is a group header.
disabledbooleanfalse
Whether the option is disabled.
contextuseListboxInstance—
The parent listbox instance providing state and methods.

Accessibility#

See Listbox Primitive for WAI-ARIA compliance details and keyboard support.