forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out usePickerNormalizedProps hook (deephaven#2071)
- Loading branch information
Showing
4 changed files
with
155 additions
and
94 deletions.
There are no files selected for viewing
98 changes: 7 additions & 91 deletions
98
packages/components/src/spectrum/picker/PickerNormalized.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
packages/components/src/spectrum/picker/usePickerNormalizedProps.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { Key, useCallback, useMemo } from 'react'; | ||
import { EMPTY_FUNCTION } from '@deephaven/utils'; | ||
import type { DOMRef } from '@react-types/shared'; | ||
import { | ||
getItemKey, | ||
isNormalizedSection, | ||
NormalizedItem, | ||
NormalizedSection, | ||
normalizeTooltipOptions, | ||
useRenderNormalizedItem, | ||
useStringifiedSelection, | ||
} from '../utils'; | ||
import { PickerNormalizedPropsT } from './PickerProps'; | ||
import { usePickerScrollOnOpen } from './usePickerScrollOnOpen'; | ||
import { Section } from '../shared'; | ||
|
||
export type UsePickerNormalizedDerivedProps<THtml extends HTMLElement> = { | ||
children: (itemOrSection: NormalizedItem | NormalizedSection) => JSX.Element; | ||
forceRerenderKey: Key; | ||
items: (NormalizedItem | NormalizedSection)[]; | ||
defaultSelectedKey?: Key; | ||
disabledKeys?: Iterable<Key>; | ||
ref: DOMRef<THtml>; | ||
selectedKey?: Key | null; | ||
onSelectionChange: (key: Key | null) => void; | ||
onOpenChange: (isOpen: boolean) => void; | ||
}; | ||
|
||
export type UsePickerNormalizedPassthroughProps<TProps> = Omit< | ||
PickerNormalizedPropsT<TProps>, | ||
| 'normalizedItems' | ||
| 'tooltip' | ||
| 'selectedKey' | ||
| 'defaultSelectedKey' | ||
| 'disabledKeys' | ||
| 'showItemIcons' | ||
| 'getInitialScrollPosition' | ||
| 'onChange' | ||
| 'onOpenChange' | ||
| 'onScroll' | ||
| 'onSelectionChange' | ||
>; | ||
|
||
export type UsePickerNormalizedProps< | ||
TProps, | ||
THtml extends HTMLElement, | ||
> = UsePickerNormalizedDerivedProps<THtml> & | ||
UsePickerNormalizedPassthroughProps<TProps>; | ||
|
||
export function usePickerNormalizedProps< | ||
TProps, | ||
THtml extends HTMLElement = HTMLElement, | ||
>({ | ||
normalizedItems, | ||
tooltip = true, | ||
selectedKey, | ||
defaultSelectedKey, | ||
disabledKeys, | ||
showItemIcons, | ||
getInitialScrollPosition, | ||
onChange, | ||
onOpenChange, | ||
onScroll = EMPTY_FUNCTION, | ||
onSelectionChange, | ||
...props | ||
}: PickerNormalizedPropsT<TProps>): UsePickerNormalizedProps<TProps, THtml> { | ||
const tooltipOptions = useMemo( | ||
() => normalizeTooltipOptions(tooltip), | ||
[tooltip] | ||
); | ||
|
||
const renderNormalizedItem = useRenderNormalizedItem({ | ||
itemIconSlot: 'icon', | ||
// Descriptions introduce variable item heights which throws off calculation | ||
// of initial scroll position and setting viewport on windowed data. For now | ||
// not going to implement description support in Picker. | ||
// https://github.com/deephaven/web-client-ui/issues/1958 | ||
showItemDescriptions: false, | ||
showItemIcons, | ||
tooltipOptions, | ||
}); | ||
|
||
// Spectrum doesn't re-render if only the `renderNormalizedItems` function | ||
// changes, so we create a key from its dependencies that can be used to force | ||
// re-render. | ||
const forceRerenderKey = `${showItemIcons}-${tooltipOptions?.placement}`; | ||
|
||
const { ref, onOpenChange: onOpenChangeInternal } = | ||
usePickerScrollOnOpen<THtml>({ | ||
getInitialScrollPosition, | ||
onScroll, | ||
onOpenChange, | ||
}); | ||
|
||
// Spectrum Picker treats keys as strings if the `key` prop is explicitly | ||
// set on `Item` elements. Since we do this in `renderItem`, we need to | ||
// map original key types to and from strings so that selection works. | ||
const { | ||
selectedStringKey, | ||
defaultSelectedStringKey, | ||
disabledStringKeys, | ||
onStringSelectionChange, | ||
} = useStringifiedSelection({ | ||
normalizedItems, | ||
selectedKey, | ||
defaultSelectedKey, | ||
disabledKeys, | ||
onChange: onChange ?? onSelectionChange, | ||
}); | ||
|
||
const children = useCallback( | ||
(itemOrSection: NormalizedItem | NormalizedSection) => { | ||
if (isNormalizedSection(itemOrSection)) { | ||
return ( | ||
<Section | ||
key={getItemKey(itemOrSection)} | ||
title={itemOrSection.item?.title} | ||
items={itemOrSection.item?.items} | ||
> | ||
{renderNormalizedItem} | ||
</Section> | ||
); | ||
} | ||
|
||
return renderNormalizedItem(itemOrSection); | ||
}, | ||
[renderNormalizedItem] | ||
); | ||
|
||
return { | ||
...props, | ||
children, | ||
forceRerenderKey, | ||
ref, | ||
items: normalizedItems, | ||
selectedKey: selectedStringKey, | ||
defaultSelectedKey: defaultSelectedStringKey, | ||
disabledKeys: disabledStringKeys, | ||
onSelectionChange: onStringSelectionChange, | ||
onOpenChange: onOpenChangeInternal, | ||
}; | ||
} | ||
|
||
export default usePickerNormalizedProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters