oakoss/ui
Components

Select

Choose one or several options from a list, with a label, description and validation message.

SourceReact Aria
FruitPicked fresh daily.

Installation

npx shadcn@latest add @oakoss/select

Usage

import { Select, SelectItem } from '#/components/ui/inputs/select';

<Select label="Fruit">
  <SelectItem id="apple">Apple</SelectItem>
  <SelectItem id="pear">Pear</SelectItem>
</Select>;

The children are the options. label, description, errors, errorMessage, placeholder and size (sm, md, lg, matching Input's heights) set up the field around the trigger, as on Text Field. start and end put text or an aria-hidden icon inside the trigger, and text is added to its description. They sit inside the trigger's button, which takes its name from the label and value, so a named element there isn't announced, and a link or button there is invalid HTML that assistive tech may flatten into the trigger; axe reports it as nested-interactive. Keep only text and aria-hidden icons there.

Examples

Sections and descriptions

SelectSection groups options under a SelectHeader, and each section after the first draws a line above it. description on SelectItem describes the option in the list; the trigger shows its label alone.

Plan

Multiple selection

selectionMode="multiple" takes an array value. The list stays open between picks, and the trigger lists the picks in the user's language. Pass items with a function as children for a list built from data, and dependencies when that function reads other state, so the options re-render when it changes.

Toppings

Custom layout

For a layout of your own, use React Aria's Select with the parts: SelectTrigger, SelectValue, SelectContent with SelectItem, SelectSection and SelectHeader, and the Field parts. The trigger still turns destructive when the field is invalid.

CountryWhere we ship your order.

Autofill

React Aria renders a hidden native <select> so browsers can autofill the field and forms submit its value. Give name and autoComplete, and use the values autofill produces as the options' ids (country codes for autoComplete="country", for example). Above 300 options the hidden select becomes a hidden input, and autofill stops.

Accessibility

React Aria renders a button that opens a listbox in a popover, and links it to its parts:

  • Name and description: the trigger is named by its value and the label, and described by the description and error; start and end text is added to the description.
  • Options: an option's description describes it in the list, and is hidden in the trigger, so it isn't read as part of the trigger's name.
  • Validation: an invalid field turns the trigger's border destructive.

The trigger shows the focus outline on keyboard focus, and its hit area reaches 44px. In Windows High Contrast the focused option takes the system's selection colors, and a disabled option takes the system's disabled color.

KeyAction
Enter, Space, ↓Opens the list on the selected option, or the first one
↑Opens the list on the selected option, or the last one
←, →Selects the previous or next option without opening (single)
TypingSelects the first option that starts with the letters (single)
EscapeCloses the list

In the list, the arrow keys move between options and Enter or Space chooses one.

API

Select

Extends React Aria's Select.

Props

Prop

Type

Also accepts all React Aria Select props.

Data attributes

AttributePresent when
data-slotAlways select
data-openThe list is open
data-invalidThe field is invalid
data-disabledisDisabled is set
data-requiredisRequired is set

SelectItem

Extends React Aria's ListBoxItem.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways select-item
data-focusedThe option has focus
data-selectedThe option is selected
data-disabledThe option is disabled

Other parts

PartExtendsdata-slot
SelectTriggerReact Aria's Button, with sizeselect-trigger
SelectValueReact Aria's SelectValueselect-value
SelectContentOur PopoverContent around a ListBoxselect-content
SelectSectionReact Aria's ListBoxSectionselect-group
SelectHeaderReact Aria's Headerselect-label