|
| 1 | +/* eslint-disable react/jsx-props-no-spreading */ |
| 2 | +import { Key, useCallback } from 'react'; |
| 3 | +import { ComboBox, Item, SpectrumComboBoxProps } from '@adobe/react-spectrum'; |
| 4 | +import type { FocusableRef } from '@react-types/shared'; |
| 5 | +import type { ReactSpectrumComponent } from '@deephaven/react-hooks'; |
| 6 | +import TextWithTooltip from './TextWithTooltip'; |
| 7 | + |
| 8 | +export interface SearchableComboboxProps<TItem, TKey extends Key> |
| 9 | + extends Omit< |
| 10 | + SpectrumComboBoxProps<TItem>, |
| 11 | + 'children' | 'menuTrigger' | 'onSelectionChange' |
| 12 | + > { |
| 13 | + getItemDisplayText: (item: TItem | null | undefined) => string | null; |
| 14 | + getKey: (item: TItem | null | undefined) => TKey | null; |
| 15 | + scrollRef: React.RefObject<ReactSpectrumComponent<HTMLElement>>; |
| 16 | + onSelectionChange: (key: TKey | null) => void; |
| 17 | +} |
| 18 | + |
| 19 | +export function SearchableCombobox<TItem, TKey extends Key>({ |
| 20 | + scrollRef, |
| 21 | + getItemDisplayText, |
| 22 | + getKey, |
| 23 | + ...props |
| 24 | +}: SearchableComboboxProps<TItem, TKey>) { |
| 25 | + const renderItem = useCallback( |
| 26 | + item => { |
| 27 | + const key = getKey(item); |
| 28 | + const displayText = getItemDisplayText(item); |
| 29 | + |
| 30 | + return ( |
| 31 | + <Item key={key} textValue={displayText ?? String(key)}> |
| 32 | + <TextWithTooltip text={displayText} /> |
| 33 | + </Item> |
| 34 | + ); |
| 35 | + }, |
| 36 | + [getItemDisplayText, getKey] |
| 37 | + ); |
| 38 | + |
| 39 | + return ( |
| 40 | + <ComboBox |
| 41 | + {...props} |
| 42 | + // The `ref`prop type defined by React Spectrum is incorrect here |
| 43 | + ref={scrollRef as unknown as FocusableRef<HTMLElement>} |
| 44 | + menuTrigger="focus" |
| 45 | + // Type assertion is necessary since <ComboBox> types don't recognize the |
| 46 | + // generic key arg |
| 47 | + onSelectionChange={props.onSelectionChange as (key: Key | null) => void} |
| 48 | + > |
| 49 | + {renderItem} |
| 50 | + </ComboBox> |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +export default SearchableCombobox; |
0 commit comments