Skip to content

Commit

Permalink
Extract from 876490f5701f04a19ec27f607fda4a74abdbf9cc
Browse files Browse the repository at this point in the history
  • Loading branch information
akeneo committed Jul 16, 2024
1 parent 6de6fba commit 9d85877
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/components/Tree/Tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ type TreeProps<T = string> = {
onClose?: (value: T) => void;
onChange?: (value: T, checked: boolean, event: SyntheticEvent) => void;
onClick?: (value: T) => void;
defaultOpen?: boolean;
_isRoot?: boolean;
children?: ReactNode;
};
declare const Tree: {
<T>({ label, value, children, isLeaf, selected, isLoading, selectable, readOnly, onChange, onOpen, onClose, onClick, _isRoot, ...rest }: React.PropsWithChildren<TreeProps<T>>): React.JSX.Element;
<T>({ label, value, children, isLeaf, selected, isLoading, selectable, readOnly, onChange, onOpen, onClose, onClick, defaultOpen, _isRoot, ...rest }: React.PropsWithChildren<TreeProps<T>>): React.JSX.Element;
displayName: string;
};
export { Tree };
5 changes: 3 additions & 2 deletions lib/components/Tree/Tree.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/components/Tree/Tree.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "akeneo-design-system",
"version": "1.0.2",
"version": "1.0.3",
"description": "Akeneo design system",
"main": "lib/index.js",
"scripts": {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 34 additions & 4 deletions src/components/Tree/Tree.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import {Tree} from './Tree.tsx';
onClick:{
description: 'Triggered when the user clicks on a node. If not specified, a click opens or closes the node.'
},
defaultOpen:{
description: 'Allow to open automatically children when present.'
},
_isRoot:{
table:{
disable: true
Expand Down Expand Up @@ -171,15 +174,42 @@ The Tree component uses its value to trigger onOpen, onClose and other events. B
return (
<>
<Tree {...args} label='Indeterminate' selectable={true}>
<Tree {...args} label='Child1' selectable={true} selected={true} />
<Tree {...args} label='Child2' selectable={true} />
<Tree {...args} label='Child1' isLeaf={true} selectable={true} selected={true} />
<Tree {...args} label='Child2' isLeaf={true} selectable={true} />
</Tree>
<Tree {...args} label='All Checked' selectable={true}>
<Tree {...args} label='Child1' selectable={true} selected={true} />
<Tree {...args} label='Child2' selectable={true} selected={true}/>
<Tree {...args} label='Child1' isLeaf={true} selectable={true} selected={true} />
<Tree {...args} label='Child2' isLeaf={true} selectable={true} selected={true}/>
</Tree>
</>
);
}}
</Story>
</Canvas>

## Variation with open children when present

<Canvas>
<Story name="defaultOpen">
{args => {
return (
<>
<Tree {...args} label='Parent1' defaultOpen={true}>
<Tree {...args} label='Child1'>
<Tree {...args} label='Child1 Sub1' isLeaf={true} />
<Tree {...args} label='Child1 Sub2' isLeaf={true} />
</Tree>
<Tree {...args} label='Child2' isLeaf={true} />
</Tree>
<Tree {...args} label='Parent 2' defaultOpen={true}>
<Tree {...args} label='Child1' isLeaf={true} />
<Tree {...args} label='Child2'>
<Tree {...args} label='Child2 Sub1' isLeaf={true} />
<Tree {...args} label='Child2 Sub2' isLeaf={true} />
</Tree>
</Tree>
</>
);
}}
</Story>
</Canvas>
5 changes: 4 additions & 1 deletion src/components/Tree/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type TreeProps<T = string> = {
onClose?: (value: T) => void;
onChange?: (value: T, checked: boolean, event: SyntheticEvent) => void;
onClick?: (value: T) => void;
defaultOpen?: boolean;
_isRoot?: boolean;
children?: ReactNode;
};
Expand All @@ -158,6 +159,7 @@ const Tree = <T,>({
onOpen,
onClose,
onClick,
defaultOpen = false,
_isRoot = true,
...rest
}: PropsWithChildren<TreeProps<T>>) => {
Expand All @@ -180,7 +182,7 @@ const Tree = <T,>({
}
}

const [isOpen, setOpen] = React.useState<boolean>(_isRoot ? subTrees.length > 0 : false);
const [isOpen, setOpen] = React.useState<boolean>(_isRoot || defaultOpen ? subTrees.length > 0 : false);

const handleOpen = React.useCallback(() => {
setOpen(true);
Expand Down Expand Up @@ -241,6 +243,7 @@ const Tree = <T,>({
{subTrees.map(subTree =>
React.cloneElement(subTree, {
key: JSON.stringify(subTree.props.value),
defaultOpen: defaultOpen,
_isRoot: false,
})
)}
Expand Down

0 comments on commit 9d85877

Please sign in to comment.