ListBox

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


import { ListBox } from 'primereact/listbox';
         

ListBox is used as a controlled component with value and onChange properties along with an options collection. Label and value of an option are defined with the optionLabel and optionValue properties respectively. Default property name for the optionLabel is label and value for the optionValue. If optionValue is omitted and the object has no value property, the object itself becomes the value of an option. Note that, when options are simple primitive values such as a string array, no optionLabel and optionValue would be necessary.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

<ListBox value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name" className="w-full md:w-14rem" />
         

ListBox allows choosing a single item by default, enable multiple property to choose more than one. When the optional metaKeySelection is present, behavior is changed in a way that selecting a new item requires meta key to be present.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

<ListBox multiple value={selectedCities} onChange={(e) => setSelectedCities(e.value)} options={cities} optionLabel="name" className="w-full md:w-14rem" />
         

Options can be grouped when a nested data structures is provided. To define the label of a group optionGroupLabel property is needed and also optionGroupChildren is required to define the property that refers to the children of a group.

  • Germany
  • Berlin
  • Frankfurt
  • Hamburg
  • Munich
  • USA
  • Chicago
  • Los Angeles
  • New York
  • San Francisco
  • Japan
  • Kyoto
  • Osaka
  • Tokyo
  • Yokohama

<ListBox value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={groupedCities} optionLabel="label" 
    optionGroupLabel="label" optionGroupChildren="items" optionGroupTemplate={groupTemplate} className="w-full md:w-14rem" listStyle={{ maxHeight: '250px' }} />
         

ListBox provides built-in filtering that is enabled by adding the filter property.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

<ListBox filter value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name" className="w-full md:w-14rem" />
         

Custom content for an option is displayed with the itemTemplate property that takes an option as a parameter. Additional available templating sections are filterTemplate and optionGroupTemplate.

  • Australia
    Australia
  • Brazil
    Brazil
  • China
    China
  • Egypt
    Egypt
  • France
    France
  • Germany
    Germany
  • India
    India
  • Japan
    Japan
  • Spain
    Spain
  • United States
    United States

<ListBox value={selectedCountry} onChange={(e) => setSelectedCountry(e.value)} options={countries} optionLabel="name" 
    itemTemplate={countryTemplate} className="w-full md:w-14rem" listStyle={{ maxHeight: '250px' }} />
         

VirtualScroller is used to render a long list of options efficiently like 100K records in this demo. The configuration is done with virtualScrollerOptions property, refer to the VirtualScroller for more information about the available options as it is used internally by ListBox.

    
    <ListBox value={selectedItem} onChange={(e) => setSelectedItem(e.value)} options={items} 
        virtualScrollerOptions={{ itemSize: 38 }} className="w-full md:w-14rem" listStyle={{ height: '250px' }} />
             

    Invalid state style is added using the p-invalid class to indicate a failed validation.

    • New York
    • Rome
    • London
    • Istanbul
    • Paris
    
    <ListBox value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name" 
        className="w-full md:w-14rem p-invalid" />
             

    When disabled is present, the element cannot be edited and focused.

    • New York
    • Rome
    • London
    • Istanbul
    • Paris
    
    <ListBox disabled options={cities} optionLabel="name" className="w-full md:w-14rem" />
             

    Compatibility with popular React form libraries.

    Formik is a popular library for handling forms in React.

    • New York
    • Rome
    • London
    • Istanbul
    • Paris
     
    
    <Toast ref={toast} />
    <ListBox
        id="item"
        name="item"
        value={formik.values.item}
        options={cities}
        optionLabel="name"
        placeholder="Select a City"
        onChange={(e) => {
            formik.setFieldValue('item', e.value);
        }}
        style={{ width: '15rem' }}
    />
             

    • New York
    • Rome
    • London
    • Istanbul
    • Paris
     
    
    <Toast ref={toast} />
    <Controller
        name="value"
        control={control}
        rules={{ required: 'Value is required.' }}
        render={({ field }) => <ListBox value={field.value} optionLabel="name" placeholder="Select a City" name="value" options={cities} control={control} onChange={(e) => field.onChange(e.value)} style={{ width: '15rem' }} />}
    />
             

    Following is the list of structural style classes, for theming classes visit theming page.

    NameElement
    p-listboxMain container element.
    p-listbox-headerHeader element.
    p-listbox-list-wrapperContainer of list element.
    p-listbox-listList element.
    p-listbox-itemAn item in the list element.
    Accessibility guide documents the specification of this component based on WCAG guidelines, the implementation is in progress.

    Screen Reader

    Value to describe the component can be provided aria-labelledby or aria-label props. The list element has a listbox role with the aria-multiselectable attribute that sets to true when multiple selection is enabled. Each list item has an option role with aria-selected and aria-disabled as their attributes.

    If filtering is enabled, filterInputProps can be defined to give aria-* props to the input element. Alternatively filterPlaceholder is usually utilized by the screen readers as well.

    
    <span id="lb">Options</span>
    <ListBox aria-labelledby="lb" />
    
    <ListBox aria-label="City" />
         

    Keyboard Support

    KeyFunction
    tabMoves focus to the first selected option, if there is none then first option receives the focus.
    up arrowMoves focus to the previous option.
    down arrowMoves focus to the next option.
    enterToggles the selected state of the focused option.
    spaceToggles the selected state of the focused option.
    homeMoves focus to the first option.
    endMoves focus to the last option.
    shift + down arrowMoves focus to the next option and toggles the selection state.
    shift + up arrowMoves focus to the previous option and toggles the selection state.
    shift + spaceSelects the items between the most recently selected option and the focused option.
    control + shift + homeSelects the focused options and all the options up to the first one.
    control + shift + endSelects the focused options and all the options down to the last one.
    control + aSelects all options.