Introducing PrimeReact v11-alpha 🎉Discover Now

Listbox

Listbox is used to select one or more values from a list of items.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
preview

Installation#

npx shadcn@latest add @primereact/listbox

Usage#

import { Listbox, ListboxList, ListboxOption } from '@/components/ui/listbox';
<Listbox options={items} optionLabel="name">
    <ListboxList>
        {items.map((item, index) => (
            <ListboxOption key={index} index={index}>
                {item.name}
            </ListboxOption>
        ))}
    </ListboxList>
</Listbox>

Examples#

Basic#

Select one or multiple options from a scrollable list.

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

Controlled#

Use the value and onValueChange properties to control the selected value. The optionLabel and optionValue properties define the label and value fields of each option respectively.

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

Focus Behavior#

Use autoOptionFocus to control initial focused option behavior, selectOnFocus to select options while navigating with focus, and focusOnHover to move focus with mouse hover when the component is focused.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
focus-behavior-demo

Option#

Use the Listbox.Option component to define options manually. Each option requires a uKey or index prop for identification and the optionKey property on the root specifies the corresponding field from the data.

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

Selection#

Checkmark#

Use the Listbox.OptionIndicator component to display a checkmark indicator next to the selected option. The match prop controls visibility based on selection state with values always, selected, or unselected.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
checkmark-selection-demo

Multiple#

Use the multiple property to allow selecting more than one item. In this mode, the value should be an array.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
multiple-selection-demo

Meta Key Selection#

Use metaKeySelection in multiple mode to require Ctrl / Cmd for additive selection.

Hold Ctrl (or Cmd on macOS) while selecting to keep existing selections.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
meta-key-selection-demo

Checkbox#

Combine the multiple property with the Checkbox component inside Listbox.OptionIndicator to render checkboxes for each option. A Listbox.Header can be used to add a select all control.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
checkbox-selection-demo

Group#

Simple#

Use the optionGroupLabel and optionGroupChildren properties to organize options into groups.

  • Berlin
  • Frankfurt
  • Hamburg
  • Munich
  • Chicago
  • Los Angeles
  • New York
  • San Francisco
  • Kyoto
  • Osaka
  • Tokyo
  • Yokohama
simple-group-demo

Custom#

Use the Listbox.Option component with the group prop to customize group headers with custom content like icons or flags.

  • Berlin
  • Frankfurt
  • Hamburg
  • Munich
  • Chicago
  • Los Angeles
  • New York
  • San Francisco
  • Kyoto
  • Osaka
  • Tokyo
  • Yokohama
custom-group-demo

Filter#

Place a Listbox.Filter component inside Listbox.Header to add a search input. Use the as prop to render any input component and control the filtering logic with onChange. A Listbox.Empty component displays a message when no options match.

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

Invalid#

Use the invalid property to indicate a validation error.

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

Disabled#

Use the disabled property to prevent interaction.

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

Disabled Options#

Use optionDisabled to disable specific items by field name while keeping the listbox itself interactive.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris
option-disabled-demo

API#

Sub-Components#

See Primitive API for ListboxRoot, ListboxList, ListboxOption, ListboxOptionIndicator, ListboxHeader, ListboxFilter, ListboxEmpty, ListboxFooter component documentation.

Hooks#

See Headless API for useListbox and useListboxOption hook documentation.

Accessibility#

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