Select
A select displays a collapsible list of options and allows a user to select one of them.
Content
Select reuses the ListBox component, following the Collection Components API. It supports ListBox features such as static and dynamic collections, sections, disabled items, links, text slots, asynchronous loading, etc. See the ListBox docs for more details.
The following example shows a dynamic collection of items, grouped into sections.
import {Select, SelectItem} from './Select';
import {ListBoxSection, Collection, Header} from 'react-aria-components';
function Example() {
return (
<Select label="Preferred fruit or vegetable" items={options}>
{section => (
<ListBoxSection id={section.name}>
<Header>{section.name}</Header>
<Collection items={section.children}>
{item => <SelectItem id={item.name}>{item.name}</SelectItem>}
</Collection>
</ListBoxSection>
)}
</Select>
);
}
Autocomplete
Select can include additional components as siblings of the ListBox. This example uses an Autocomplete with a SearchField to let the user filter the items.
import {Select, Label, SelectValue, Autocomplete, useFilter} from 'react-aria-components';
import {Button} from './Button';
import {SelectItem} from './Select';
import {Popover} from './Popover';
import {ListBox} from './ListBox';
import {SearchField} from './SearchField';
import {ChevronDown} from 'lucide-react';
function Example() {
let {contains} = useFilter({sensitivity: 'base'});
return (
<Select>
<Label>Category</Label>
<Button>
<SelectValue />
<ChevronDown size={18} />
</Button>
<Popover hideArrow>
<Autocomplete filter={contains}>
<SearchField aria-label="Search tags" placeholder="Search tags" autoFocus style={{margin: 4}} />
<ListBox>
<SelectItem>News</SelectItem>
<SelectItem>Travel</SelectItem>
<SelectItem>Shopping</SelectItem>
<SelectItem>Business</SelectItem>
<SelectItem>Entertainment</SelectItem>
<SelectItem>Food</SelectItem>
<SelectItem>Technology</SelectItem>
<SelectItem>Health</SelectItem>
<SelectItem>Science</SelectItem>
</ListBox>
</Autocomplete>
</Popover>
</Select>
);
}
TagGroup
Use the SelectValue render prop function to display the selected items as a TagGroup.
import {Autocomplete, Select, SelectValue, Group, useFilter} from 'react-aria-components';
import {Button} from './Button';
import {ListBox, DropdownItem} from './ListBox';
import {Label} from './Form';
import {Popover} from './Popover';
import {Plus} from 'lucide-react';
import {SearchField} from './SearchField';
import {Tag, TagGroup} from './TagGroup';
import {useRef} from 'react';
import './MultiSelect.css';
function SelectWithTagGroup() {
let triggerRef = useRef<HTMLDivElement | null>(null);
let {contains} = useFilter({sensitivity: 'base'});
return (
<Select selectionMode="multiple" className="multi-select">
<Label>States</Label>
<Group aria-label="States" ref={triggerRef}>
<SelectValue<typeof states[0]> style={{flex: 1}}>
{({selectedItems, state}) => (
<TagGroup
aria-label="Selected states"
items={selectedItems.filter(item => item != null)}
renderEmptyState={() => 'No selected items'}
onRemove={(keys) => {
// Remove keys from Select state.
if (Array.isArray(state.value)) {
state.setValue(state.value.filter(k => !keys.has(k)));
}
}}>
{item => <Tag>{item.name}</Tag>}
</TagGroup>
)}
</SelectValue>
<Button variant="primary"><Plus /></Button>
</Group>
<Popover
// Position popover relative to the wrapping div instead of the Button
triggerRef={triggerRef}
hideArrow
style={{display: 'flex', flexDirection: 'column', width: 250, padding: 4}}>
<Autocomplete filter={contains}>
<SearchField aria-label="Search states" placeholder="Search states" autoFocus style={{marginBottom: 4}} />
<ListBox items={states}>
{state => <DropdownItem>{state.name}</DropdownItem>}
</ListBox>
</Autocomplete>
</Popover>
</Select>
);
}
Value
Use the defaultValue or value prop to set the selected item. The value corresponds to the id prop of an item. When selectionMode="multiple", value and onChange accept an array. Items can be disabled with the isDisabled prop.
Current selection: "bison"
Forms
Use the name prop to submit the id of the selected item to the server. Set the isRequired prop to validate that the user selects an option, or implement custom client or server-side validation. See the Forms guide to learn more.
import {Select, SelectItem} from './Select';
import {Button} from './Button';
import {Form} from './Form';
<Form>
<Select
label="Animal"
name="animal"
isRequired
description="Please select an animal.">
<SelectItem id="aardvark">Aardvark</SelectItem>
<SelectItem id="cat">Cat</SelectItem>
<SelectItem id="dog">Dog</SelectItem>
<SelectItem id="kangaroo">Kangaroo</SelectItem>
<SelectItem id="panda">Panda</SelectItem>
<SelectItem id="snake">Snake</SelectItem>
</Select>
<Button type="submit">Submit</Button>
</Form>