Dropdown also known as Select, is used to choose an item from a collection of options.
import { Dropdown } from 'primereact/dropdown';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
SelectButton 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'}
];
<Dropdown value={city} options={citySelectItems} onChange={(e) => setCity(e.value)} placeholder="Select a City"/>
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'}
];
<Dropdown optionLabel="name" value={city} options={cities} onChange={(e) => setCity(e.value)} placeholder="Select a City"/>
<Dropdown optionLabel="name" optionValue="code" value={city} options={cities} onChange={(e) => setCity(e.value)} placeholder="Select a City"/>
When optionValue is not defined, value of an option refers to the option object itself.
Common pattern is providing an empty option as the placeholder when using native selects, however Dropdown has built-in support using the placeholder option so it is suggested to use it instead of creating an empty option.
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".
<Dropdown value={selectedCountry} options={countries} onChange={(e) => setSelectedCountry(e.value)} optionLabel="name" filter showClear filterBy="name"
placeholder="Select a Country" itemTemplate={countryOptionTemplate} />
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.
<Dropdown value={selectedCountry} options={countries} onChange={(e) => setSelectedCountry(e.value)} optionLabel="name" placeholder="Select a Country"
valueTemplate={selectedCountryTemplate} itemTemplate={countryOptionTemplate} filter filterTemplate={filterTemplate}/>
const [filterValue, setFilterValue] = useState('');
const filterInputRef = useRef();
const selectedCountryTemplate = (option, props) => {
if (option) {
return (
<div className="country-item country-item-value">
<img alt={option.name} src="images/flag/flag_placeholder.png" className={`flag flag-${option.code.toLowerCase()}`} />
<div>{option.name}</div>
</div>
);
}
return (
<span>
{props.placeholder}
</span>
);
}
const countryOptionTemplate = (option) => {
return (
<div className="country-item">
<img alt={option.name} src="images/flag/flag_placeholder.png" className={`flag flag-${option.code.toLowerCase()}`} />
<div>{option.name}</div>
</div>
);
}
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);
}
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' }
]
}
];
<Dropdown 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. |
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 | any | 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 arbitrary objects are used as options instead of SelectItems. |
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. |
valueTemplate | any | null | The template of selected item. |
itemTemplate | any | null | The template of items. |
filterTemplate | any | null | The template of filter element. |
optionGroupTemplate | any | null | Template of an option group item. |
style | string | null | Inline style of the element. |
className | string | null | Style class of the element. |
scrollHeight | string | 200px | Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value. |
filter | boolean | false | 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. |
emptyMessage | string | No results found | Text to display when there are no options available. |
emptyFilterMessage | any | No results found | Template to display when filtering does not return any results. |
resetFilterOnHide | boolean | false | Clears the filter value when hiding the dropdown. |
editable | boolean | false | When present, custom value instead of predefined options can be entered using the editable input field. |
placeholder | string | null | Default text to display when no option is selected. |
required | boolean | false | When present, it specifies that an input field must be filled out before submitting the form. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
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. |
tabIndex | number | null | Index of the element in tabbing order. |
autoFocus | boolean | false | When present, it specifies that the component should automatically get focus on load. |
filterInputAutoFocus | boolean | true | When the panel is opened, it specifies that the filter input should focus automatically. |
showFilterClear | boolean | false | When enabled, a clear icon is displayed to clear the filtered value. |
panelClassName | string | null | Style class of the overlay panel element. |
panelStyle | object | null | Inline style of the overlay panel element. |
dataKey | string | null | A property to uniquely match the value in options for better performance. |
inputId | string | null | Identifier of the focusable input. |
showClear | boolean | false | When enabled, a clear icon is displayed to clear the value. |
maxLength | number | null | Maximum number of characters to be typed on an editable input. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
ariaLabel | string | false | Used to define a string that labels the component. |
ariaLabelledBy | string | null | Contains the element IDs of labels. |
transitionOptions | object | null | The properties of CSSTransition can be customized, except for "nodeRef" and "in" properties. |
dropdownIcon | string | pi pi-chevron-down | Icon class of the dropdown icon. |
showOnFocus | boolean | false | When enabled, overlay panel will be visible with input focus. |
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: Original event event.value: Value of the checkbox | Callback to invoke on value change |
onMouseDown | event: Browser event | Callback to invoke to when a mouse button is pressed. |
onContextMenu | event: Browser event | Callback to invoke on right-click. |
onFocus | event: Browser event. | Callback to invoke when the element receives focus. |
onBlur | event: Browser event. | Callback to invoke when the element loses focus. |
onFilter | event.originalEvent: Original event event.filter: Value of the filter input | Callback to invoke when the value is filtered. |
Name | Parameters | Description |
---|---|---|
checkValidity | - | Checks whether the native hidden select element has any constraints and returns a boolean for the result. |
resetFilter | - | Reset the options filter. |
onShow | - | Callback to invoke when overlay panel becomes visible. |
onHide | - | Callback to invoke when overlay panel becomes hidden. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-dropdown | Container element. |
p-dropdown-label | Element to display label of selected option. |
p-dropdown-trigger | Icon element. |
p-dropdown-panel | Icon element. |
p-dropdown-items-wrapper | Wrapper element of items list. |
p-dropdown-items | List element of items. |
p-dropdown-item | An item in the list. |
p-dropdown-filter-container | Container of filter input. |
p-dropdown-filter | Filter element. |
p-dropdown-open | Container element when overlay is visible. |
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.