A complete select field with integrated label and description.
import { SelectControl } from '@wordpress/ui';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
name |
Identifies the field when a form is submitted. | |
form |
Identifies the form that owns the hidden input. Useful when the select is rendered outside the form. | |
children |
| |
id |
The id of the Select. | |
readOnly | false |
Whether the user should be unable to choose a different option from the select popup. |
defaultValue |
The uncontrolled value of the select when it’s initially rendered. To render a controlled select, use the | |
required | false |
Whether the user must choose a value before submitting a form. |
disabled | false |
Whether the component should ignore user interaction. |
value |
The value of the select. Use when controlled. | |
onValueChange |
Event handler called when the value of the select changes. | |
open |
Whether the select popup is currently open. | |
defaultOpen | false |
Whether the select popup is initially open. To render a controlled select popup, use the |
onOpenChange |
Event handler called when the select popup is opened or closed. | |
autoComplete |
Provides a hint to the browser for autofill. | |
itemToStringValue |
When the item values are objects ( | |
isItemEqualToValue |
Custom comparison logic used to determine if a select item value matches the current selected value. Useful when item values are objects without matching referentially.
Defaults to | |
itemToStringLabel |
When the item values are objects ( | |
highlightItemOnHover | true |
Whether moving the pointer over items should highlight them.
Disabling this prop allows CSS |
actionsRef |
A ref to imperative actions.
| |
onOpenChangeComplete |
Event handler called after any animations complete when the select popup is opened or closed. | |
modal | true |
Determines if the select enters a modal state when open.
On touch devices, a |
label Required |
The accessible label. All controls must be labeled. | |
description |
The accessible description, associated using For screen reader accessibility, this should only contain plain text, and no semantics such as links. | |
details |
Additional information about the field, which unlike a normal description, can include links and other semantic elements. Do not use this prop when the content is only plain text;
use | |
hideLabelFromVision | false |
Whether to visually hide the label while keeping it accessible to screen readers. |
className |
CSS class to apply. | |
items |
The array of option items to render in the select. | |
popupWidth | 'content' |
Controls how the popup width is constrained relative to its anchor. For all presets, the popup is never narrower than its anchor.
|
placeholder | __( 'Select' ) |
Text to show when no value is selected. This is overridden by |
triggerContent |
The custom trigger content to use instead of the default. | |
size | 'default' |
The size of the control. |
ref |
Allows getting a ref to the component instance.
Once the component unmounts, React will set | |
key |
|
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = () => <SelectControl items={defaultItems} label="Label" description="This is the description." />;
With Custom PlaceholderPermalink to this section
When no value is selected, the trigger shows the default placeholder text.
Use the placeholder prop to customize text shown.
Prefer a concise label without a trailing ellipsis.
const WithCustomPlaceholder = () => <SelectControl placeholder="Choose an item" />;
With Null Value OptionPermalink to this section
Use a null item value when users should be able to clear the selected value
from the popup.
const WithNullValueOption = () => <SelectControl
items={nullValueOptionItems}
label="Theme"
description="Choose a theme preference."
defaultValue={nullValueOptionItems[ 0 ]} />;
Visually Hidden LabelPermalink to this section
const VisuallyHiddenLabel = () => <SelectControl hideLabelFromVision />;
With DetailsPermalink to this section
const WithDetails = () => <SelectControl description={undefined} details={DETAILS_EXAMPLE} />;
With Disabled OptionPermalink to this section
const WithDisabledOption = () => <SelectControl
items={disabledOptionItems}
label="Label"
description="This is the description."
defaultValue={disabledOptionItems[ 0 ]} />;
GroupedPermalink to this section
Options can be organized into labeled groups with SelectControl.Group
and SelectControl.GroupLabel. Pass a flat items array for trigger label
resolution, and use children to render the grouped popup content.
const Grouped = () => <SelectControl
label="Fruit"
description="Choose a fruit."
items={groupedItems.flatMap( ( group ) => group.items )}>{[
groupedItems.map( ( group ) => (
<SelectControl.Group key={ group.label }>
<SelectControl.GroupLabel>
{ group.label }
</SelectControl.GroupLabel>
{ group.items.map( ( item ) => (
<SelectControl.Item
key={ item.value }
value={ item }
label={ item.label }
>
<SelectControl.ItemLabel>
{ item.label }
</SelectControl.ItemLabel>
</SelectControl.Item>
) ) }
</SelectControl.Group>
) ),
]}</SelectControl>;
With Custom Trigger And ItemsPermalink to this section
To customize what is rendered inside the trigger element, pass a
render function to the triggerContent prop.
The item list can be customized by passing an array of
SelectControl.Item as children. Note that the label prop of a SelectControl.Item
is used as the string to match against in the typeahead functionality, while the
item content is determined by children.
const WithCustomTriggerAndItems = () => <SelectControl
items={userOptions}
label="Label"
description="This is the description."
triggerContent={( item ) => <User user={ item } />}
defaultValue={userOptions[ 0 ]}>{[
userOptions.map( ( item ) => (
<SelectControl.Item
key={ item.value }
value={ item }
label={ item.label }
>
<SelectControl.ItemLabel>
<User user={ item } />
</SelectControl.ItemLabel>
</SelectControl.Item>
) ),
]}</SelectControl>;
With Items Array And Partial CustomizationPermalink to this section
By default, the items array is used to render both the Trigger
and the Item list. Passing a custom triggerContent or children in addition
to items will override that particular aspect of the behavior.
In other words, if you pass both an items array and a custom triggerContent,
the Item list in the popover will still be rendered based on the items array.
const WithItemsArrayAndPartialCustomization = () => <SelectControl>{[
Default.args?.items?.map( ( item ) => (
<SelectControl.Item
key={ item.value ?? 'null' }
value={ item }
label={ item.label }
disabled={ item.disabled }
>
<SelectControl.ItemLabel>
✨ { item.label }
</SelectControl.ItemLabel>
</SelectControl.Item>
) ),
]}</SelectControl>;
With Item DescriptionPermalink to this section
Pass description on an items entry for supplementary text.
It is announced as a description rather than part of the item name.
Compose SelectControl.Item children when you need multiple
descriptions or custom markup.
const WithItemDescription = () => <SelectControl
label="Fruit"
items={[
{
value: 'apple',
label: 'Apple',
description:
'99 in stock. Ships in two to three business days.',
},
{
value: 'banana',
label: 'Banana',
description: '12 in stock. Restock expected next week.',
},
]} />;
Popup WidthPermalink to this section
Use popupWidth to control how the popup width is constrained relative to
its anchor. Defaults to content so static option lists can grow with their
labels.
const PopupWidth = () => <SelectControl label="Label" items={longLabelPopupItems} popupWidth="content" />;