Combo Box
Installation
Add the component with the CLI.
npx shadcn@latest add @intentui/combo-box
Composed components
When you install this component via the CLI, it automatically loads all composed components, so you don’t need to add them individually.
Manual installation
Install the dependencies, then copy the component source into your project.
npm install react-aria-components @heroicons/react
Anatomy
import { Label } from "@/components/ui/field"
import {
ComboBox,
ComboBoxInput,
ComboBoxContent,
ComboBoxItem,
} from "@/components/ui/combo-box"<ComboBox name="users">
<Label>Users</Label>
<ComboBoxInput placeholder="Select a user" />
<ComboBoxContent>
<ComboBoxItem>John Lennon</ComboBoxItem>
<ComboBoxItem>Paul McCartney</ComboBoxItem>
<ComboBoxItem>George Harrison</ComboBoxItem>
<ComboBoxItem>Ringo Starr</ComboBoxItem>
</ComboBoxContent>
</ComboBox>Examples
Using avatars
The combo box can be configured to display avatars instead of just regular text.
Section
A combo box can display items grouped together in sections.
Readonly
When the combo box is in a readonly state, it cannot be edited.
Label
If the children of a ComboBoxItem is a string, the ComboBoxLabel will automatically wrap it. However, if you're including additional elements like an icon or avatar, you'll need to manually wrap the label, as shown below:
<ComboBoxItem id={item.id} textValue={item.name}>
<Avatar src={item.image_url} />
<ComboBoxLabel>{item.name}</ComboBoxLabel>
</ComboBoxItem>Details description
If you need to provide more information about an option, use the ComboBoxDescription component to display additional details.
Validation
If a user submits the form without selecting an item, an error message will appear.
Controlled
Control the input text with inputValue and onInputChange, or control the selected item with selectedKey and onSelectionChange.
onInputChange receives the input text, including text typed before an option is selected. onSelectionChange receives the selected item’s key.
Input
Pass inputValue and update it with onInputChange.
Selection
Pass selectedKey and update it with onSelectionChange.
Multiple select
Set selectionMode="multiple" to allow multi-select, and use TagGroup to display the selected items.
Custom value
Set allowsCustomValue to allow input that does not match a listed option.
Disabled
When the combo box is disabled, it becomes non-interactive and cannot be edited.
Component API
ComboBox
ComboBox extends the React Aria Components <ComboBox> component.
classNamestringComboBoxContent
ComboBoxContent extends the React Aria Components <ListBox> component.
classNamestringpopoverOmit<PopoverProps, "children">ComboBoxInput
ComboBoxInput extends the React Aria Components <Input> component.
classNamestringComboBoxValue
ComboBoxValue extends the React Aria Components <ComboBoxValue> component.
classNamestringComboBoxItem
ComboBoxItem extends the Intent UI <DropdownItem> component.
classNamestringintent"danger" | "warning"ComboBoxLabel
ComboBoxLabel extends the Intent UI <DropdownLabel> component.
classNamestringComboBoxDescription
ComboBoxDescription extends the Intent UI <DropdownDescription> component.
classNamestringComboBoxSection
ComboBoxSection extends the Intent UI <DropdownSection> component.
classNamestringtitlestringSee the React Aria for the full API reference.
