Skip to content

Commit

Permalink
chore(@clayui/core): Revert "feat(@clayui/core): adds support for fla…
Browse files Browse the repository at this point in the history
…t items with dynamic content"

This reverts commit 19e2816.
  • Loading branch information
matuzalemsteles committed Sep 6, 2021
1 parent 7507259 commit f02986c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 86 deletions.
19 changes: 2 additions & 17 deletions packages/clay-core/src/tree-view/Collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

import React from 'react';

import {useTreeViewContext} from './context';
import {ItemContextProvider, useItem} from './useItem';

export type ChildrenFunction<T> = (item: T) => React.ReactElement;

export interface ICollectionProps<T> {
children: React.ReactNode | ChildrenFunction<T>;
items?: Array<T> | Record<string, T>;
rootItem?: string;
items?: Array<T>;
}

function getKey(index: number, key?: React.Key | null, parentKey?: React.Key) {
Expand All @@ -29,27 +27,14 @@ function getKey(index: number, key?: React.Key | null, parentKey?: React.Key) {

export function Collection<T extends Record<any, any>>({
children,
items: compoundItems,
rootItem,
items,
}: ICollectionProps<T>) {
const {key: parentKey} = useItem();
const {items: rootItems} = useTreeViewContext();

const items =
typeof compoundItems === 'object' &&
!Array.isArray(compoundItems) &&
rootItem
? [compoundItems[rootItem]]
: (compoundItems as Array<T>);

return (
<>
{typeof children === 'function' && items
? items.map((item, index) => {
if (typeof item === 'string') {
item = (rootItems as Record<string, T>)[item];
}

const child = children(item);

const key = getKey(
Expand Down
6 changes: 1 addition & 5 deletions packages/clay-core/src/tree-view/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export function TreeView<T>({
nestedKey,
onExpandedChange,
onSelectionChange,
rootItem,
selectedKeys,
showExpanderOnHover = true,
...otherProps
Expand All @@ -58,7 +57,6 @@ export function TreeView<T>({
? (children as ChildrenFunction<Object>)
: undefined,
expanderIcons,
items,
nestedKey,
showExpanderOnHover,
...state,
Expand All @@ -74,9 +72,7 @@ export function TreeView<T>({
role="tree"
>
<TreeViewContext.Provider value={context}>
<Collection<T> items={items} rootItem={rootItem}>
{children}
</Collection>
<Collection<T> items={items}>{children}</Collection>
</TreeViewContext.Provider>
</ul>
);
Expand Down
2 changes: 0 additions & 2 deletions packages/clay-core/src/tree-view/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import React, {useContext} from 'react';

import type {ICollectionProps} from './Collection';
import type {ITreeState} from './useTree';

export type Icons = {
Expand All @@ -17,7 +16,6 @@ export interface ITreeViewContext extends ITreeState {
childrenRoot?: (item: Object) => React.ReactElement;
expanderIcons?: Icons;
nestedKey?: string;
items?: ICollectionProps<unknown>['items'];
showExpanderOnHover?: boolean;
}

Expand Down
72 changes: 10 additions & 62 deletions packages/clay-core/stories/TreeView.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ interface IItem {
type?: string;
}

const TYPES_TO_SYMBOLS = {
container: 'container',
editable: 'text',
'fragment-image': 'picture',
'fragment-text': 'h1',
paragraph: 'paragraph',
row: 'table',
} as Record<string, string>;

const ITEMS_DRIVE = [
{
children: [
Expand Down Expand Up @@ -291,6 +282,15 @@ storiesOf('Components|ClayTreeView', module)
</Provider>
))
.add('page elements', () => {
const TYPES_TO_SYMBOLS = {
container: 'container',
editable: 'text',
'fragment-image': 'picture',
'fragment-text': 'h1',
paragraph: 'paragraph',
row: 'table',
} as Record<string, string>;

const items = [
{
children: [
Expand Down Expand Up @@ -442,56 +442,4 @@ storiesOf('Components|ClayTreeView', module)
</TreeView>
</Provider>
);
})
.add('flat items', () => (
<Provider spritemap={spritemap} theme="cadmin">
<TreeView
items={{
ab7146d1: {
children: ['p659d51e', 'q8a7c550'],
id: 'ab7146d1',
name: 'Grid',
type: 'row',
},
fd5768d1: {
children: ['ab7146d1'],
id: 'fd5768d1',
name: 'Container',
type: 'container',
},
p659d51e: {
id: 'p659d51e',
name: 'Module',
},
q8a7c550: {
id: 'q8a7c550',
name: 'Module',
},
}}
nestedKey="children"
rootItem="fd5768d1"
showExpanderOnHover={false}
>
{(item) => (
<TreeView.Item>
<TreeView.ItemStack>
{item.type && (
<Icon symbol={TYPES_TO_SYMBOLS[item.type]} />
)}
{item.name}
</TreeView.ItemStack>
<TreeView.Group items={item.children}>
{(item: any) => (
<TreeView.Item>
<Icon
symbol={TYPES_TO_SYMBOLS[item.type]}
/>
{item.name}
</TreeView.Item>
)}
</TreeView.Group>
</TreeView.Item>
)}
</TreeView>
</Provider>
));
});

0 comments on commit f02986c

Please sign in to comment.