Select
Choose a single option from a collapsible list of options.
<Select aria-label="Country Select"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Usage
Selection
The listbox components supports either no selection, single and multiple selection modes using the
selectionMode
prop.
function Selection() { const options = [ { id: 1, name: 'United States' }, { id: 2, name: 'Canada' }, { id: 3, name: 'Mexico' }, { id: 4, name: 'United Kingdom' }, { id: 5, name: 'France' }, { id: 6, name: 'China' }, { id: 7, name: 'India' }, { id: 8, name: 'Turkey' }, ]; const [selected, setSelected] = React.useState('Turkey'); return ( <Select aria-label="Country Select" selectedKey={selected} onSelectionChange={setSelected}> {options.map((item) => ( <SelectItem key={item.name}>{item.name}</SelectItem> ))} </Select> ); }
Sections
Group items within a dropdown using a section.
<Select aria-label="Country Select"> <SelectSection title="North America"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </SelectSection> <SelectSection title="Europe"> <SelectItem>United Kingdom</SelectItem> <SelectItem>France</SelectItem> </SelectSection> <SelectSection title="Asia"> <SelectItem>China</SelectItem> <SelectItem>India</SelectItem> </SelectSection> </Select>
Async
Items can be loaded asynchronously use the useAsyncData
hook. It also supports infinite scrolling
to load more data on demand as the user scrolls, via the onLoadMore prop.
function Async() { const list = useAsyncList({ async load({ signal }) { const res = await fetch('https://restcountries.com/v3.1/alpha?codes=US,CA,MX,GB,FR,CN,IN', { signal, }); const items = await res.json(); return { items }; }, }); return ( <Select aria-label="Country Select" items={list.items}> {(item) => <SelectItem key={item.name.common}>{item.name.common}</SelectItem>} </Select> ); }
Label
Attach a label to the combobox using the label
prop.
<Select label="Country"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
HelperText
Add additional context as well as display error messages with the helperText
prop.
<Select label="Country" helperText="Please select a country"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Small
A small select is used when vertical spacing is limited.
<Select aria-label="Country Select" size="small"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Start Icon
Include an icon before the input text.
<Select aria-label="status" startIcon={<Search />}> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Item Icon
Include an icon before the item label on each item.
<Select aria-label="status"> <SelectItem startIcon={<Truck />}>Truck</SelectItem> <SelectItem startIcon={<Train />}>Train</SelectItem> <SelectItem startIcon={<Airplane />}>Airplane</SelectItem> <SelectItem startIcon={<Boat />}>Ship</SelectItem> </Select>
Controlled
A select's state can be controlled using the selectedKeys
prop and onSelectionChange
handler.
function ControlledExample() { const data = [ { id: 1, name: 'United States' }, { id: 2, name: 'Canada' }, { id: 3, name: 'Mexico' }, { id: 4, name: 'United Kingdom' }, { id: 5, name: 'France' }, { id: 6, name: 'China' }, { id: 7, name: 'India' }, { id: 8, name: 'Turkey' }, ]; const [selected, setSelected] = React.useState('Turkey'); return ( <Select aria-label="status" items={data} onSelectionChange={setSelected} selectedKey={selected}> {(item) => <SelectItem key={item.name}>{item.name}</SelectItem>} </Select> ); }
Disabled State
Set the isDisabled
prop to prevent a user from interacting with the select.
<Select aria-label="status" isDisabled> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Disabled items
Set the disabledKeys
prop to disable certain items in the select list.
<Select aria-label="status" disabledKeys={['canada']}> <SelectItem key="united_states">United States</SelectItem> <SelectItem key="canada">Canada</SelectItem> <SelectItem key="mexico">Mexico</SelectItem> </Select>
Invalid State
Set the validationState
prop to invalid
to render the select in an invalid state.
<Select aria-label="status" helperText="A selection is required" validationState="invalid"> <SelectItem>United States</SelectItem> <SelectItem>Canada</SelectItem> <SelectItem>Mexico</SelectItem> </Select>
Props
Select Props
as?The root element to be rendered
SelectItem Props
as?The root element to be rendered
SelectSection Props
as?The root element to be rendered