@rovula/ui 0.0.48 → 0.0.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/bundle.css +9 -0
- package/dist/cjs/bundle.js +3 -3
- package/dist/cjs/bundle.js.map +1 -1
- package/dist/cjs/types/components/Tree/Tree.d.ts +4 -45
- package/dist/cjs/types/components/Tree/Tree.stories.d.ts +8 -1
- package/dist/cjs/types/components/Tree/TreeItem.d.ts +4 -0
- package/dist/cjs/types/components/Tree/index.d.ts +4 -0
- package/dist/cjs/types/components/Tree/type.d.ts +76 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/components/Tree/Tree.js +15 -49
- package/dist/components/Tree/Tree.stories.js +117 -8
- package/dist/components/Tree/TreeItem.js +81 -0
- package/dist/components/Tree/index.js +4 -0
- package/dist/components/Tree/type.js +1 -0
- package/dist/esm/bundle.css +9 -0
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/types/components/Tree/Tree.d.ts +4 -45
- package/dist/esm/types/components/Tree/Tree.stories.d.ts +8 -1
- package/dist/esm/types/components/Tree/TreeItem.d.ts +4 -0
- package/dist/esm/types/components/Tree/index.d.ts +4 -0
- package/dist/esm/types/components/Tree/type.d.ts +76 -0
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/index.d.ts +82 -2
- package/dist/index.js +1 -0
- package/dist/src/theme/global.css +12 -0
- package/package.json +1 -1
- package/src/components/Tree/Tree.stories.tsx +230 -8
- package/src/components/Tree/Tree.tsx +44 -183
- package/src/components/Tree/TreeItem.tsx +231 -0
- package/src/components/Tree/index.ts +5 -0
- package/src/components/Tree/type.ts +90 -0
- package/src/index.ts +1 -0
|
@@ -1,45 +1,4 @@
|
|
|
1
|
-
import { FC
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
icon?: ReactNode;
|
|
6
|
-
children?: TreeData[];
|
|
7
|
-
};
|
|
8
|
-
export interface TreeItemProps extends TreeData {
|
|
9
|
-
isFirstLevel?: boolean;
|
|
10
|
-
isLastItem: boolean;
|
|
11
|
-
checkIsExpanded: (id: string) => boolean;
|
|
12
|
-
checkIsChecked: (id: string) => boolean;
|
|
13
|
-
onExpandChange?: (id: string, expanded: boolean) => void;
|
|
14
|
-
onCheckedChange?: (id: string, checked: boolean) => void;
|
|
15
|
-
}
|
|
16
|
-
export type TreeProps = {
|
|
17
|
-
data: TreeData[];
|
|
18
|
-
defaultExpandedId?: string[];
|
|
19
|
-
defaultCheckedId?: string[];
|
|
20
|
-
checkedId?: string[];
|
|
21
|
-
onCheckedChange?: (checkedId: string[]) => void;
|
|
22
|
-
defaultExpandAll?: boolean;
|
|
23
|
-
defaultCheckAll?: boolean;
|
|
24
|
-
hierarchicalCheck?: boolean;
|
|
25
|
-
};
|
|
26
|
-
export declare const Tree: FC<TreeProps>;
|
|
27
|
-
/**
|
|
28
|
-
* TODO
|
|
29
|
-
* -----
|
|
30
|
-
* - Custom style
|
|
31
|
-
* - Custom icon, elm and render props -> callback with selected*expanded
|
|
32
|
-
* - OnClick item
|
|
33
|
-
* - OnClick expandButton
|
|
34
|
-
* - disabled props
|
|
35
|
-
* - right section icon
|
|
36
|
-
* - props for show icon, line
|
|
37
|
-
* - props for render item
|
|
38
|
-
* - OnLoad mode
|
|
39
|
-
* -----
|
|
40
|
-
* - props onLoad item
|
|
41
|
-
* - props for hasChildren * for check to trigger on load
|
|
42
|
-
* - animate expand
|
|
43
|
-
* - check duplicate reversive on updateChildren
|
|
44
|
-
* - write storybook
|
|
45
|
-
*/
|
|
1
|
+
import { FC } from "react";
|
|
2
|
+
import { TreeProps } from "./type";
|
|
3
|
+
declare const Tree: FC<TreeProps>;
|
|
4
|
+
export default Tree;
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from "@storybook/react";
|
|
2
|
-
import
|
|
2
|
+
import Tree from "./Tree";
|
|
3
3
|
declare const meta: Meta<typeof Tree>;
|
|
4
4
|
export default meta;
|
|
5
5
|
export declare const Default: StoryObj<typeof Tree>;
|
|
6
|
+
export declare const onClick: StoryObj<typeof Tree>;
|
|
7
|
+
export declare const CustomIcon: StoryObj<typeof Tree>;
|
|
8
|
+
export declare const renderRightSection: StoryObj<typeof Tree>;
|
|
9
|
+
export declare const ControlShowExpandButton: StoryObj<typeof Tree>;
|
|
10
|
+
export declare const Diabled: StoryObj<typeof Tree>;
|
|
11
|
+
export declare const DiabledEachItem: StoryObj<typeof Tree>;
|
|
12
|
+
export declare const ExpandLoadData: StoryObj<typeof Tree>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { CSSProperties, ReactNode } from "react";
|
|
2
|
+
export type TreeData = {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
icon?: ReactNode;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
onClickItem?: (id: string) => void;
|
|
8
|
+
children?: TreeData[];
|
|
9
|
+
renderIcon?: (params: {
|
|
10
|
+
id: string;
|
|
11
|
+
expanded: boolean;
|
|
12
|
+
selected: boolean;
|
|
13
|
+
}) => ReactNode;
|
|
14
|
+
};
|
|
15
|
+
export interface TreeItemProps extends TreeData {
|
|
16
|
+
isFirstLevel?: boolean;
|
|
17
|
+
isLastItem: boolean;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
showIcon?: boolean;
|
|
20
|
+
showExpandButton?: boolean;
|
|
21
|
+
enableSeparatorLine?: boolean;
|
|
22
|
+
checkIsExpanded: (id: string) => boolean;
|
|
23
|
+
checkIsChecked: (id: string) => boolean;
|
|
24
|
+
checkIsLoading?: (id: string) => void;
|
|
25
|
+
onExpandChange?: (id: string, expanded: boolean) => void;
|
|
26
|
+
onCheckedChange?: (id: string, checked: boolean) => void;
|
|
27
|
+
renderRightSection?: (params: {
|
|
28
|
+
id: string;
|
|
29
|
+
expanded: boolean;
|
|
30
|
+
selected: boolean;
|
|
31
|
+
}) => ReactNode;
|
|
32
|
+
renderElement?: (params: {
|
|
33
|
+
id: string;
|
|
34
|
+
expanded: boolean;
|
|
35
|
+
selected: boolean;
|
|
36
|
+
children: ReactNode;
|
|
37
|
+
styles: {
|
|
38
|
+
branch: CSSProperties;
|
|
39
|
+
horizontalLine: CSSProperties;
|
|
40
|
+
expandButton: CSSProperties;
|
|
41
|
+
childPadding: CSSProperties;
|
|
42
|
+
};
|
|
43
|
+
onClick?: TreeItemProps["onClickItem"];
|
|
44
|
+
}) => ReactNode;
|
|
45
|
+
renderTitle?: (params: {
|
|
46
|
+
id: string;
|
|
47
|
+
title: string;
|
|
48
|
+
expanded: boolean;
|
|
49
|
+
selected: boolean;
|
|
50
|
+
}) => ReactNode;
|
|
51
|
+
classes?: Partial<{
|
|
52
|
+
elementWrapper: string;
|
|
53
|
+
branch: string;
|
|
54
|
+
itemWrapper: string;
|
|
55
|
+
itemContainer: string;
|
|
56
|
+
horizontalLine: string;
|
|
57
|
+
expandButton: string;
|
|
58
|
+
separatorLine: string;
|
|
59
|
+
checkbox: string;
|
|
60
|
+
item: string;
|
|
61
|
+
title: string;
|
|
62
|
+
childrenWrapper: string;
|
|
63
|
+
}>;
|
|
64
|
+
}
|
|
65
|
+
export interface TreeProps extends Pick<TreeItemProps, "renderIcon" | "renderRightSection" | "renderElement" | "renderTitle" | "showIcon" | "disabled" | "enableSeparatorLine" | "classes"> {
|
|
66
|
+
data: TreeData[];
|
|
67
|
+
defaultExpandedId?: string[];
|
|
68
|
+
defaultCheckedId?: string[];
|
|
69
|
+
checkedId?: string[];
|
|
70
|
+
loadingId?: string[];
|
|
71
|
+
onExpandChange?: (id: string, expanded: boolean) => void;
|
|
72
|
+
onCheckedChange?: (checkedId: string[]) => void;
|
|
73
|
+
defaultExpandAll?: boolean;
|
|
74
|
+
defaultCheckAll?: boolean;
|
|
75
|
+
hierarchicalCheck?: boolean;
|
|
76
|
+
}
|
|
@@ -32,6 +32,7 @@ export * from "./components/Tooltip/TooltipSimple";
|
|
|
32
32
|
export * from "./components/Toast/Toast";
|
|
33
33
|
export * from "./components/Toast/Toaster";
|
|
34
34
|
export * from "./components/Toast/useToast";
|
|
35
|
+
export * from "./components/Tree";
|
|
35
36
|
export type { ButtonProps } from "./components/Button/Button";
|
|
36
37
|
export type { InputProps } from "./components/TextInput/TextInput";
|
|
37
38
|
export type { DropdownProps, Options } from "./components/Dropdown/Dropdown";
|
|
@@ -1,48 +1,7 @@
|
|
|
1
|
-
import { jsx as _jsx
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
const
|
|
5
|
-
const isChecked = useMemo(() => checkIsChecked(id), [checkIsChecked, id]);
|
|
6
|
-
const isExpanded = useMemo(() => checkIsExpanded(id), [checkIsExpanded, id]);
|
|
7
|
-
const hasChildren = useMemo(() => !!(children === null || children === void 0 ? void 0 : children.length), [children]);
|
|
8
|
-
const handleExpandToggle = useCallback(() => {
|
|
9
|
-
onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange(id, !isExpanded);
|
|
10
|
-
}, [id, isExpanded, onExpandChange]);
|
|
11
|
-
// TODO move to props
|
|
12
|
-
const lineSize = 2;
|
|
13
|
-
const horizontalLineWidth = 4;
|
|
14
|
-
const expandButtonSize = 30;
|
|
15
|
-
const spacing = 2;
|
|
16
|
-
const styles = {
|
|
17
|
-
branch: {
|
|
18
|
-
height: isLastItem
|
|
19
|
-
? `calc(50% + ${lineSize}px)`
|
|
20
|
-
: `calc(100% + ${lineSize}px)`,
|
|
21
|
-
width: lineSize,
|
|
22
|
-
marginTop: -lineSize,
|
|
23
|
-
borderBottomLeftRadius: lineSize / 2,
|
|
24
|
-
},
|
|
25
|
-
horizontalLine: {
|
|
26
|
-
height: lineSize,
|
|
27
|
-
width: lineSize +
|
|
28
|
-
horizontalLineWidth +
|
|
29
|
-
(hasChildren ? 0 : expandButtonSize + spacing),
|
|
30
|
-
marginLeft: -lineSize + 0.1,
|
|
31
|
-
borderBottomLeftRadius: lineSize / 2,
|
|
32
|
-
},
|
|
33
|
-
expandButton: {
|
|
34
|
-
width: expandButtonSize,
|
|
35
|
-
height: expandButtonSize,
|
|
36
|
-
},
|
|
37
|
-
childPadding: {
|
|
38
|
-
paddingLeft: isFirstLevel
|
|
39
|
-
? expandButtonSize / 2 - lineSize / 2
|
|
40
|
-
: expandButtonSize / 2 + horizontalLineWidth - lineSize / 2,
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
return (_jsxs("div", { className: "flex flex-row w-full", children: [_jsx("div", { className: cn("bg-grey-150", { "h-1/2": isLastItem }), style: styles.branch }), _jsxs("div", { className: "flex flex-col w-full", children: [_jsxs("div", { className: "flex items-center py-2 min-h-10", children: [!isFirstLevel && (_jsx("div", { className: "bg-grey-150", style: styles.horizontalLine })), isFirstLevel && !hasChildren && (_jsx("div", { className: "flex mr-[2px]", style: styles.expandButton })), hasChildren && (_jsx("div", { className: "flex mr-[2px]", style: styles.expandButton, children: _jsx(ActionButton, { variant: "icon", size: "sm", onClick: handleExpandToggle, children: _jsx(Icon, { name: isExpanded ? "chevron-down" : "chevron-right" }) }) })), _jsx(Checkbox, { id: id, className: "size-[16pt]", checked: isChecked, onCheckedChange: (newChecked) => onCheckedChange === null || onCheckedChange === void 0 ? void 0 : onCheckedChange(id, newChecked) }), _jsxs("div", { className: "ml-2 gap-1 flex items-center text-foreground", children: [_jsx(Icon, { name: isExpanded ? "folder-open" : "folder", className: "fill-warning" }), _jsx("label", { htmlFor: id, className: "flex-1 cursor-pointer text-subtitle5 text-ellipsis", children: title })] })] }), isExpanded && hasChildren && (_jsx("div", { className: "flex flex-col", style: styles.childPadding, children: children === null || children === void 0 ? void 0 : children.map((child, idx) => (_jsx(TreeItem, Object.assign({}, child, { isLastItem: idx === children.length - 1, checkIsExpanded: checkIsExpanded, checkIsChecked: checkIsChecked, onExpandChange: onExpandChange, onCheckedChange: onCheckedChange }), child.id))) }))] })] }));
|
|
44
|
-
};
|
|
45
|
-
export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], checkedId, onCheckedChange, defaultExpandAll = false, defaultCheckAll = false, hierarchicalCheck = false, }) => {
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
import TreeItem from "./TreeItem";
|
|
4
|
+
const Tree = ({ classes, data, defaultExpandedId = [], defaultCheckedId = [], checkedId, loadingId, renderIcon, renderRightSection, renderElement, renderTitle, onExpandChange, onCheckedChange, defaultExpandAll = false, defaultCheckAll = false, hierarchicalCheck = false, showIcon = true, disabled, enableSeparatorLine = true, }) => {
|
|
46
5
|
const [checkedState, setCheckedState] = useState({});
|
|
47
6
|
const [expandedState, setExpandedState] = useState({});
|
|
48
7
|
const traverseTree = (nodes, callback) => {
|
|
@@ -61,7 +20,7 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
|
|
|
61
20
|
});
|
|
62
21
|
setExpandedState(allExpanded);
|
|
63
22
|
}
|
|
64
|
-
else {
|
|
23
|
+
else if (defaultExpandedId === null || defaultExpandedId === void 0 ? void 0 : defaultExpandedId.length) {
|
|
65
24
|
const initialExpandedState = defaultExpandedId.reduce((acc, id) => {
|
|
66
25
|
acc[id] = true;
|
|
67
26
|
return acc;
|
|
@@ -77,7 +36,7 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
|
|
|
77
36
|
});
|
|
78
37
|
setCheckedState(allChecked);
|
|
79
38
|
}
|
|
80
|
-
else if (!checkedId) {
|
|
39
|
+
else if (!checkedId && (defaultCheckedId === null || defaultCheckedId === void 0 ? void 0 : defaultCheckedId.length)) {
|
|
81
40
|
const initialCheckedState = defaultCheckedId.reduce((acc, id) => {
|
|
82
41
|
acc[id] = true;
|
|
83
42
|
return acc;
|
|
@@ -86,8 +45,9 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
|
|
|
86
45
|
}
|
|
87
46
|
}, [data, defaultCheckedId, checkedId, defaultCheckAll]);
|
|
88
47
|
const handleExpandChange = useCallback((id, expanded) => {
|
|
48
|
+
onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange(id, expanded);
|
|
89
49
|
setExpandedState((prev) => (Object.assign(Object.assign({}, prev), { [id]: expanded })));
|
|
90
|
-
}, []);
|
|
50
|
+
}, [onExpandChange]);
|
|
91
51
|
const handleCheckedChange = useCallback((id, checked) => {
|
|
92
52
|
let newState = Object.assign(Object.assign({}, checkedState), { [id]: checked });
|
|
93
53
|
if (hierarchicalCheck) {
|
|
@@ -134,5 +94,11 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
|
|
|
134
94
|
}
|
|
135
95
|
return !!checkedState[id];
|
|
136
96
|
}, [checkedId, checkedState]);
|
|
137
|
-
|
|
97
|
+
const checkIsLoading = useCallback((id) => {
|
|
98
|
+
if (loadingId) {
|
|
99
|
+
return loadingId.includes(id);
|
|
100
|
+
}
|
|
101
|
+
}, [loadingId]);
|
|
102
|
+
return (_jsx("div", { className: "w-full", children: data.map((item, idx) => (_jsx(TreeItem, Object.assign({ classes: classes, isFirstLevel: true, isLastItem: idx === data.length - 1, checkIsExpanded: checkIsExpanded, checkIsChecked: checkIsChecked, onExpandChange: handleExpandChange, onCheckedChange: handleCheckedChange, checkIsLoading: checkIsLoading, renderIcon: renderIcon, renderElement: renderElement, renderTitle: renderTitle, renderRightSection: renderRightSection, enableSeparatorLine: enableSeparatorLine, disabled: disabled, showIcon: showIcon }, item), item.id))) }));
|
|
138
103
|
};
|
|
104
|
+
export default Tree;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import Tree from "./Tree";
|
|
4
|
+
import { ActionButton, Icon } from "@/index";
|
|
4
5
|
const exampleData = [
|
|
5
6
|
{
|
|
6
7
|
id: "1",
|
|
@@ -24,6 +25,15 @@ const exampleData = [
|
|
|
24
25
|
},
|
|
25
26
|
{ id: "3", title: "Parent Folder 3" },
|
|
26
27
|
];
|
|
28
|
+
const commonProps = {
|
|
29
|
+
defaultExpandedId: ["1", "1.1"],
|
|
30
|
+
defaultCheckedId: ["1.1"],
|
|
31
|
+
defaultExpandAll: true,
|
|
32
|
+
defaultCheckAll: true,
|
|
33
|
+
hierarchicalCheck: true,
|
|
34
|
+
disabled: false,
|
|
35
|
+
showIcon: true,
|
|
36
|
+
};
|
|
27
37
|
// Storybook metadata
|
|
28
38
|
const meta = {
|
|
29
39
|
title: "Components/Tree",
|
|
@@ -39,15 +49,114 @@ const meta = {
|
|
|
39
49
|
export default meta;
|
|
40
50
|
// Default story
|
|
41
51
|
export const Default = {
|
|
52
|
+
args: Object.assign(Object.assign({ data: exampleData }, commonProps), { showIcon: true }),
|
|
53
|
+
render: (args) => {
|
|
54
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args)) }));
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
export const onClick = {
|
|
58
|
+
args: Object.assign({ data: exampleData.map((item) => (Object.assign(Object.assign({}, item), { onClickItem: (id) => alert("Click item " + id) }))) }, commonProps),
|
|
59
|
+
render: (args) => {
|
|
60
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args)) }));
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
export const CustomIcon = {
|
|
64
|
+
args: Object.assign({ data: exampleData.map((item, idx) => (Object.assign(Object.assign(Object.assign({}, item), (idx === 0 && {
|
|
65
|
+
icon: _jsx(Icon, { name: "home" }),
|
|
66
|
+
})), (idx === 1 && {
|
|
67
|
+
renderIcon: ({ expanded, selected }) => (_jsx(Icon, { name: expanded ? "home" : "home-modern", className: selected ? "fill-info" : "fill-error" })),
|
|
68
|
+
})))) }, commonProps),
|
|
69
|
+
render: (args) => {
|
|
70
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args, { renderIcon: ({ expanded, selected }) => (_jsx(Icon, { name: expanded ? "bell" : "bell-slash", className: selected ? "fill-primary" : "fill-secondary" })) })) }));
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
export const renderRightSection = {
|
|
74
|
+
args: Object.assign({ data: exampleData }, commonProps),
|
|
75
|
+
render: (args) => {
|
|
76
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args, { renderRightSection: () => (_jsx(ActionButton, { variant: "icon", onClick: () => alert("Say hi!"), children: _jsx(Icon, { name: "ellipsis-vertical" }) })) })) }));
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
export const ControlShowExpandButton = {
|
|
42
80
|
args: {
|
|
43
|
-
data: exampleData,
|
|
44
|
-
defaultExpandedId: ["1", "1.1"],
|
|
45
|
-
defaultCheckedId: ["1.1"],
|
|
46
|
-
defaultExpandAll: true,
|
|
47
|
-
defaultCheckAll: true,
|
|
48
|
-
hierarchicalCheck: true,
|
|
81
|
+
data: exampleData.map((item) => (Object.assign(Object.assign({}, item), { showExpandButton: true }))),
|
|
49
82
|
},
|
|
50
83
|
render: (args) => {
|
|
51
84
|
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args)) }));
|
|
52
85
|
},
|
|
53
86
|
};
|
|
87
|
+
export const Diabled = {
|
|
88
|
+
args: Object.assign(Object.assign({ data: exampleData }, commonProps), { disabled: true }),
|
|
89
|
+
render: (args) => {
|
|
90
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args, { renderRightSection: () => (_jsx(ActionButton, { variant: "icon", onClick: () => alert("Say hi!"), children: _jsx(Icon, { name: "ellipsis-vertical" }) })) })) }));
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
export const DiabledEachItem = {
|
|
94
|
+
args: Object.assign(Object.assign({ data: exampleData.map((item, i) => (Object.assign(Object.assign({}, item), { disabled: i === 0 }))) }, commonProps), { disabled: undefined }),
|
|
95
|
+
render: (args) => {
|
|
96
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args, { renderRightSection: () => (_jsx(ActionButton, { variant: "icon", onClick: () => alert("Say hi!"), children: _jsx(Icon, { name: "ellipsis-vertical" }) })) })) }));
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
export const ExpandLoadData = {
|
|
100
|
+
args: {},
|
|
101
|
+
render: (args) => {
|
|
102
|
+
const [data, setData] = useState([
|
|
103
|
+
{
|
|
104
|
+
id: "1",
|
|
105
|
+
title: "Parent Folder 1",
|
|
106
|
+
showExpandButton: true,
|
|
107
|
+
children: [],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: "2",
|
|
111
|
+
title: "Parent Folder 2",
|
|
112
|
+
showExpandButton: true,
|
|
113
|
+
children: [],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: "3",
|
|
117
|
+
title: "Parent Folder 3",
|
|
118
|
+
showExpandButton: true,
|
|
119
|
+
children: [],
|
|
120
|
+
},
|
|
121
|
+
]);
|
|
122
|
+
const [loadingId, setLoadingId] = useState([]);
|
|
123
|
+
const [loadedId, setLoadedId] = useState([]);
|
|
124
|
+
const updateNode = (nodes, id, newChildren) => nodes.map((node) => {
|
|
125
|
+
var _a;
|
|
126
|
+
if (node.id === id) {
|
|
127
|
+
return Object.assign(Object.assign({}, node), { children: newChildren });
|
|
128
|
+
}
|
|
129
|
+
if ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) {
|
|
130
|
+
return Object.assign(Object.assign({}, node), { children: updateNode(node.children, id, newChildren) });
|
|
131
|
+
}
|
|
132
|
+
return node;
|
|
133
|
+
});
|
|
134
|
+
const handleOnExpandChange = (id, isExpand) => {
|
|
135
|
+
// *Note can you other way for improve should load if need with other way without loadedId
|
|
136
|
+
if (isExpand && !loadingId.includes(id) && !loadedId.includes(id)) {
|
|
137
|
+
setLoadingId((prev) => [...prev, id]);
|
|
138
|
+
setTimeout(() => {
|
|
139
|
+
// Mock child data
|
|
140
|
+
const newChildren = [
|
|
141
|
+
{
|
|
142
|
+
id: Date.now() + "1",
|
|
143
|
+
title: `Child of ${id} - 1`,
|
|
144
|
+
children: [],
|
|
145
|
+
showExpandButton: true,
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: Date.now() + "2",
|
|
149
|
+
title: `Child of ${id} - 2`,
|
|
150
|
+
children: [],
|
|
151
|
+
showExpandButton: true,
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
setData((prevData) => updateNode(prevData, id, newChildren));
|
|
155
|
+
setLoadingId((prev) => prev.filter((val) => val !== id));
|
|
156
|
+
setLoadedId((prev) => [...prev, id]);
|
|
157
|
+
}, 1500);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return (_jsx("div", { className: "flex flex-row gap-4 w-full", children: _jsx(Tree, Object.assign({}, args, { data: data, loadingId: loadingId, onExpandChange: handleOnExpandChange })) }));
|
|
161
|
+
},
|
|
162
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ActionButton, Checkbox, Loading } from "@/index";
|
|
3
|
+
import { cn } from "@/utils/cn";
|
|
4
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
5
|
+
import Icon from "../Icon/Icon";
|
|
6
|
+
const TreeItem = ({ id, title, classes, children, isFirstLevel = false, disabled, icon, showIcon, showExpandButton, enableSeparatorLine = true, isLastItem, checkIsExpanded, checkIsChecked, checkIsLoading, onExpandChange, onCheckedChange, onClickItem, renderIcon, renderElement, renderTitle, renderRightSection, }) => {
|
|
7
|
+
const isLoading = useMemo(() => checkIsLoading === null || checkIsLoading === void 0 ? void 0 : checkIsLoading(id), [checkIsLoading, id]);
|
|
8
|
+
const isChecked = useMemo(() => checkIsChecked(id), [checkIsChecked, id]);
|
|
9
|
+
const isExpanded = useMemo(() => checkIsExpanded(id), [checkIsExpanded, id]);
|
|
10
|
+
const hasChildren = useMemo(() => !!(children === null || children === void 0 ? void 0 : children.length), [children]);
|
|
11
|
+
const shouldExpandButton = useMemo(() => (showExpandButton !== undefined ? showExpandButton : hasChildren), [hasChildren, showExpandButton]);
|
|
12
|
+
const handleExpandToggle = useCallback(() => {
|
|
13
|
+
onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange(id, !isExpanded);
|
|
14
|
+
}, [id, isExpanded, onExpandChange]);
|
|
15
|
+
// TODO move to props
|
|
16
|
+
const lineSize = 2;
|
|
17
|
+
const horizontalLineWidth = 4;
|
|
18
|
+
const expandButtonSize = 30;
|
|
19
|
+
const spacing = 2;
|
|
20
|
+
const styles = {
|
|
21
|
+
branch: {
|
|
22
|
+
height: isLastItem
|
|
23
|
+
? `calc(50% + ${lineSize}px)`
|
|
24
|
+
: `calc(100% + ${lineSize}px)`,
|
|
25
|
+
width: lineSize,
|
|
26
|
+
marginTop: -lineSize,
|
|
27
|
+
borderBottomLeftRadius: lineSize / 2,
|
|
28
|
+
},
|
|
29
|
+
horizontalLine: {
|
|
30
|
+
height: lineSize,
|
|
31
|
+
width: lineSize +
|
|
32
|
+
horizontalLineWidth +
|
|
33
|
+
(shouldExpandButton ? 0 : expandButtonSize + spacing),
|
|
34
|
+
marginLeft: -lineSize + 0.1,
|
|
35
|
+
borderBottomLeftRadius: lineSize / 2,
|
|
36
|
+
},
|
|
37
|
+
expandButton: {
|
|
38
|
+
width: expandButtonSize,
|
|
39
|
+
height: expandButtonSize,
|
|
40
|
+
},
|
|
41
|
+
childPadding: {
|
|
42
|
+
paddingLeft: isFirstLevel
|
|
43
|
+
? expandButtonSize / 2 - lineSize / 2
|
|
44
|
+
: expandButtonSize / 2 + horizontalLineWidth - lineSize / 2,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (isExpanded && !isLoading && !hasChildren) {
|
|
49
|
+
handleExpandToggle();
|
|
50
|
+
}
|
|
51
|
+
}, [isLoading, handleExpandToggle]);
|
|
52
|
+
const handleOnClickItem = useCallback(() => {
|
|
53
|
+
onClickItem === null || onClickItem === void 0 ? void 0 : onClickItem(id);
|
|
54
|
+
}, [onClickItem, id]);
|
|
55
|
+
const defaultIcon = (_jsx(Icon, { name: isExpanded ? "folder-open" : "folder", className: "fill-warning" }));
|
|
56
|
+
const customIcon = icon !== null && icon !== void 0 ? icon : renderIcon === null || renderIcon === void 0 ? void 0 : renderIcon({
|
|
57
|
+
id,
|
|
58
|
+
expanded: isExpanded,
|
|
59
|
+
selected: isChecked,
|
|
60
|
+
});
|
|
61
|
+
const rightIcon = renderRightSection === null || renderRightSection === void 0 ? void 0 : renderRightSection({
|
|
62
|
+
id,
|
|
63
|
+
expanded: isExpanded,
|
|
64
|
+
selected: isChecked,
|
|
65
|
+
});
|
|
66
|
+
const titleContent = renderTitle
|
|
67
|
+
? renderTitle({ id, title, expanded: isExpanded, selected: isChecked })
|
|
68
|
+
: title;
|
|
69
|
+
const elementWrapper = (content) => renderElement
|
|
70
|
+
? renderElement({
|
|
71
|
+
id,
|
|
72
|
+
expanded: isExpanded,
|
|
73
|
+
selected: isChecked,
|
|
74
|
+
children: content,
|
|
75
|
+
styles,
|
|
76
|
+
onClick: handleOnClickItem,
|
|
77
|
+
})
|
|
78
|
+
: content;
|
|
79
|
+
return elementWrapper(_jsxs("div", { className: cn("flex flex-row w-full", classes === null || classes === void 0 ? void 0 : classes.elementWrapper), children: [_jsx("div", { className: cn("bg-grey-150", { "h-1/2": isLastItem }, classes === null || classes === void 0 ? void 0 : classes.branch), style: styles.branch }), _jsxs("div", { className: cn("flex flex-col w-full", classes === null || classes === void 0 ? void 0 : classes.itemWrapper), children: [_jsxs("div", { className: cn("flex items-center py-2 min-h-10", classes === null || classes === void 0 ? void 0 : classes.itemContainer), children: [!isFirstLevel && (_jsx("div", { className: cn("bg-grey-150", classes === null || classes === void 0 ? void 0 : classes.horizontalLine), style: styles.horizontalLine })), isFirstLevel && !shouldExpandButton && (_jsx("div", { className: cn("flex mr-[2px]", classes === null || classes === void 0 ? void 0 : classes.expandButton), style: styles.expandButton })), shouldExpandButton && (_jsx("div", { className: cn("flex mr-[2px]", classes === null || classes === void 0 ? void 0 : classes.expandButton), style: styles.expandButton, onClick: !isLoading && handleExpandToggle, children: _jsx(ActionButton, { variant: "icon", size: "sm", children: isLoading ? (_jsx(Loading, {})) : (_jsx(Icon, { name: isExpanded ? "chevron-down" : "chevron-right" })) }) })), _jsx(Checkbox, { id: id, className: cn("size-[16pt]", classes === null || classes === void 0 ? void 0 : classes.checkbox), checked: isChecked, disabled: disabled, onCheckedChange: (newChecked) => onCheckedChange === null || onCheckedChange === void 0 ? void 0 : onCheckedChange(id, newChecked) }), _jsxs("div", { className: cn("ml-2 gap-1 flex flex-1 items-center text-foreground", classes === null || classes === void 0 ? void 0 : classes.item), onClick: handleOnClickItem, children: [showIcon ? customIcon || defaultIcon : null, _jsx("div", { className: cn("flex flex-1 cursor-pointer text-subtitle5 text-ellipsis", classes === null || classes === void 0 ? void 0 : classes.title), children: titleContent })] }), rightIcon] }), isExpanded && hasChildren && (_jsx("div", { className: cn("flex flex-col", classes === null || classes === void 0 ? void 0 : classes.childrenWrapper), style: styles.childPadding, children: children === null || children === void 0 ? void 0 : children.map((child, idx) => (_jsx(TreeItem, Object.assign({ classes: classes, isLastItem: idx === children.length - 1, checkIsExpanded: checkIsExpanded, checkIsChecked: checkIsChecked, checkIsLoading: checkIsLoading, onExpandChange: onExpandChange, onCheckedChange: onCheckedChange, renderIcon: renderIcon, renderElement: renderElement, renderTitle: renderTitle, disabled: disabled, showIcon: showIcon }, child), child.id))) })), enableSeparatorLine && isFirstLevel && !isLastItem && (_jsx("div", { className: cn("bg-grey-150 w-full h-[2px] rounded", classes === null || classes === void 0 ? void 0 : classes.separatorLine) }))] })] }));
|
|
80
|
+
};
|
|
81
|
+
export default TreeItem;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/esm/bundle.css
CHANGED
|
@@ -829,6 +829,9 @@ input[type=number] {
|
|
|
829
829
|
.h-\[24px\]{
|
|
830
830
|
height: 24px;
|
|
831
831
|
}
|
|
832
|
+
.h-\[2px\]{
|
|
833
|
+
height: 2px;
|
|
834
|
+
}
|
|
832
835
|
.h-\[32px\]{
|
|
833
836
|
height: 32px;
|
|
834
837
|
}
|
|
@@ -2717,6 +2720,9 @@ input[type=number] {
|
|
|
2717
2720
|
.fill-error{
|
|
2718
2721
|
fill: color-mix(in srgb, var(--state-color-error-default) calc(100% * 1), transparent);
|
|
2719
2722
|
}
|
|
2723
|
+
.fill-info{
|
|
2724
|
+
fill: color-mix(in srgb, var(--state-color-info-default) calc(100% * 1), transparent);
|
|
2725
|
+
}
|
|
2720
2726
|
.fill-inherit{
|
|
2721
2727
|
fill: inherit;
|
|
2722
2728
|
}
|
|
@@ -2735,6 +2741,9 @@ input[type=number] {
|
|
|
2735
2741
|
.fill-primary-default{
|
|
2736
2742
|
fill: color-mix(in srgb, var(--state-color-primary-default) calc(100% * 1), transparent);
|
|
2737
2743
|
}
|
|
2744
|
+
.fill-secondary{
|
|
2745
|
+
fill: color-mix(in srgb, var(--state-color-secondary-default) calc(100% * 1), transparent);
|
|
2746
|
+
}
|
|
2738
2747
|
.fill-warning{
|
|
2739
2748
|
fill: color-mix(in srgb, var(--state-color-warning-default) calc(100% * 1), transparent);
|
|
2740
2749
|
}
|