Picker
| install | yarn add @react-spectrum/picker |
|---|---|
| version | 3.0.0-alpha.1 |
| usage | import {Picker} from '@react-spectrum/picker' |
Example#
<Picker label="Choose frequency" onSelectionChange=selected => consolelog(selected)>
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Content#
Picker follows the collections API, accepting both static and dynamic forms of children. Similar to the Menu, Picker accepts an <Item> component, each with a uniqueKey prop. Basic usage of the Picker, seen in the example above, shows multiple options populated with a string. This implementation would be used when the list of options is finite and known.
The dynamic method (shown below) would be better suited to use if the options came from a data object such as values returned from an API call. Providing the data in this way allows for Picker to automatically cache the rendering of each item, which dramatically improves performance.
As seen below, an iterable list of options is passed to the Picker using the items prop. The value to be passed to the onSelectionChange handler is set via the uniqueKey prop. See the events section for more detail on selection.s
function Example() {
let options = [
{name: 'Aardvark'}
{name: 'Cat'}
{name: 'Dog'}
{name: 'Kangaroo'}
{name: 'Koala'}
{name: 'Penguin'}
{name: 'Snake'}
{name: 'Turtle'}
{name: 'Wombat'}
];
return (
// Dynamic Items
<Picker label="Pick an animal" items=options onSelectionChange=selected => consolelog(selected)>
item => <Item uniqueKey=itemname>itemname</Item>
</Picker>
);
}Internationalization#
To internationalize a Picker, a localized string should be passed to the children of each Item.
For languages that are read right to left (e.g. Hebrew and Arabic), the layout of the Picker is flipped.
Labeling#
Picker can be labeled using the label prop. If no label is provided, an alternative text label must be provided to identify the control for accessibility. This should be added using the aria-label prop. If the Picker is a required field, the isRequired and necessityIndicator props can be used to show a required state.
<Picker label="Choose frequency" isRequired necessityIndicator="icon" marginEnd="10px">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>
<Picker label="Choose frequency" isRequired necessityIndicator="label" marginEnd="10px">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>
<Picker label="Choose frequency" necessityIndicator="label">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Selection#
Setting a selected option can be done by using the defaultSelectedKey or selectedKey prop. The selected key corresponds to the uniqueKey of an item. See Events for more details on selection events.
function Example() {
let options = [
{name: 'Koala'}
{name: 'Kangaroo'}
{name: 'Platypus'}
{name: 'Bald Eagle'}
{name: 'Bison'}
{name: 'Skunk'}
];
return (
<Picker
label="Pick an animal" items=options defaultSelectedKey="Bison">
item => <Item uniqueKey=itemname>itemname</Item>
</Picker>
);
}Sections#
Picker supports sections in order to group options. Sections can be used by wrapping groups of Items in a Section component. Each Section takes a title and uniqueKey prop.
Static Items#
<Picker label="Pick an animal">
<Section uniqueKey="Animals" title="Animals">
<Item uniqueKey="Aardvark">Aardvark</Item>
<Item uniqueKey="Kangaroo">Kangaroo</Item>
<Item uniqueKey="Snake">Snake</Item>
</Section>
<Section uniqueKey="People" title="People">
<Item uniqueKey="Danni">Danni</Item>
<Item uniqueKey="Devon">Devon</Item>
<Item uniqueKey="Ross">Ross</Item>
</Section>
</Picker>Dynamic Items#
Sections used with dynamic items are populated from a hierarchical data structure. Similarly to the props on Picker, Section takes an array of data using the items prop.
function Example() {
let options = [
{name: 'Australian' children: [
{id: 2 name: 'Koala'}
{id: 3 name: 'Kangaroo'}
{id: 4 name: 'Platypus'}
]}
{name: 'American' children: [
{id: 6 name: 'Bald Eagle'}
{id: 7 name: 'Bison'}
{id: 8 name: 'Skunk'}
]}
];
return (
<>
<Picker label="Pick an animal" items=options onSelectionChange=selected => alert(selected)>
item => (
<Section uniqueKey=itemname items=itemchildren title=itemname>
item => <Item>itemname</Item>
</Section>
)
</Picker>
</>
);
}Events#
Picker supports selection via mouse, keyboard, and touch. You can handle all of these via the onSelectionChange
prop. Picker will pass the selected uniqueKey to the onSelectionChange callback.
The following example uses an onSelectionChange callback to update the selection stored in React state.
function StaticExample() {
let [frequency setFrequency] = ReactuseState();
return (
<>
<Picker label="Choose frequency" onSelectionChange=selected => setFrequency(selected)>
<Item uniqueKey="Rarely">Rarely</Item>
<Item uniqueKey="Sometimes">Sometimes</Item>
<Item uniqueKey="Always">Always</Item>
</Picker>
<p>You selected frequency</p>
</>
);
}When using Picker with dynamic items, selection works much the same way using uniqueKey. However, if your data already has an id key (as is common with many data sets), Picker can use that id without needing to specify a uniqueKey prop. The below example shows Picker using the id of each item from the items array as the selected value without the need for uniqueKey. Note that uniqueKey will always take precedence if set.
function DynamicExample() {
let [animalId setAnimalId] = ReactuseState();
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="Pick an animal" items=options onSelectionChange=selected => setAnimalId(selected)>
item => <Item>itemname</Item>
</Picker>
<p>Your favorite animal has id: animalId</p>
</>
);
}Complex Items#
<Picker label="Test" >
<Section title="Section 1">
<Item textValue="Copy">
<Copy size="S" />
<Text>Copy</Text>
</Item>
<Item textValue="Cut">
<Cut size="S" />
<Text>Cut</Text>
</Item>
<Item textValue="Paste">
<Paste size="S" />
<Text>Paste</Text>
</Item>
</Section>
<Section title="Section 2">
<Item textValue="Puppy">
<AlignLeft size="S" />
<Text>Puppy</Text>
<Text slot="description">Puppy description super long as well geez</Text>
</Item>
<Item textValue="Doggo with really really really long long long text">
<AlignCenter size="S" />
<Text>Doggo with really really really long long long text</Text>
</Item>
<Item textValue="Floof">
<AlignRight size="S" />
<Text>Floof</Text>
</Item>
</Section>
</Picker>Props#
| Name | Type | Default | Description |
Visual Options#
Label Alignment#
<Picker label="Choose frequency" labelAlign="end">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Label Position#
<Picker label="Choose frequency" labelPosition="side">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Quiet#
<Picker label="Choose frequency" isQuiet labelPosition="side">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Disabled#
<Picker label="Choose frequency" isDisabled>
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>Custom Widths#
<Flex flexDirection="column">
<Picker label="Choose frequency" width="size-6000">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>
<Picker label="Choose animal" menuWidth="size-6000">
<Item uniqueKey="Emu">Emu</Item>
<Item uniqueKey="Kangaroo">Kangaroo</Item>
<Item uniqueKey="Platypus">Platypus</Item>
</Picker>
</Flex>Align and Direction#
<Flex flexDirection="column">
<Picker label="Choose frequency" align="start">
<Item uniqueKey="rarely">Rarely</Item>
<Item uniqueKey="sometimes">Sometimes</Item>
<Item uniqueKey="always">Always</Item>
</Picker>
<Picker label="Choose animal" direction="top">
<Item uniqueKey="Emu">Emu</Item>
<Item uniqueKey="Kangaroo">Kangaroo</Item>
<Item uniqueKey="Platypus">Platypus</Item>
</Picker>
</Flex>TODO Align Direction shouldFlip Open
// TODO add exaple of controlled // call out id as key in earlier // do the same examples in list box // mobile behavior - maybe in an iframe