Usage
import { Select } from "@chakra-ui/react"<Select.Root>
<Select.HiddenSelect />
<Select.Label />
<Select.Control>
<Select.Trigger>
<Select.ValueText />
</Select.Trigger>
<Select.IndicatorGroup>
<Select.Indicator />
<Select.ClearTrigger />
</Select.IndicatorGroup>
</Select.Control>
<Select.Positioner>
<Select.Content>
<Select.Item />
<Select.ItemGroup>
<Select.ItemGroupLabel />
<Select.Item />
</Select.ItemGroup>
</Select.Content>
</Select.Positioner>
</Select.Root>Examples
Sizes
Use the size prop to change the size of the select component.
Variants
Use the variant prop to change the appearance of the select component.
Option Group
Use the Select.ItemGroup component to group select options.
Controlled
Use the value and onValueChange props to control the select component.
Async Loading
Here's an example of how to populate the select collection from a remote
source.
Hook Form
Here's an example of how to use the Select component with react-hook-form.
Disabled
Use the disabled prop to disable the select component.
Invalid
Here's an example of how to compose the Select component with the Field
component to display an error state.
Multiple
Use the multiple prop to allow multiple selections.
Positioning
Use the positioning prop to control the underlying floating-ui options of
the select component.
Clear Trigger
Render the Select.ClearTrigger component to show a clear button. Clicking the
clear button will clear the selected value.
Overflow
When the options are too many, the options will overflow the container due to
the maxHeight set.
Item Description
Here's an example of how to render a description for each item.
Within Popover
Here's an example of how to use the Select within a Popover component.
Within Dialog
To use the Select within a Dialog, you need to avoid portalling the
Select.Positioner to the document's body.
-<Portal>
<Select.Positioner>
<Select.Content>
{/* ... */}
</Select.Content>
</Select.Positioner>
-</Portal>If you have set scrollBehavior="inside" on the Dialog, you need to:
- Set the select positioning to
fixedto avoid the select from being clipped by the dialog. - Set
hideWhenDetachedtotrueto hide the select when the trigger is scrolled out of view.
<Select.Root positioning={{ strategy: "fixed", hideWhenDetached: true }}>
{/* ... */}
</Select.Root>Avatar Select
Here's an example of how to compose the Select and the Avatar.
Country Select
Here's an example of how to use the Select component to select a country.
Icon Button
Here's an example of how to trigger the select component with an IconButton.
Props
Root
| Prop | Default | Type |
|---|---|---|
collection * | ListCollection<T>The collection of items | |
closeOnSelect | true | booleanWhether the select should close after an item is selected |
composite | true | booleanWhether the select is a composed with other composite widgets like tabs or combobox |
lazyMount | false | booleanWhether to enable lazy mounting |
loopFocus | false | booleanWhether to loop the keyboard navigation through the options |
skipAnimationOnMount | false | booleanWhether to allow the initial presence animation. |
unmountOnExit | false | booleanWhether to unmount on exit. |
colorPalette | gray | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'The color palette of the component |
variant | outline | 'outline' | 'subtle'The variant of the component |
size | md | 'xs' | 'sm' | 'md' | 'lg'The size of the component |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. | |
defaultHighlightedValue | stringThe initial value of the highlighted item when opened. Use when you don't need to control the highlighted value of the select. | |
defaultOpen | booleanWhether the select's open state is controlled by the user | |
defaultValue | string[]The initial default value of the select when rendered. Use when you don't need to control the value of the select. | |
deselectable | booleanWhether the value can be cleared by clicking the selected item. **Note:** this is only applicable for single selection | |
disabled | booleanWhether the select is disabled | |
form | stringThe associate form of the underlying select. | |
highlightedValue | stringThe controlled key of the highlighted item | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
content: string
control: string
trigger: string
clearTrigger: string
label: string
hiddenSelect: string
positioner: string
item: (id: string | number) => string
itemGroup: (id: string | number) => string
itemGroupLabel: (id: string | number) => string
}>The ids of the elements in the select. Useful for composition. | |
immediate | booleanWhether to synchronize the present change immediately or defer it to the next frame | |
invalid | booleanWhether the select is invalid | |
multiple | booleanWhether to allow multiple selection | |
name | stringThe `name` attribute of the underlying select. | |
onExitComplete | VoidFunctionFunction called when the animation ends in the closed state | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onHighlightChange | (details: HighlightChangeDetails<T>) => voidThe callback fired when the highlighted item changes. | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onOpenChange | (details: OpenChangeDetails) => voidFunction called when the popup is opened | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onSelect | (details: SelectionDetails) => voidFunction called when an item is selected | |
onValueChange | (details: ValueChangeDetails<T>) => voidThe callback fired when the selected item changes. | |
open | booleanWhether the select menu is open | |
positioning | PositioningOptionsThe positioning options of the menu. | |
present | booleanWhether the node is present (controlled by the user) | |
readOnly | booleanWhether the select is read-only | |
required | booleanWhether the select is required | |
scrollToIndexFn | (details: ScrollToIndexDetails) => voidFunction to scroll to a specific index | |
value | string[]The controlled keys of the selected items |
Explorer
Explore the Select component parts interactively. Click on parts in the
sidebar to highlight them in the preview.
Component Anatomy
Hover to highlight, click to select parts
label
positioner
trigger
indicator
clearTrigger
item
itemText
itemIndicator
itemGroup
itemGroupLabel
list
content
root
control
valueText
indicatorGroup