@rovula/ui 0.0.48 → 0.0.50

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.
Files changed (33) hide show
  1. package/dist/cjs/bundle.css +9 -3
  2. package/dist/cjs/bundle.js +3 -3
  3. package/dist/cjs/bundle.js.map +1 -1
  4. package/dist/cjs/types/components/Tree/Tree.d.ts +4 -45
  5. package/dist/cjs/types/components/Tree/Tree.stories.d.ts +9 -1
  6. package/dist/cjs/types/components/Tree/TreeItem.d.ts +4 -0
  7. package/dist/cjs/types/components/Tree/index.d.ts +4 -0
  8. package/dist/cjs/types/components/Tree/type.d.ts +76 -0
  9. package/dist/cjs/types/index.d.ts +1 -0
  10. package/dist/components/Tree/Tree.js +20 -52
  11. package/dist/components/Tree/Tree.stories.js +2210 -8
  12. package/dist/components/Tree/TreeItem.js +81 -0
  13. package/dist/components/Tree/index.js +4 -0
  14. package/dist/components/Tree/type.js +1 -0
  15. package/dist/esm/bundle.css +9 -3
  16. package/dist/esm/bundle.js +1 -1
  17. package/dist/esm/bundle.js.map +1 -1
  18. package/dist/esm/types/components/Tree/Tree.d.ts +4 -45
  19. package/dist/esm/types/components/Tree/Tree.stories.d.ts +9 -1
  20. package/dist/esm/types/components/Tree/TreeItem.d.ts +4 -0
  21. package/dist/esm/types/components/Tree/index.d.ts +4 -0
  22. package/dist/esm/types/components/Tree/type.d.ts +76 -0
  23. package/dist/esm/types/index.d.ts +1 -0
  24. package/dist/index.d.ts +82 -2
  25. package/dist/index.js +1 -0
  26. package/dist/src/theme/global.css +12 -4
  27. package/package.json +1 -1
  28. package/src/components/Tree/Tree.stories.tsx +2397 -8
  29. package/src/components/Tree/Tree.tsx +52 -188
  30. package/src/components/Tree/TreeItem.tsx +232 -0
  31. package/src/components/Tree/index.ts +5 -0
  32. package/src/components/Tree/type.ts +90 -0
  33. package/src/index.ts +1 -0
@@ -1,45 +1,4 @@
1
- import { FC, ReactNode } from "react";
2
- export type TreeData = {
3
- id: string;
4
- title: string;
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,13 @@
1
1
  import type { Meta, StoryObj } from "@storybook/react";
2
- import { Tree } from "./Tree";
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 Controller: StoryObj<typeof Tree>;
7
+ export declare const onClick: StoryObj<typeof Tree>;
8
+ export declare const CustomIcon: StoryObj<typeof Tree>;
9
+ export declare const renderRightSection: StoryObj<typeof Tree>;
10
+ export declare const ControlShowExpandButton: StoryObj<typeof Tree>;
11
+ export declare const Diabled: StoryObj<typeof Tree>;
12
+ export declare const DiabledEachItem: StoryObj<typeof Tree>;
13
+ export declare const ExpandLoadData: StoryObj<typeof Tree>;
@@ -0,0 +1,4 @@
1
+ import { FC } from "react";
2
+ import { TreeItemProps } from "./type";
3
+ declare const TreeItem: FC<TreeItemProps>;
4
+ export default TreeItem;
@@ -0,0 +1,4 @@
1
+ import Tree from "./Tree";
2
+ import TreeItem from "./TreeItem";
3
+ export * from "./type";
4
+ export { Tree, TreeItem };
@@ -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, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { ActionButton, Checkbox, Icon, cn } from "@/index";
3
- import { useCallback, useMemo, useState, useEffect, } from "react";
4
- const TreeItem = ({ id, title, children, isFirstLevel = false, isLastItem, checkIsExpanded, checkIsChecked, onExpandChange, onCheckedChange, }) => {
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) => {
@@ -54,14 +13,14 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
54
13
  });
55
14
  };
56
15
  useEffect(() => {
57
- if (defaultExpandAll) {
16
+ if (data.length && defaultExpandAll) {
58
17
  const allExpanded = {};
59
18
  traverseTree(data, (node) => {
60
19
  allExpanded[node.id] = true;
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;
@@ -70,24 +29,27 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
70
29
  }
71
30
  }, [data, defaultExpandedId, defaultExpandAll]);
72
31
  useEffect(() => {
73
- if (defaultCheckAll) {
32
+ var _a;
33
+ if (data.length && defaultCheckAll) {
74
34
  const allChecked = {};
75
35
  traverseTree(data, (node) => {
76
36
  allChecked[node.id] = true;
77
37
  });
78
38
  setCheckedState(allChecked);
79
39
  }
80
- else if (!checkedId) {
40
+ else if (((_a = Object.keys(checkedState)) === null || _a === void 0 ? void 0 : _a.length) === 0 &&
41
+ (defaultCheckedId === null || defaultCheckedId === void 0 ? void 0 : defaultCheckedId.length)) {
81
42
  const initialCheckedState = defaultCheckedId.reduce((acc, id) => {
82
43
  acc[id] = true;
83
44
  return acc;
84
45
  }, {});
85
46
  setCheckedState(initialCheckedState);
86
47
  }
87
- }, [data, defaultCheckedId, checkedId, defaultCheckAll]);
48
+ }, [data, defaultCheckedId, defaultCheckAll]);
88
49
  const handleExpandChange = useCallback((id, expanded) => {
50
+ onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange(id, expanded);
89
51
  setExpandedState((prev) => (Object.assign(Object.assign({}, prev), { [id]: expanded })));
90
- }, []);
52
+ }, [onExpandChange]);
91
53
  const handleCheckedChange = useCallback((id, checked) => {
92
54
  let newState = Object.assign(Object.assign({}, checkedState), { [id]: checked });
93
55
  if (hierarchicalCheck) {
@@ -134,5 +96,11 @@ export const Tree = ({ data, defaultExpandedId = [], defaultCheckedId = [], chec
134
96
  }
135
97
  return !!checkedState[id];
136
98
  }, [checkedId, checkedState]);
137
- return (_jsx("div", { className: "w-full", children: data.map((item, idx) => (_jsx(TreeItem, Object.assign({}, item, { isFirstLevel: true, isLastItem: idx === data.length - 1, checkIsExpanded: checkIsExpanded, checkIsChecked: checkIsChecked, onExpandChange: handleExpandChange, onCheckedChange: handleCheckedChange }), item.id))) }));
99
+ const checkIsLoading = useCallback((id) => {
100
+ if (loadingId) {
101
+ return loadingId.includes(id);
102
+ }
103
+ }, [loadingId]);
104
+ 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
105
  };
106
+ export default Tree;