React

Select

A component that selects a value from a list that opens when a trigger is pressed.

Available since@seed-design/react@2.1.0, @seed-design/css@2.3.0

Installation

npx @seed-design/cli@latest add ui:select

Usage

import {
  SelectRoot,
  SelectTrigger,
  SelectContent,
  SelectGroup,
  SelectItem,
} from "seed-design/ui/select";
<SelectRoot defaultValue={["apple"]}>
  <SelectTrigger aria-label="Fruit" placeholder="Select a fruit" />
  <SelectContent>
    <SelectGroup label="Domestic">
      <SelectItem value="apple" label="Apple" />
      <SelectItem value="pear" label="Pear" />
    </SelectGroup>
    <SelectGroup label="Imported">
      <SelectItem value="banana" label="Banana" />
    </SelectGroup>
  </SelectContent>
</SelectRoot>
  • SelectRoot: Manages the selected value (value) and open state.
  • SelectTrigger: Displays the selected value or placeholder, and opens the list when clicked.
  • SelectContent: Floating container that wraps the option list.
  • SelectGroup: Groups related options together. All SelectItem must be inside a SelectGroup. The group title can be displayed with label.
  • SelectItem: Individual option. Requires value, and shows a checkmark when selected.

Props

SelectRoot

Prop

Type

label?React.ReactNode
indicator?React.ReactNode
showRequiredIndicator?boolean | undefined
description?React.ReactNode
errorMessage?React.ReactNode
hiddenSelectProps?SeedSelect.HiddenSelectProps | undefined
fieldRef?React.Ref<HTMLDivElement> | undefined
children?React.ReactNode
open?boolean | undefined
defaultOpen?boolean | undefined
onOpenChange?((open: boolean, details?: SelectOpenChangeDetails) => void) | undefined
defaultValue?string[] | undefined
onValueChange?((value: string[]) => void) | undefined
disabled?boolean | undefined
invalid?boolean | undefined
readOnly?boolean | undefined

SelectTrigger

Prop

Type

placeholder?React.ReactNode
prefixIcon?React.ReactNode

SelectContent

Prop

Type

positionerContainer?React.RefObject<HTMLElement | null> | undefined

SelectGroup

Prop

Type

label?React.ReactNode

SelectItem

Prop

Type

description?React.ReactNode
valuestring
disabled?boolean | undefined

Examples

Size

The size of the trigger and list is set by size. (default: large)

When using responsive, the size automatically switches based on screen width.

Groups

Group options with SelectGroup and display the group title with label. Regardless of the number of SelectGroup, all SelectItem must be inside a SelectGroup.

If there are two or more groups, a divider is automatically drawn between the groups.

Multiple Selection

Specifying multiple on SelectRoot allows selecting multiple options. The list does not close when an option is selected, and pressing an already selected option again deselects it.

The trigger displays the textValue of the selected options joined by ", ". This text can be changed with Custom Value Format.

With Description

Add supplementary descriptions to options with SelectItem's description prop.

With Prefix Icon

Display icons on options with SelectItem's prefixIcon prop.

When one option is selected, that option's icon is displayed as the trigger's prefix icon. When no option is selected, the selected option has no icon, or multiple options are selected, the trigger displays the prefixIcon specified on SelectTrigger.

Custom Item Label

You can pass a ReactNode to SelectItem's label. This node is rendered only on the options in the list.

When specifying a ReactNode instead of a string as SelectItem's label, the trigger displays SelectItem's textValue. textValue is label if label is a string, otherwise value.

Therefore, when label is not a string, it is recommended to also specify textValue. textValue is used as the text displayed on the trigger, the string matched when finding options by typing on the keyboard (typeahead), and the text of the native <option> for form submission. If you pass a ReactNode instead of a string as label without specifying textValue, these values are all set to value by default, and a warning is printed in development mode.

If you want to change only the typeahead string, use typeaheadLabel.

Disabled

Disable specific options with SelectItem's disabled, and disable the entire Select with SelectRoot's disabled.

Long List

If there are many options, the list scrolls within itself. The maximum height of the list is set to the smaller of the specified maximum height and the remaining screen space around the trigger, and it is placed to avoid the notch and home indicator areas.

The list opens scrolled to the position where the currently selected option is visible.

Placement

Set the list's position with SelectRoot's placement prop. The default value is "bottom".

Listening to Value Changes

You can control the selected value with value and onValueChange.

Controlled Open State

You can control the list's open state with open and onOpenChange. To specify only the initial state, use defaultOpen.

When the list opens, focus moves to the list, and when it closes, it returns to the trigger.

onOpenChange Details

details is provided as the second argument to onOpenChange.

reason

When opening (open: true)

  • "trigger": clicking SelectTrigger or using the ↓, ↑, Enter, Space keys while SelectTrigger is focused

When closing (open: false)

  • "trigger": clicking SelectTrigger while the list is open
  • "itemSelect": selecting an option
    • In multiple mode, this does not occur because the list does not close when an option is selected.
  • "keyboardClose": using the Enter or Space key when no option is highlighted
  • "escapeKeyDown": ESC key used
  • "interactOutside": click outside area
  • "focusOut": focus moves outside the list via Tab or similar
  • "cascadeDismiss": cascading close due to parent layer closing

Custom Value Format

Customize the selected value displayed on the trigger with SelectRoot's formatValue prop. By default, it displays the textValue of the selected options joined by ", ". The default value of textValue is label if label is a string, otherwise value.

Each item received by formatValue also contains label, so if you want to render a ReactNode label on the trigger, you can extract and use it here.

// Display only the label of the first selected option on the trigger
<SelectRoot formatValue={(items) => items[0]?.label} />

Using Intl.ListFormat allows you to combine the list with conjunctions appropriate for the locale.

Field Integration

You can use Field-related props such as label, description, and errorMessage.

Form Integration

It can participate in form submission through the hidden native <select> that SelectRoot renders internally.

React Hook Form

By connecting value, onValueChange, invalid, and errorMessage, you can control the validation state with React Hook Form's useController.

Table of contents