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.
Started simplifying item normalization and rendering (deephaven#1890)
- Loading branch information
Showing
7 changed files
with
264 additions
and
66 deletions.
There are no files selected for viewing
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
62 changes: 62 additions & 0 deletions
62
packages/components/src/spectrum/listView/ListViewFromChildren.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,62 @@ | ||
import { forwardRef, useMemo } from 'react'; | ||
import { | ||
ListView as SpectrumListView, | ||
SpectrumListViewProps, | ||
} from '@adobe/react-spectrum'; | ||
import type { DOMRefValue } from '@react-types/shared'; | ||
import type { ListViewPropsCommon } from './ListViewModel'; | ||
import { | ||
ItemElementOrPrimitive, | ||
ItemKey, | ||
normalizeAsItemElementList, | ||
TooltipOptions, | ||
} from '../utils'; | ||
|
||
export interface ListViewFromChildrenProps extends ListViewPropsCommon { | ||
tooltipOptions: TooltipOptions | null; | ||
children: ItemElementOrPrimitive | ItemElementOrPrimitive[]; | ||
} | ||
|
||
export const ListViewFromChildren = forwardRef< | ||
DOMRefValue<HTMLDivElement>, | ||
ListViewFromChildrenProps | ||
>( | ||
({ | ||
children, | ||
selectedKeys, | ||
defaultSelectedKeys, | ||
disabledKeys, | ||
tooltipOptions, | ||
onChange, | ||
onSelectionChange, | ||
...props | ||
}): JSX.Element => { | ||
const wrappedChildren = useMemo( | ||
() => normalizeAsItemElementList(children), | ||
[children] | ||
); | ||
|
||
return ( | ||
<SpectrumListView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
selectedKeys={ | ||
selectedKeys as SpectrumListViewProps<ItemKey>['selectedKeys'] | ||
} | ||
defaultSelectedKeys={ | ||
defaultSelectedKeys as SpectrumListViewProps<ItemKey>['defaultSelectedKeys'] | ||
} | ||
disabledKeys={ | ||
disabledKeys as SpectrumListViewProps<ItemKey>['disabledKeys'] | ||
} | ||
onSelectionChange={onChange ?? onSelectionChange} | ||
> | ||
{wrappedChildren} | ||
</SpectrumListView> | ||
); | ||
} | ||
); | ||
|
||
ListViewFromChildren.displayName = 'ListViewFromChildren'; | ||
|
||
export default ListViewFromChildren; |
77 changes: 77 additions & 0 deletions
77
packages/components/src/spectrum/listView/ListViewFromItems.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,77 @@ | ||
import { forwardRef } from 'react'; | ||
import { ListView as SpectrumListView } from '@adobe/react-spectrum'; | ||
import type { DOMRefValue } from '@react-types/shared'; | ||
import { | ||
NormalizedItem, | ||
TooltipOptions, | ||
useRenderNormalizedItem, | ||
useStringifiedMultiSelection, | ||
} from '../utils'; | ||
import { ListViewPropsCommon } from './ListViewModel'; | ||
|
||
export interface ListViewFromItemsProps extends ListViewPropsCommon { | ||
items: NormalizedItem[]; | ||
showItemDescriptions: boolean; | ||
showItemIcons: boolean; | ||
tooltipOptions: TooltipOptions | null; | ||
} | ||
|
||
export const ListViewFromItems = forwardRef< | ||
DOMRefValue<HTMLDivElement>, | ||
ListViewFromItemsProps | ||
>( | ||
( | ||
{ | ||
selectedKeys, | ||
defaultSelectedKeys, | ||
disabledKeys, | ||
showItemDescriptions, | ||
showItemIcons, | ||
tooltipOptions, | ||
items, | ||
onChange, | ||
onSelectionChange, | ||
...props | ||
}, | ||
forwardedRef | ||
): JSX.Element => { | ||
const renderNormalizedItem = useRenderNormalizedItem({ | ||
itemIconSlot: 'image', | ||
showItemDescriptions, | ||
showItemIcons, | ||
tooltipOptions, | ||
}); | ||
|
||
const { | ||
selectedStringKeys, | ||
defaultSelectedStringKeys, | ||
disabledStringKeys, | ||
onStringSelectionChange, | ||
} = useStringifiedMultiSelection({ | ||
normalizedItems: items, | ||
selectedKeys, | ||
defaultSelectedKeys, | ||
disabledKeys, | ||
onChange: onChange ?? onSelectionChange, | ||
}); | ||
|
||
return ( | ||
<SpectrumListView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
ref={forwardedRef} | ||
items={items} | ||
selectedKeys={selectedStringKeys} | ||
defaultSelectedKeys={defaultSelectedStringKeys} | ||
disabledKeys={disabledStringKeys} | ||
onSelectionChange={onStringSelectionChange} | ||
> | ||
{renderNormalizedItem} | ||
</SpectrumListView> | ||
); | ||
} | ||
); | ||
|
||
ListViewFromItems.displayName = 'ListViewFromItems'; | ||
|
||
export default ListViewFromItems; |
30 changes: 30 additions & 0 deletions
30
packages/components/src/spectrum/listView/ListViewModel.ts
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,30 @@ | ||
import { SpectrumListViewProps } from '@adobe/react-spectrum'; | ||
import type { ItemKey, ItemSelection, NormalizedItem } from '../utils'; | ||
|
||
export type ListViewPropsCommon = { | ||
selectedKeys?: 'all' | Iterable<ItemKey>; | ||
defaultSelectedKeys?: 'all' | Iterable<ItemKey>; | ||
disabledKeys?: Iterable<ItemKey>; | ||
|
||
/** | ||
* Handler that is called when the selection change. | ||
* Note that under the hood, this is just an alias for Spectrum's | ||
* `onSelectionChange`. We are renaming for better consistency with other | ||
* components. | ||
*/ | ||
onChange?: (keys: ItemSelection) => void; | ||
|
||
/** | ||
* Handler that is called when the selection changes. | ||
* @deprecated Use `onChange` instead | ||
*/ | ||
onSelectionChange?: (keys: ItemSelection) => void; | ||
} & Omit< | ||
SpectrumListViewProps<NormalizedItem>, | ||
| 'children' | ||
| 'items' | ||
| 'selectedKeys' | ||
| 'defaultSelectedKeys' | ||
| 'disabledKeys' | ||
| 'onSelectionChange' | ||
>; |
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
Oops, something went wrong.