ListBox is used to select one or more values from a list of items.
import { ListBox } from 'primereact/listbox';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/listbox/listbox.min.js"></script>
Listbox is used as a controlled component with value and onChange properties along with the options collection. There are two alternatives of how to define the options property; One way is providing a collection of SelectItem instances having label-value pairs whereas other way is providing an array of arbitrary objects along with the optionLabel and optionValue properties to specify the label/value field pair. In addition, options can be simple primitive values such as a string array, in this case no optionLabel or optionValue is necessary.
Options as SelectItems
const citySelectItems = [
{label: 'New York', value: 'NY'},
{label: 'Rome', value: 'RM'},
{label: 'London', value: 'LDN'},
{label: 'Istanbul', value: 'IST'},
{label: 'Paris', value: 'PRS'}
];
<ListBox value={city} options={citySelectItems} onChange={(e) => setCity(e.value)} />
Options as any type
const cities = [
{name: 'New York', code: 'NY'},
{name: 'Rome', code: 'RM'},
{name: 'London', code: 'LDN'},
{name: 'Istanbul', code: 'IST'},
{name: 'Paris', code: 'PRS'}
];
<ListBox optionLabel="name" value={city} options={cities} onChange={(e) => setCity(e.value)} />
<ListBox optionLabel="name" optionValue="code" value={city} options={cities} onChange={(e) => setCity(e.value)} />
When optionValue is not defined, value of an option refers to the option object itself.
Listbox allows selection of either single or multiple items. In single case, model should be a single object reference whereas in multiple case should be an array. Multiple items can either be selected using metaKey or toggled individually depending on the value of metaKeySelection property value which is false by default. On touch enabled devices metaKeySelection is turned on automatically even when enabled.
<ListBox value={cities} options={cities} onChange={(e) => setCity(e.value)} multiple />
Label of an option is used as the display text of an item by default, for custom content support define an itemTemplate property. Its value can be JSXElement, function or string. For custom filter support, define a filterTemplate function that gets the option instance as a parameter and returns the content for the filter element.
<ListBox value={city} options={cities} onChange={(e) => setCity(e.value)} itemTemplate={itemTemplate} filter filterTemplate={filterTemplate}/>
const [filterValue, setFilterValue] = useState('');
const filterInputRef = React.useRef();
itemTemplate(option) {
// custom item content
}
const filterTemplate = (options) => {
let {filterOptions} = options;
return (
<div className="flex flex-column gap-2">
<InputText value={filterValue} ref={filterInputRef} onChange={(e) => myFilterFunction(e, filterOptions)} />
<Button label="Reset" onClick={() => myResetFunction(filterOptions)} />
</div>
)
}
const myResetFunction = (options) => {
setFilterValue('');
options.reset();
filterInputRef && filterInputRef.current.focus()
}
const myFilterFunction = (event, options) => {
let _filterValue = event.target.value;
setFilterValue(_filterValue);
options.filter(event);
}
Options can be filtered using an input field in the overlay by enabling the filter property. By default filtering is done against label of the items and filterBy property is available to choose one or more properties of the options. In addition filterMatchMode can be utilized to define the filtering algorithm, valid options are "contains" (default), "startsWith", "endsWith", "equals" and "notEquals". Also, the filterValue and onFilterValueChange properties can be used to control the filter value.
<ListBox value={city} options={cities} onChange={(e) => setCity(e.value)} filter />
Filter input can be customized with the filterInputProps option that passes any property to the filter input element.
<ListBox value={city} options={cities} onChange={(e) => setCity(e.value)} filter filterInputProps={{className:'p-3', maxLength: 10}}/>
Options groups are specified with the optionGroupLabel and optionGroupChildren properties.
const groupedCities = [
{
label: 'Germany', code: 'DE',
items: [
{ label: 'Berlin', value: 'Berlin' },
{ label: 'Frankfurt', value: 'Frankfurt' },
{ label: 'Hamburg', value: 'Hamburg' },
{ label: 'Munich', value: 'Munich' }
]
},
{
label: 'USA', code: 'US',
items: [
{ label: 'Chicago', value: 'Chicago' },
{ label: 'Los Angeles', value: 'Los Angeles' },
{ label: 'New York', value: 'New York' },
{ label: 'San Francisco', value: 'San Francisco' }
]
},
{
label: 'Japan', code: 'JP',
items: [
{ label: 'Kyoto', value: 'Kyoto' },
{ label: 'Osaka', value: 'Osaka' },
{ label: 'Tokyo', value: 'Tokyo' },
{ label: 'Yokohama', value: 'Yokohama' }
]
}
];
<ListBox value={selectedGroupedCity} options={groupedCities} onChange={(e) => setSelectedGroupedCity(e.value)} optionLabel="label" optionGroupLabel="label" optionGroupChildren="items" />
Name | Type | Default | Description |
---|---|---|---|
label | string | null | Label of the option. |
value | string | null | Value of the option. |
className | string | null | ClassName of the option. |
title | string | null | Tooltip text of the option. (Not supported) |
disabled | boolean | false | Whether the option is disabled or not. (Not supported) |
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
value | object | null | Selected value to display. |
options | array | null | An array of objects to display as the available options. |
optionLabel | string | null | Name of the label field of an option when an arbitrary objects instead of SelectItems are used as options. |
optionValue | string | null | Name of the value field of an option when arbitrary objects are used as options instead of SelectItems. |
optionDisabled | function | string | null | Property name or getter function to use as the disabled flag of an option, defaults to false when not defined. |
optionGroupLabel | string | null | Property name or getter function to use as the label of an option group. |
optionGroupChildren | string | null | Property name or getter function that refers to the children options of option group. |
itemTemplate | any | null | Custom template for the items. |
filterTemplate | any | null | Custom template for the filter element. |
optionGroupTemplate | any | null | Template of an option group item. |
style | string | null | Inline style of the element. |
listStyle | object | null | Inline style of inner list element. |
listClassName | string | null | Inline style class of inner list element. |
className | string | null | Style class of the element. |
disabled | boolean | false | When specified, disables the component. |
dataKey | string | false | A property to uniquely match the value in options for better performance. |
multiple | boolean | false | When specified, allows selecting multiple values. |
metaKeySelection | boolean | true | Defines how multiple items can be selected, when true metaKey needs to be pressed to select or unselect an item and when set to false selection of each item can be toggled individually. On touch enabled devices, metaKeySelection is turned off automatically. |
filter | boolean | false | When specified, displays a filter input at header. |
filterBy | string | label | When filtering is enabled, filterBy decides which field or fields (comma separated) to search against. |
filterValue | string | null | When specified, filter displays with this value. |
filterMatchMode | string | contains | Defines how the items are filtered, valid values are "contains" (default), "startsWith", "endsWith", "equals" and "notEquals". |
filterPlaceholder | string | null | Placeholder text to show when filter input is empty. |
filterLocale | string | undefined | Locale to use in filtering. The default locale is the host environment's current locale. |
filterInputProps | object | undefined | Props for the filter input, any prop is passed implicity to the filter input element. |
tabIndex | number | null | Index of the element in tabbing order. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
virtualScrollerOptions | object | null | Whether to use the virtualScroller feature. The properties of VirtualScroller component can be used like an object in it. |
Name | Parameters | Description |
---|---|---|
onChange | event.originalEvent: Browser event event.value: Single value or an array of values depending on the selection mode | Callback to invoke when value of listbox changes. |
onFilterValueChange | event.originalEvent: Browser event event.value: the filtered value | Callback to invoke when filter value changes. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-listbox | Main container element. |
p-listbox-header | Header element. |
p-listbox-list-wrapper | Container of list element. |
p-listbox-list | List element. |
p-listbox-item | An item in the list element. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.