Dropdown also known as Select, is used to choose an item from a collection of options.
import { Dropdown } from 'primereact/dropdown';
Dropdown 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.
<Dropdown value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name"
placeholder="Select a City" className="w-full md:w-14rem" />
When editable is present, the input can also be entered with typing.
<Dropdown value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name"
editable placeholder="Select a City" 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.
<Dropdown value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={groupedCities} optionLabel="label"
optionGroupLabel="label" optionGroupChildren="items" optionGroupTemplate={groupedItemTemplate} className="w-full md:w-14rem" placeholder="Select a City" />
Options and the selected option display support templating with itemTemplate and valueTemplate properties respectively.
<Dropdown value={selectedCountry} onChange={(e) => setSelectedCountry(e.value)} options={countries} optionLabel="name" placeholder="Select a Country"
valueTemplate={selectedCountryTemplate} itemTemplate={countryOptionTemplate} className="w-full md:w-14rem" panelFooterTemplate={panelFooterTemplate} />
Dropdown provides built-in filtering that is enabled by adding the filter property.
<Dropdown value={selectedCountry} onChange={(e) => setSelectedCountry(e.value)} options={countries} optionLabel="name" placeholder="Select a Country"
filter valueTemplate={selectedCountryTemplate} itemTemplate={countryOptionTemplate} className="w-full md:w-14rem" />
When showClear is enabled, a clear icon is added to reset the Dropdown.
<Dropdown value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name"
showClear placeholder="Select a City" className="w-full md:w-14rem" />
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 Dropdown.
<Dropdown value={selectedItem} onChange={(e) => setSelectedItem(e.value)} options={items} virtualScrollerOptions={{ itemSize: 38 }}
placeholder="Select Item" className="w-full md:w-14rem" />
A floating label appears on top of the input field when focused.
<span className="p-float-label">
<Dropdown inputId="dd-city" value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name" className="w-full md:w-14rem" />
<label htmlFor="dd-city">Select a City</label>
</span>
Invalid state style is added using the p-invalid class to indicate a failed validation.
<Dropdown value={selectedCity} onChange={(e) => setSelectedCity(e.value)} options={cities} optionLabel="name"
placeholder="Select a City" className="p-invalid w-full md:w-14rem" />
When disabled is present, the element cannot be edited and focused.
<Dropdown disabled placeholder="Select a City" className="w-full md:w-14rem" />
Compatibility with popular React form libraries.
Formik is a popular library for handling forms in React.
<Toast ref={toast} />
<Dropdown
inputId="city"
name="city"
value={formik.values.city}
options={cities}
optionLabel="name"
placeholder="Select a City"
onChange={(e) => {
formik.setFieldValue('city', e.value);
}}
/>
{getFormErrorMessage('city')}
<Button type="submit" label="Submit" />
React Hook Form is another popular React library to handle forms.
<Toast ref={toast} />
<Controller
name="city"
control={control}
rules={{ required: 'City is required.' }}
render={({ field, fieldState }) => (
<Dropdown
id={field.name}
value={field.value}
optionLabel="name"
placeholder="Select a City"
options={cities}
focusInputRef={field.ref}
onChange={(e) => field.onChange(e.value)}
className={classNames({ 'p-invalid': fieldState.error })}
/>
)}
/>
{getFormErrorMessage('city')}
<Button type="submit" label="Submit" />
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. |
Value to describe the component can either be provided with aria-labelledby or aria-label props. The dropdown element has a combobox role in addition to aria-haspopup and aria-expanded attributes. If the editable option is enabled aria-autocomplete is also added. The relation between the combobox and the popup is created with aria-controls and aria-activedescendant attribute is used to instruct screen reader which option to read during keyboard navigation within the popup list.
The popup list has an id that refers to the aria-controls attribute of the combobox element and uses listbox as the role. Each list item has an option role, an id to match the aria-activedescendant of the input element along with aria-label, aria-selected and aria-disabled attributes.
If filtering is enabled, filterInputProps can be defined to give aria-* props to the filter input element.
<span id="dd1">Options</span>
<Dropdown aria-labelledby="dd1" />
<Dropdown aria-label="Options" />
Key | Function |
---|---|
tab | Moves focus to the dropdown element. |
space | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
down arrow | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
up arrow | Opens the popup and moves visual focus to the selected option, if there is none then last option receives the focus. |
Key | Function |
---|---|
tab | Moves focus to the next focusable element in the popup, if there is none then first focusable element receives the focus. |
shift + tab | Moves focus to the previous focusable element in the popup, if there is none then last focusable element receives the focus. |
enter | Selects the focused option and closes the popup. |
space | Selects the focused option and closes the popup. |
escape | Closes the popup, moves focus to the dropdown element. |
down arrow | Moves focus to the next option, if there is none then visual focus does not change. |
up arrow | Moves focus to the previous option, if there is none then visual focus does not change. |
right arrow | If the dropdown is editable, removes the visual focus from the current option and moves input cursor to one character left. |
left arrow | If the dropdown is editable, removes the visual focus from the current option and moves input cursor to one character right. |
home | If the dropdown is editable, moves input cursor at the end, if not then moves focus to the first option. |
end | If the dropdown is editable, moves input cursor at the beginning, if not then moves focus to the last option. |
any printable character | Moves focus to the option whose label starts with the characters being typed if dropdown is not editable. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the dropdown element. |
escape | Closes the popup and moves focus to the dropdown element. |