MultiSelect is used to select multiple items from a collection.
import { MultiSelect } from 'primereact/multiselect';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/multiselect/multiselect.min.js"></script>
MultiSelect 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'}
];
<MultiSelect value={cities} options={citySelectItems} onChange={(e) => setCities(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'}
];
<MultiSelect optionLabel="name" value={cities} options={cities} onChange={(e) => setCities(e.value)} />
<MultiSelect optionLabel="name" optionValue="code" value={cities} options={cities} onChange={(e) => setCities(e.value)} />
When optionValue is not defined, value of an option refers to the option object itself.
A comma separated list is used by default to display selected items whereas alternative chip mode is provided using the display property to visualize the items as tokens.
<MultiSelect display="chip" optionLabel="name" value={selectedCities} options={cities} onChange={(e) => setSelectedCities(e.value)} />
Label of an option is used as the display text of an item by default, for custom content support define an itemTemplate function that gets the option instance as a parameter and returns the content. For custom filter support define a filterTemplate function that gets the option instance as a parameter and returns the content for the filter element.
<MultiSelect value={cities} options={citySelectItems} onChange={(e) => setCities(e.value)} itemTemplate={itemTemplate} filter filterTemplate={filterTemplate}/>
const [filterValue, setFilterValue] = useState('');
const filterInputRef = useRef();
itemTemplate(option) {
// custom item content
}
const filterTemplate = (options) => {
let {filterOptions} = options;
return (
<div className="flex 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);
}
selectedItemTemplate can be used to customize the selected values display instead of the default comma separated list.
<MultiSelect value={cities} options={citySelectItems} onChange={(e) => setCities(e.value)} selectedItemTemplate={selectedItemTemplate} />
selectedItemTemplate(option) {
// custom selected item content
}
In addition panelHeaderTemplate and panelFooterTemplate can be used to customize the header and footer of panel.
<MultiSelect value={cities} options={citySelectItems} onChange={(e) => setCities(e.value)} panelHeaderTemplate={panelHeaderTemplate} panelFooterTemplate={panelFooterTemplate} />
panelHeaderTemplate(options) {
// options.className: Style class of the panel header.
// options.checkboxElement: Default checkbox element created by the component.
// options.checked: Whether the all checkbox is checked.
// options.onChange: Change event of toggleable checkbox.
// options.filterElement: Default filter element created by the component.
// options.closeElement: Default close element created by the component.
// options.closeElementClassName: Style class of the close container
// options.closeIconClassName: Style class of the close icon
// options.onCloseClick: Click event for the close icon.
// options.element: Default element created by the component.
// options.props: component props.
}
panelFooterTemplate(options) {
// options.props: component props.
}
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' }
]
}
];
<MultiSelect value={selectedGroupedCities} options={groupedCities} onChange={(e) => setSelectedGroupedCities(e.value)} optionLabel="label" optionGroupLabel="label" optionGroupChildren="items" />
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".
<MultiSelect value={cities} options={citySelectItems} onChange={(e) => setCities(e.value)} filter/>
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) |
Any valid attribute is passed to the root element implicitly, extended properties are as follows;
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
name | string | null | Name of the input element. |
value | array | null | Value of the component. |
options | array | null | An array of selectitems 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 | Property name or getter function to use as the value of an option, defaults to the option itself when not defined. |
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. |
style | string | null | Inline style of the element. |
className | string | null | Style class of the element. |
panelClassName | string | null | Style class of the overlay panel element. |
panelStyle | string | null | Inline style of the overlay panel element. |
scrollHeight | string | 200px | Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value. |
placeholder | string | null | Label to display when there are no selections. |
fixedPlaceholder | boolean | false | Whether to display selected items in the label section or always display the placeholder as the default label. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
showClear | boolean | false | When enabled, a clear icon is displayed to clear the value. |
filter | boolean | true | When specified, displays an input field to filter the items on keyup. |
filterBy | string | label | When filtering is enabled, filterBy decides which field or fields (comma separated) to search against. |
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. |
emptyFilterMessage | any | No records found | Template to display when filtering does not return any results. |
resetFilterOnHide | boolean | false | Clears the filter value when hiding the dropdown. |
tabIndex | number | null | Index of the element in tabbing order. |
dataKey | string | null | A property to uniquely match the value in options for better performance. |
inputId | string | null | Identifier of the focusable input. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
itemTemplate | function | null | Function that gets the option and returns the content for it. |
filterTemplate | any | null | The template of filter element. |
optionGroupTemplate | any | null | Template of an option group item. |
selectedItemTemplate | function | null | Function that gets an item in the value and returns the content for it. |
panelHeaderTemplate | any | null | Template of the panel header. |
panelFooterTemplate | any | null | Template of the panel footer. |
appendTo | DOM element | string | document.body | DOM element instance where the overlay panel should be mounted. Valid values are any DOM Element and 'self'. The self value is used to render a component where it is located. |
maxSelectedLabels | number | null | Decides how many selected item labels to show at most. |
selectionLimit | number | null | Number of maximum options that can be selected. |
selectedItemsLabel | string | {0} items selected | Label to display after exceeding max selected labels. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
display | string | comma | Used mode to display the selected item. Valid values are 'comma' and 'chip'. |
transitionOptions | object | null | The properties of CSSTransition can be customized, except for "nodeRef" and "in" properties. |
dropdownIcon | any | pi pi-chevron-down | Icon class of the dropdown icon. |
removeIcon | any | pi pi-times-circle | Icon of the remove chip element. |
virtualScrollerOptions | object | null | Whether to use the virtualScroller feature. The properties of VirtualScroller component can be used like an object in it. |
showSelectAll | boolean | true | Whether to show the select all checkbox inside the panel's header. |
selectAll | boolean | false | Whether all data is selected. |
Name | Parameters | Description |
---|---|---|
onChange | event.originalEvent: Browser event event.value: Current selected values | Callback to invoke when value changes. |
onFocus | event: Browser event. | Callback to invoke when the element receives focus. |
onBlur | event: Browser event. | Callback to invoke when the element loses focus. |
onShow | - | Callback to invoke when overlay panel becomes visible. |
onHide | - | Callback to invoke when overlay panel becomes hidden. |
onFilter | event.originalEvent: Browser event event.filter: Filter value. | Callback to invoke on filtering. |
onSelectAll | event.originalEvent: Browser event event.checked: Whether all data is selected. | Callback to invoke when all data is selected. |
Name | Parameters | Description |
---|---|---|
checkValidity | - | Checks whether the native hidden input element has any constraints and returns a boolean for the result. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-multiselect | Container element. |
p-multiselect-label-container | Container of the label to display selected items. |
p-multiselect-label-container | Label to display selected items. |
p-multiselect-trigger | Dropdown button. |
p-multiselect-filter-container | Container of filter input. |
p-multiselect-panel | Overlay panel for items. |
p-multiselect-items | List container of items. |
p-multiselect-item | An item in the list. |
p-multiselect-token | A selected item element container on display='chip' mode. |
p-chips-token-icon | Icon of a selected item element on display='chip' mode. |
p-chips-token-label | Label of a selected item element on display='chip' mode. |
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.