Pickers allow users to choose a single option from a collapsible list of options when space is limited.
Content
Picker follows the Collection Components API, accepting both static and dynamic collections. This example shows a dynamic collection, passing a list of objects to the items prop, and a function to render the children.
import {Picker, PickerItem} from '@react-spectrum/s2/Picker';
function Example() {
let options = [
{ id: 1, name: 'Aardvark' },
{ id: 2, name: 'Cat' },
{ id: 3, name: 'Dog' },
{ id: 4, name: 'Kangaroo' },
{ id: 5, name: 'Koala' },
{ id: 6, name: 'Penguin' },
{ id: 7, name: 'Snake' },
{ id: 8, name: 'Turtle' },
{ id: 9, name: 'Wombat' }
];
return (
<Picker label="Animals" items={options}>
{(item) => <PickerItem>{item.name}</PickerItem>}
</Picker>
);
}
Slots
PickerItem supports icons, avatars, and label and description text slots.
import {Avatar} from '@react-spectrum/s2/Avatar';
import {Picker, PickerItem, Text} from '@react-spectrum/s2/Picker';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import Comment from '@react-spectrum/s2/icons/Comment';
import Edit from '@react-spectrum/s2/icons/Edit';
import UserSettings from '@react-spectrum/s2/icons/UserSettings';
const users = Array.from({length: 5}, (_, i) => ({
id: `user${i + 1}`,
name: `User ${i + 1}`,
email: `user${i + 1}@example.com`,
avatar: 'https://i.imgur.com/kJOwAdv.png'
}));
<div className={style({display: 'flex', gap: 12, flexWrap: 'wrap'})}>
<Picker label="Permissions" defaultValue="read">
<PickerItem id="read" textValue="Read">
<Comment />
<Text slot="label">Read</Text>
<Text slot="description">Comment only</Text>
</PickerItem>
<PickerItem id="write" textValue="Write">
<Edit />
<Text slot="label">Write</Text>
<Text slot="description">Read and write only</Text>
</PickerItem>
<PickerItem id="admin" textValue="Admin">
<UserSettings />
<Text slot="label">Admin</Text>
<Text slot="description">Full access</Text>
</PickerItem>
</Picker>
<Picker label="Share" items={users} defaultValue="user1">
{(item) => (
<PickerItem id={item.id} textValue={item.name}>
<Avatar slot="avatar" src={item.avatar} />
<Text slot="label">{item.name}</Text>
<Text slot="description">{item.email}</Text>
</PickerItem>
)}
</Picker>
</div>
Accessibility
Sections
Use the <PickerSection> component to group options. A <Header> element, with a <Heading> and optional description slot can be included to label a section. Sections without a header must have an aria-label.
import {Picker, PickerSection, PickerItem, Header, Heading, Text} from '@react-spectrum/s2/Picker';
<Picker label="Ice cream flavor">
<PickerSection>
<Header>
<Heading>Neopolitan flavors</Heading>
<Text slot="description">These flavors are common</Text>
</Header>
<PickerItem>Chocolate</PickerItem>
<PickerItem>Strawberry</PickerItem>
<PickerItem>Vanilla</PickerItem>
</PickerSection>
<PickerSection>
<Header>
<Heading>Others</Heading>
<Text slot="description">These flavors are uncommon</Text>
</Header>
<PickerItem>Matcha</PickerItem>
<PickerItem>Ube</PickerItem>
<PickerItem>Prickly pear</PickerItem>
</PickerSection>
</Picker>
Asynchronous loading
Use the loadingState and onLoadMore props to enable async loading and infinite scrolling.
import {Picker, PickerItem} from '@react-spectrum/s2/Picker';
import {useAsyncList} from '@react-spectrum/s2/useAsyncList';
interface Character {
name: string
}
function AsyncLoadingExample() {
let list = useAsyncList<Character>({
async load({signal, cursor}) {
let res = await fetch(
cursor || `https://pokeapi.co/api/v2/pokemon`,
{ signal }
);
let json = await res.json();
return {
items: json.results,
cursor: json.next
};
}
});
return (
<Picker
aria-label="Pokemon"
items={list.items}
loadingState={list.loadingState}
onLoadMore={list.loadMore}>
{(item) => <PickerItem id={item.name}>{item.name}</PickerItem>}
</Picker>
);
}
Links
Use the href prop on a <PickerItem> to create a link. See the getting started guide to learn how to integrate with your framework. Link items in a Picker are not selectable.
import {Picker, PickerItem} from '@react-spectrum/s2/Picker';
<Picker label="Project">
<PickerItem href="https://example.com/" target="_blank">Create new…</PickerItem>
<PickerItem>Proposal</PickerItem>
<PickerItem>Budget</PickerItem>
<PickerItem>Onboarding</PickerItem>
</Picker>
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"
Custom Render Value
Use the renderValue prop to provide a custom element to display selected items. The callback is given an array of the selected user-defined objects.
import {AvatarGroup, Avatar} from '@react-spectrum/s2/AvatarGroup';
import {Picker, PickerItem, Text} from '@react-spectrum/s2/Picker';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
function Example() {
return (
<div>
<Picker
label="Pick users"
items={users}
selectionMode={"multiple"}
renderValue={(selectedItems) => (
<AvatarGroup aria-label="Selected users" styles={style({marginX: 4})}>
{selectedItems.map(item => (
<Avatar key={item.id} src={item.avatar} alt={item.name} />
))}
</AvatarGroup>
)}
>
{(item) =>
<PickerItem textValue={item.name}>
<Avatar slot="avatar" src={item.avatar} />
<Text slot="label">{item.name}</Text>
<Text slot="description">{item.email}</Text>
</PickerItem>
}
</Picker>
</div>
);
}
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.
Popover options
The open state of the Picker can be controlled via the defaultOpen and isOpen props. The align, direction, shouldFlip and menuWidth props control the behavior of the popover.
Select is closed
API
<Picker>
<PickerItem>
<Icon /> or <Avatar />
<Text slot="label" />
<Text slot="description" />
</PickerItem>
<PickerSection>
<Header>
<Heading />
<Text slot="description" />
</Header>
<PickerItem />
</PickerSection>
</Picker>
Picker
| Name | Type | Default |
|---|---|---|
renderValue | | Default: — |
Custom renderer for the picker value. Allows one to provide a custom element to render selected items. | ||
placeholder | string | Default: 'Select an item' (localized)
|
Temporary text that occupies the select when it is empty. | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the Picker. | ||
isQuiet | boolean | Default: — |
Whether the picker should be displayed with a quiet style. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
children | ReactNode | | Default: — |
The contents of the collection. | ||
items | Iterable | Default: — |
Item objects in the collection. | ||
loadingState | LoadingState | Default: — |
The current loading state of the Picker. | ||
onLoadMore | | Default: — |
Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. | ||
dependencies | ReadonlyArray | Default: — |
Values that should invalidate the item cache when using dynamic collections. | ||
selectionMode | SelectionMode | Default: 'single'
|
Whether single or multiple selection is enabled. | ||
disabledKeys | Iterable | Default: — |
The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. | ||
value | ValueType | Default: — |
The current value (controlled). | ||
defaultValue | ValueType | Default: — |
The default value (uncontrolled). | ||
onChange | | Default: — |
Handler that is called when the value changes. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
necessityIndicator | NecessityIndicator | Default: 'icon'
|
Whether the required state should be shown as an icon or text. | ||
description | ReactNode | Default: — |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode | | Default: — |
An error message for the field. | ||
isOpen | boolean | Default: — |
Sets the open state of the menu. | ||
defaultOpen | boolean | Default: — |
Sets the default open state of the menu. | ||
onOpenChange | | Default: — |
Method that is called when the open state of the menu changes. | ||
shouldCloseOnSelect | boolean | Default: — |
Whether the Select should close when an item is selected. Defaults to true if selectionMode is single, false otherwise. | ||
direction | 'bottom' | 'top' | Default: 'bottom'
|
Direction the menu will render relative to the Picker. | ||
align | 'start' | 'end' | Default: 'start'
|
Alignment of the menu relative to the input target. | ||
shouldFlip | boolean | Default: true
|
Whether the element should flip its orientation (e.g. top to bottom or left to right) when there is insufficient room for it to render completely. | ||
menuWidth | number | Default: — |
Width of the menu. By default, matches width of the trigger. Note that the minimum width of the dropdown is always equal to the trigger's width. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
name | string | Default: — |
The name of the input, used when submitting an HTML form. | ||
autoComplete | string | Default: — |
Describes the type of autocomplete functionality the input should provide if any. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
PickerItem
| Name | Type | |
|---|---|---|
children | ReactNode | |
id | Key | |
The unique id of the item. | ||
value | T | |
The object value that this item represents. When using dynamic collections, this is set automatically. | ||
textValue | string | |
A string representation of the item's contents, used for features like typeahead. | ||
isDisabled | boolean | |
Whether the item is disabled. | ||
styles | StylesProp | |
Spectrum-defined styles, returned by the | ||
onAction | | |
Handler that is called when a user performs an action on the item. The exact user event depends
on the collection's | ||
onHoverStart | | |
Handler that is called when a hover interaction starts. | ||
onHoverEnd | | |
Handler that is called when a hover interaction ends. | ||
onHoverChange | | |
Handler that is called when the hover state changes. | ||
onPress | | |
Handler that is called when the press is released over the target. | ||
onPressStart | | |
Handler that is called when a press interaction starts. | ||
onPressEnd | | |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | |
Handler that is called when the press state changes. | ||
onPressUp | | |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
onKeyDown | | |
Handler that is called when a key is pressed. | ||
onKeyUp | | |
Handler that is called when a key is released. | ||
onFocus | | |
Handler that is called when the element receives focus. | ||
onBlur | | |
Handler that is called when the element loses focus. | ||
onFocusChange | | |
Handler that is called when the element's focus status changes. | ||
href | Href | |
A URL to link to. See MDN. | ||
hrefLang | string | |
Hints at the human language of the linked URL. SeeMDN. | ||
target | HTMLAttributeAnchorTarget | |
The target window for the link. See MDN. | ||
rel | string | |
The relationship between the linked resource and the current page. See MDN. | ||
download | boolean | string | |
Causes the browser to download the linked URL. A string may be provided to suggest a file name. See MDN. | ||
ping | string | |
A space-separated list of URLs to ping when the link is followed. See MDN. | ||
referrerPolicy | HTMLAttributeReferrerPolicy | |
How much of the referrer to send when following the link. See MDN. | ||
routerOptions | RouterOptions | |
Options for the configured client side router. | ||
aria-label | string | |
An accessibility label for this item. | ||
UNSAFE_className | UnsafeClassName | |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | |
Sets inline style for the
element. Only use as a last resort. Use the | ||
PickerSection
| Name | Type | |
|---|---|---|
id | Key | |
The unique id of the section. | ||
value | T | |
The object value that this section represents. When using dynamic collections, this is set automatically. | ||
children | ReactNode | | |
Static child items or a function to render children. | ||
items | Iterable | |
Item objects in the section. | ||
dependencies | ReadonlyArray | |
Values that should invalidate the item cache when using dynamic collections. | ||
aria-label | string | |
An accessibility label for the section. | ||