Select
View as MarkdownA dropdown for choosing a predefined value, or several.
Features
Permalink to heading "Features"- Single and multiple selection
- Typeahead focuses items as you type
- Keyboard navigation with arrow keys, home and end
- Disabled items and groups
- Hosts other widgets, like tabs, next to the list with the
dialogpopup type - Works with the Field component
- Custom positioning
Place the options inside list, which carries the listbox semantics, focus and scrolling. content wraps it and is announced as a dialog with the dialog popup type. The styled ui:select.content combines positioner and content in one part, so you nest ui:select.list in it yourself:
<ui:select.content>
<ui:select.list>
<ui:select.item>...</ui:select.item>
</ui:select.list>
</ui:select.content>
<primitives:select.root>
<primitives:select.label />
<primitives:select.control>
<primitives:select.trigger>
<primitives:select.valueText />
<primitives:select.indicator />
</primitives:select.trigger>
<primitives:select.clearTrigger />
</primitives:select.control>
<primitives:select.positioner>
<primitives:select.content>
<primitives:select.list>
<primitives:select.item>
<primitives:select.itemText />
<primitives:select.itemIndicator />
</primitives:select.item>
<primitives:select.itemGroup>
<primitives:select.itemGroupLabel />
</primitives:select.itemGroup>
</primitives:select.list>
</primitives:select.content>
</primitives:select.positioner>
<primitives:select.hiddenSelect />
</primitives:select.root>
Installation
Permalink to heading "Installation"typo3 ui:add select
Please copy the files manually from GitHub into your project.
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Default value
Permalink to heading "Default value"Pass defaultValue as a list to preselect an item. The demo opens with Banana selected.
Multiple selection
Permalink to heading "Multiple selection"Set multiple="{true}" to select several items.
Disabled items
Permalink to heading "Disabled items"Set disabled: true on an item of the collection to make it unselectable.
With item groups
Permalink to heading "With item groups"Set groupByKey on ui:listCollection to group the items, here by their type, and render an itemGroup with its label per group.
With form field
Permalink to heading "With form field"Wrap the select in ui:field.root so it takes the field's name, required and invalid state. The demo is a country select for a shipping address.
With tabs
Permalink to heading "With tabs"Set popupType="{f:constant(name: 'Jramke\FluidPrimitives\Enum\PopupType::Dialog')}" when the popup holds more than the list. content is then announced as a dialog, list stays the listbox, and you place the other widgets next to the list inside content. In the demo, tabs narrow the produce down to fruits or vegetables.
Localization
Permalink to heading "Localization"The label of the clear button ships in the language file and follows the site language. Pass translations to override it for one select.
<ui:select.root
collection="{countries}"
translations="{
clearTriggerLabel: f:translate(key: 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:select.clear')
}"
>
...
</ui:select.root>
A screen reader announces the highlighted item through a live region, and the texts ship in the language file too. itemAnnouncement sets that text and itemAnnouncementSelected the one for the selected item, both with %label% and %value% placeholders, e.g. itemAnnouncementSelected: '%label%, ausgewählt'. A client script passes itemAnnouncement as a function of { value, label, selected } instead.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
select.root
Permalink to heading "select.root"Provides shared select state and wraps all related parts. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
collection | Domain\Dto\ListCollectionThe item collection. | Yes | - |
name | stringThe `name` attribute of the underlying select. Inherited from a surrounding field when available. | No | - |
form | stringThe associate form of the underlying select. | No | - |
disabled | booleanWhether the select is disabled. Inherited from a surrounding field when available. | No | - |
invalid | booleanWhether the select is invalid. Inherited from a surrounding field when available. | No | - |
readOnly | booleanWhether the select is read-only. Inherited from a surrounding field when available. | No | - |
required | booleanWhether the select is required. Inherited from a surrounding field when available. | No | - |
closeOnSelect | booleanWhether the select should close after an item is selected. | No | true |
positioning | arrayThe positioning options of the menu. | No | {
"placement": "bottom-start",
"gutter": 8
} |
defaultValue | string|string[]The initial default value of the select when rendered. Use when you don't need to control the value of the select. Inherited from a surrounding field when available. | No | - |
defaultHighlightedValue | stringThe initial value of the highlighted item when opened. Use when you don't need to control the highlighted value of the select. | No | - |
loopFocus | booleanWhether to loop the keyboard navigation through the options. | No | false |
multiple | booleanWhether to allow multiple selection. | No | - |
defaultOpen | booleanWhether the select's open state is controlled by the user. | No | false |
popupType | Enum\PopupTypeThe kind of popup the select's content is: `listbox` for a regular select, `dialog` when it holds other interactive content. | No | Listbox |
deselectable | booleanWhether the value can be cleared by clicking the selected item. | No | - |
translations | arrayLocalized select labels. `itemAnnouncement` (the live region text of a highlighted item) and `itemAnnouncementSelected` (the same for a selected item) take `%label%` and `%value%` placeholders, and `itemAnnouncement` is a function of the item in a client script. Use `f:translate` for per-template localization overrides when needed. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-root | <uid> |
data-invalid | Present when invalid |
data-readonly | Present when read-only |
select.label
Permalink to heading "select.label"Labels the select. Renders a <label> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-label | <uid> |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-readonly | Present when read-only |
data-required | Present when required |
select.control
Permalink to heading "select.control"Groups the trigger and optional clear button. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-control | <uid> |
data-state | "open" | "closed" |
data-focus | Present when focused |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
select.trigger
Permalink to heading "select.trigger"Opens and closes the select menu. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-trigger | <uid> |
data-state | "open" | "closed" |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-readonly | Present when read-only |
data-placement | The placement of the trigger |
data-align-with-trigger | |
data-side | The side of the trigger that the trigger is positioned on |
data-placeholder-shown | Present when placeholder is shown |
select.valueText
Permalink to heading "select.valueText"Displays the selected value or placeholder text. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
placeholder | stringThe placeholder text to render when no value is selected. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-value-text | <uid> |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-focus | Present when focused |
select.indicator
Permalink to heading "select.indicator"Displays a decorative indicator for the trigger. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-indicator | <uid> |
data-state | "open" | "closed" |
data-disabled | Present when disabled |
data-invalid | Present when invalid |
data-readonly | Present when read-only |
select.clearTrigger
Permalink to heading "select.clearTrigger"Clears the current selection. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-clear-trigger | <uid> |
data-invalid | Present when invalid |
select.positioner
Permalink to heading "select.positioner"Positions the floating select content. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-positioner | <uid> |
data-nested | Present when the positioner is nested within another {{widget}} |
data-has-nested | Present when the positioner has nested {{widget}}s |
select.content
Permalink to heading "select.content"The popup surface, positioned by positioner. Holds the list and, with popupTypedialog, other widgets next to it. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-content | <uid> |
data-state | "open" | "closed" |
data-placement | The placement of the content |
data-align-with-trigger | |
data-side | The side of the trigger that the content is positioned on |
data-nested | listbox |
data-has-nested | listbox |
select.list
Permalink to heading "select.list"The listbox inside content that holds the options - it takes focus, scrolls and tracks the active option. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-list | <uid> |
data-activedescendant | The id the active descendant of the list |
select.itemGroup
Permalink to heading "select.itemGroup"Groups related options together. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value identifier of the item group. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-item-group | <uid> |
data-disabled | Present when disabled |
select.itemGroupLabel
Permalink to heading "select.itemGroupLabel"Labels a group of related options. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
select.item
Permalink to heading "select.item"Represents a selectable option. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
item | mixedThe item to render. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-item | <uid> |
data-value | The value of the item |
data-state | "checked" | "unchecked" |
data-highlighted | Present when highlighted |
data-disabled | Present when disabled |
select.itemText
Permalink to heading "select.itemText"Displays the text content of an option. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-item-text | <uid> |
data-state | "checked" | "unchecked" |
data-disabled | Present when disabled |
data-highlighted | Present when highlighted |
select.itemIndicator
Permalink to heading "select.itemIndicator"Displays the selected-state indicator for an option. Renders a <div> element.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-select-item-indicator | <uid> |
data-state | "checked" | "unchecked" |
select.hiddenSelect
Permalink to heading "select.hiddenSelect"Provides the native <select> element for form submission. Renders a <select> element, with the empty-value input that submits '' while nothing is selected in front of it.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
scrollArrowVisibility | "none" | "top" | "bottom" | "both" | Which scroll arrows should be visible based on the content's scroll position. - `"none"`: content fits, no scrolling needed - `"top"`: scrolled past the top (top arrow visible) - `"bottom"`: more content below (bottom arrow visible) - `"both"`: scrolled somewhere in the middle (both arrows visible) |
focused | boolean | Whether the select is focused |
open | boolean | Whether the select is open |
empty | boolean | Whether the select value is empty |
highlightedValue | string | null | The value of the highlighted item |
highlightedItem | V | null | The highlighted item |
setHighlightValue | (value: string) => void | Function to highlight a value |
clearHighlightValue | VoidFunction | Function to clear the highlighted value |
selectedItems | V[] | The selected items |
hasSelectedItems | boolean | Whether there's a selected option |
value | string[] | The selected item keys |
valueAsString | string | The string representation of the selected items |
selectValue | (value: string) => void | Function to select a value |
selectAll | VoidFunction | Function to select all values |
setValue | (value: string[]) => void | Function to set the value of the select |
clearValue | (value?: string) => void | Function to clear the value of the select. If a value is provided, it will only clear that value, otherwise, it will clear all values. |
focus | VoidFunction | Function to focus on the select input |
getItemState | (props: ItemProps) => ItemState | Returns the state of a select item |
setOpen | (open: boolean) => void | Function to open or close the select |
collection | ListCollection<V> | Function to toggle the select |
reposition | (options?: Partial<PositioningOptions>) => void | Function to set the positioning options of the select |
multiple | boolean | Whether the select allows multiple selections |
disabled | boolean | Whether the select is disabled |
getRootState | () => RootState | Returns the state of the root |
getTriggerState | () => TriggerState | Returns the state of the trigger |
getContentState | () => ContentState | Returns the state of the content |
Accessibility
Permalink to heading "Accessibility"| Key | Description |
|---|---|
Space | When focus is on trigger, opens the select and focuses the first selected item. When focus is on the content, selects the highlighted item. |
Enter | When focus is on trigger, opens the select and focuses the first selected item. When focus is on content, selects the focused item. |
ArrowDown | When focus is on trigger, opens the select. When focus is on content, moves focus to the next item. |
ArrowUp | When focus is on trigger, opens the select. When focus is on content, moves focus to the previous item. |
Esc | Closes the select and moves focus to trigger. |
A-Za-z | When focus is on trigger, selects the item whose label starts with the typed character. When focus is on the listbox, moves focus to the next item with a label that starts with the typed character. |