-
Notifications
You must be signed in to change notification settings - Fork 1.4k
wip: allow custom react element for S2 Picker value #9541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7087906
6425a6f
eabc625
d5c4f3c
195d5d9
8324d81
061e14a
8b90e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,6 +242,54 @@ function Example(props) { | |
| } | ||
| ``` | ||
|
|
||
| ### 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. | ||
|
|
||
| ```tsx render | ||
| "use client"; | ||
| import {Avatar, Picker, PickerItem, Text} from '@react-spectrum/s2'; | ||
| import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused |
||
| import {useState} from 'react'; | ||
|
|
||
| function Example(props) { | ||
| const users = Array.from({length: 5}, (_, i) => ({ | ||
| id: `user${i + 1}`, | ||
| name: `User ${i + 1}`, | ||
| email: `user${i + 1}@example.com`, | ||
| avatar: 'https://mir-s3-cdn-cf.behance.net/project_modules/disp/690bc6105945313.5f84bfc9de488.png', | ||
| })); | ||
|
|
||
| return ( | ||
| <div> | ||
| <Picker | ||
| {...props} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there are no controllable props in this example, you can remove this (and the argument above). That should fix the TS issue I think (it should be able to infer the generic based on |
||
| label="Pick an animal" | ||
| items={users} | ||
| selectionMode={"multiple"} | ||
| ///- begin highlight -/// | ||
| renderValue={(selectedItems) => ( | ||
| <div style={{display: 'flex', gap: 4, height: '80%'}}> | ||
| {selectedItems.map(item => ( | ||
| <img key={item.id} src={item.avatar} /> | ||
| ))} | ||
| </div> | ||
| )} | ||
| ///- end highlight -/// | ||
devongovett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| > | ||
| {(item) => | ||
| <PickerItem> | ||
| <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](forms) guide to learn more. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Locked height to picker button height. Enough for our use case as we don't have anything larger than the button that should cause it to resize.