- Components
- Menus and selection
- Select
Select
Select displays a collapsible list of options and allows a user to select one of them.
Installation
Usage
bezbez
Use Select
to allow users to choose a single option from a collapsible list of options when space is limited.
Options
Placeholder
Use the placeholder
prop to provide a temporary text that occupies the text input when it is empty.
Label
A visual label can be provided for the Select
using the label
prop, or a hidden label using aria-label
prop.
Description
A description can be supplied to a Select via the description
prop. The description is always visible unless the isInvalid
prop is true
and an error message is provided.
Contextual help
A ContextualHelp
element may be placed next to the label to provide additional information or help about a Select
.
Validation
An errorMessage
can be supplied to Select
, which will be displayed when the isInvalid
prop is set to true
.
Sections
Select supports sections in order to group options. Sections can be used by wrapping groups of items in a Section
element.
Sections without a title
must provide an aria-label
for accessibility.
Links
Items may be links to another page or website. This can be achieved by passing the href
prop to the Item
component.
Disabled
The isDisabled
prop can be used to disable the Select.
Loading
Use the isLoading
prop to indicate that the list is loading.
Required
Use the isRequired
prop to mark the TextField as required.
Use the necessityIndicator
prop to control the visual style of the required state.
Uncontrolled
Use defaultSelectedKey
to provide a default set of selected item.
Controlled
Use selectedKey
to control the selected item.
Selected provider: eleven-labs
Composition
If you need to customize things further, you can drop down to the composition level.
Examples
Asynchronous laoding
API Reference
Select
Prop | Type | Default | Description |
---|---|---|---|
autoComplete | string | - | Describes the type of autocomplete functionality the input should provide if any. |
name | string | - | The name of the input, used when submitting an HTML form. |
isOpen | boolean | - | Sets the open state of the menu. |
defaultOpen | boolean | - | Sets the default open state of the menu. |
disabledKeys | Iterable<Key> | - | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
isDisabled | boolean | - | Whether the input is disabled. |
isRequired | boolean | - | Whether user input is required on the input before form submission. |
isInvalid | boolean | - | Whether the input value is invalid. |
validate | (value: Key) => ValidationError | true | null | undefined | - | A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead. |
placeholder | string | - | Temporary text that occupies the text input when it is empty. |
selectedKey | Key | null | - | The currently selected key in the collection (controlled). |
defaultSelectedKey | Key | - | The initial selected key in the collection (uncontrolled). |
autoFocus | boolean | - | Whether the element should receive focus on render. |
validationBehavior | 'native' | 'aria' | 'native' | Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. |
children | ReactNode | (item: object) => ReactNode | - | The contents of the collection. |
dependencies | any[] | - | Values that should invalidate the item cache when using dynamic collections. |
className | string | (values: SelectRenderProps & {defaultClassName: string | undefined}) => string | - | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (values: SelectRenderProps & {defaultStyle: CSSProperties}) => CSSProperties | - | The inline style for the element. A function may be provided to compute the style based on component state. |
Event | Type | Description |
---|---|---|
onOpenChange | (isOpen: boolean) => void | Method that is called when the open state of the menu changes. |
onSelectionChange | (key: Key) => void | Handler that is called when the selection changes. |
onFocus | (e: FocusEvent<Target>) => void | Handler that is called when the element receives focus. |
onBlur | (e: FocusEvent<Target>) => void | Handler that is called when the element loses focus. |
onFocusChange | (isFocused: boolean) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (e: KeyboardEvent) => void | Handler that is called when a key is pressed. |
onKeyUp | (e: KeyboardEvent) => void | Handler that is called when a key is released. |
Data attribute | Description |
---|---|
[data-focused] | Whether the select is focused, either via a mouse or keyboard. |
[data-focus-visible] | Whether the select is keyboard focused. |
[data-disabled] | Whether the select is disabled. |
[data-open] | Whether the select is currently open. |
[data-invalid] | Whether the select is invalid. |
[data-required] | Whether the select is required. |
SelectValue
Prop | Type | Default | Description |
---|---|---|---|
children | ReactNode | (values: SelectValueRenderProps<object> & {defaultChildren: ReactNode | undefined}) => ReactNode | - | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (values: SelectValueRenderProps<object> & {defaultClassName: string | undefined}) => string | - | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (values: SelectValueRenderProps<object> & {defaultStyle: CSSProperties}) => CSSProperties | - | The inline style for the element. A function may be provided to compute the style based on component state. |
Accessibility
Keyboard interactions
When the popover is closed:
Key | Description |
---|---|
Space ArrowDown | Opens the popover menu. The focus is set on the menu item selected. |
When the popover menu is open:
Key | Description |
---|---|
Space | Selects the menu item in focus, closes the popover menu and moves focus to the field button. |
ArrowDown ArrowUp | Moves focus to previous or next menu item in the popover. Does not loop when the last or first menu item is reached. |
Esc | Closes the popover menu and moves focus to the field button. |