|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import { Item, Picker as SpectrumPicker } from '@adobe/react-spectrum'; |
| 3 | +import { Tooltip } from '../../popper'; |
| 4 | +import { |
| 5 | + NormalizedSpectrumPickerProps, |
| 6 | + normalizePickerItemList, |
| 7 | + normalizeTooltipOptions, |
| 8 | + PickerItem, |
| 9 | + PickerItemKey, |
| 10 | + TooltipOptions, |
| 11 | +} from './PickerUtils'; |
| 12 | +import { PickerItemContent } from './PickerItemContent'; |
| 13 | + |
| 14 | +export type PickerProps = { |
| 15 | + children: PickerItem | PickerItem[]; |
| 16 | + /** Can be set to true or a TooltipOptions to enable item tooltips */ |
| 17 | + tooltip?: boolean | TooltipOptions; |
| 18 | + /** The currently selected key in the collection (controlled). */ |
| 19 | + selectedKey?: PickerItemKey | null; |
| 20 | + /** The initial selected key in the collection (uncontrolled). */ |
| 21 | + defaultSelectedKey?: PickerItemKey; |
| 22 | + /** |
| 23 | + * Handler that is called when the selection change. |
| 24 | + * Note that under the hood, this is just an alias for Spectrum's |
| 25 | + * `onSelectionChange`. We are renaming for better consistency with other |
| 26 | + * components. |
| 27 | + */ |
| 28 | + onChange?: (key: PickerItemKey) => void; |
| 29 | + /** |
| 30 | + * Handler that is called when the selection changes. |
| 31 | + * @deprecated Use `onChange` instead |
| 32 | + */ |
| 33 | + onSelectionChange?: (key: PickerItemKey) => void; |
| 34 | +} /* |
| 35 | + * Support remaining SpectrumPickerProps. |
| 36 | + * Note that `selectedKey`, `defaultSelectedKey`, and `onSelectionChange` are |
| 37 | + * re-defined above to account for boolean types which aren't included in the |
| 38 | + * React `Key` type, but are actually supported by the Spectrum Picker component. |
| 39 | + */ & Omit< |
| 40 | + NormalizedSpectrumPickerProps, |
| 41 | + | 'children' |
| 42 | + | 'items' |
| 43 | + | 'onSelectionChange' |
| 44 | + | 'selectedKey' |
| 45 | + | 'defaultSelectedKey' |
| 46 | +>; |
| 47 | + |
| 48 | +/** |
| 49 | + * Picker component for selecting items from a list of items. Items can be |
| 50 | + * provided via the `items` prop or as children. Each item can be a string, |
| 51 | + * number, boolean, or a Spectrum <Item> element. The remaining props are just |
| 52 | + * pass through props for the Spectrum Picker component. |
| 53 | + * See https://react-spectrum.adobe.com/react-spectrum/Picker.html |
| 54 | + */ |
| 55 | +export function Picker({ |
| 56 | + children, |
| 57 | + tooltip, |
| 58 | + defaultSelectedKey, |
| 59 | + selectedKey, |
| 60 | + onChange, |
| 61 | + onSelectionChange, |
| 62 | + ...spectrumPickerProps |
| 63 | +}: PickerProps): JSX.Element { |
| 64 | + const normalizedItems = useMemo( |
| 65 | + () => normalizePickerItemList(children), |
| 66 | + [children] |
| 67 | + ); |
| 68 | + |
| 69 | + const tooltipOptions = useMemo( |
| 70 | + () => normalizeTooltipOptions(tooltip), |
| 71 | + [tooltip] |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <SpectrumPicker |
| 76 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 77 | + {...spectrumPickerProps} |
| 78 | + items={normalizedItems} |
| 79 | + // Type assertions are necessary for `selectedKey`, `defaultSelectedKey`, |
| 80 | + // and `onSelectionChange` due to Spectrum types not accounting for |
| 81 | + // `boolean` keys |
| 82 | + selectedKey={selectedKey as NormalizedSpectrumPickerProps['selectedKey']} |
| 83 | + defaultSelectedKey={ |
| 84 | + defaultSelectedKey as NormalizedSpectrumPickerProps['defaultSelectedKey'] |
| 85 | + } |
| 86 | + // `onChange` is just an alias for `onSelectionChange` |
| 87 | + onSelectionChange={ |
| 88 | + (onChange ?? |
| 89 | + onSelectionChange) as NormalizedSpectrumPickerProps['onSelectionChange'] |
| 90 | + } |
| 91 | + > |
| 92 | + {({ content, textValue }) => ( |
| 93 | + <Item textValue={textValue === '' ? 'Empty' : textValue}> |
| 94 | + <PickerItemContent>{content}</PickerItemContent> |
| 95 | + {tooltipOptions == null || content === '' ? null : ( |
| 96 | + <Tooltip options={tooltipOptions}>{content}</Tooltip> |
| 97 | + )} |
| 98 | + </Item> |
| 99 | + )} |
| 100 | + </SpectrumPicker> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +export default Picker; |
0 commit comments