@react-fabric/data 1.0.0-beta.0

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 (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/histogram/Histogram.js +89 -0
  4. package/dist/cjs/histogram/Histogram.module.css +1 -0
  5. package/dist/cjs/hooks/useFilteredList.js +56 -0
  6. package/dist/cjs/hooks/usePagination.js +85 -0
  7. package/dist/cjs/i18n/ar.json.js +56 -0
  8. package/dist/cjs/i18n/en.json.js +56 -0
  9. package/dist/cjs/i18n/index.js +33 -0
  10. package/dist/cjs/index.js +24 -0
  11. package/dist/cjs/json/JsonViewer.js +246 -0
  12. package/dist/cjs/pagination/Pagination.js +100 -0
  13. package/dist/cjs/table/BodyCell.js +47 -0
  14. package/dist/cjs/table/HeaderCell.js +79 -0
  15. package/dist/cjs/table/Table.js +133 -0
  16. package/dist/cjs/table/useTableColumns.js +26 -0
  17. package/dist/cjs/table/useVirtualTable.js +56 -0
  18. package/dist/cjs/tree/TreeNode.js +107 -0
  19. package/dist/cjs/tree/TreePanel.js +136 -0
  20. package/dist/cjs/tree/types.js +30 -0
  21. package/dist/cjs/tree/useTree.js +181 -0
  22. package/dist/cjs/tree/utils.js +145 -0
  23. package/dist/cjs/virtual/Gallery.js +229 -0
  24. package/dist/cjs/virtual/List.js +218 -0
  25. package/dist/cjs/virtual/Scroller.js +77 -0
  26. package/dist/esm/histogram/Histogram.js +89 -0
  27. package/dist/esm/histogram/Histogram.module.css +1 -0
  28. package/dist/esm/hooks/useFilteredList.js +56 -0
  29. package/dist/esm/hooks/usePagination.js +85 -0
  30. package/dist/esm/i18n/ar.json.js +47 -0
  31. package/dist/esm/i18n/en.json.js +47 -0
  32. package/dist/esm/i18n/index.js +33 -0
  33. package/dist/esm/index.js +12 -0
  34. package/dist/esm/json/JsonViewer.js +246 -0
  35. package/dist/esm/pagination/Pagination.js +100 -0
  36. package/dist/esm/table/BodyCell.js +47 -0
  37. package/dist/esm/table/HeaderCell.js +79 -0
  38. package/dist/esm/table/Table.js +133 -0
  39. package/dist/esm/table/useTableColumns.js +26 -0
  40. package/dist/esm/table/useVirtualTable.js +56 -0
  41. package/dist/esm/tree/TreeNode.js +107 -0
  42. package/dist/esm/tree/TreePanel.js +136 -0
  43. package/dist/esm/tree/types.js +29 -0
  44. package/dist/esm/tree/useTree.js +181 -0
  45. package/dist/esm/tree/utils.js +140 -0
  46. package/dist/esm/virtual/Gallery.js +229 -0
  47. package/dist/esm/virtual/List.js +218 -0
  48. package/dist/esm/virtual/Scroller.js +77 -0
  49. package/dist/types/histogram/Histogram.d.ts +41 -0
  50. package/dist/types/hooks/useFilteredList.d.ts +7 -0
  51. package/dist/types/hooks/usePagination.d.ts +25 -0
  52. package/dist/types/i18n/index.d.ts +1 -0
  53. package/dist/types/index.d.ts +10 -0
  54. package/dist/types/json/JsonViewer.d.ts +59 -0
  55. package/dist/types/pagination/Pagination.d.ts +24 -0
  56. package/dist/types/table/BodyCell.d.ts +5 -0
  57. package/dist/types/table/HeaderCell.d.ts +2 -0
  58. package/dist/types/table/Table.d.ts +2 -0
  59. package/dist/types/table/types.d.ts +26 -0
  60. package/dist/types/table/useTableColumns.d.ts +8 -0
  61. package/dist/types/table/useVirtualTable.d.ts +8 -0
  62. package/dist/types/tree/TreeNode.d.ts +2 -0
  63. package/dist/types/tree/TreePanel.d.ts +2 -0
  64. package/dist/types/tree/types.d.ts +138 -0
  65. package/dist/types/tree/useTree.d.ts +20 -0
  66. package/dist/types/tree/utils.d.ts +10 -0
  67. package/dist/types/virtual/Gallery.d.ts +55 -0
  68. package/dist/types/virtual/List.d.ts +59 -0
  69. package/dist/types/virtual/Scroller.d.ts +8 -0
  70. package/package.json +55 -0
  71. package/src/histogram/Histogram.module.css +57 -0
  72. package/src/histogram/Histogram.tsx +176 -0
  73. package/src/hooks/useFilteredList.tsx +78 -0
  74. package/src/hooks/usePagination.tsx +135 -0
  75. package/src/i18n/ar.json +36 -0
  76. package/src/i18n/en.json +36 -0
  77. package/src/i18n/index.ts +28 -0
  78. package/src/index.ts +35 -0
  79. package/src/json/JsonViewer.tsx +381 -0
  80. package/src/pagination/Pagination.tsx +157 -0
  81. package/src/table/BodyCell.tsx +54 -0
  82. package/src/table/HeaderCell.tsx +70 -0
  83. package/src/table/Table.tsx +143 -0
  84. package/src/table/types.ts +65 -0
  85. package/src/table/useTableColumns.ts +52 -0
  86. package/src/table/useVirtualTable.ts +54 -0
  87. package/src/tree/TreeNode.tsx +163 -0
  88. package/src/tree/TreePanel.tsx +157 -0
  89. package/src/tree/types.ts +181 -0
  90. package/src/tree/useTree.ts +227 -0
  91. package/src/tree/utils.ts +185 -0
  92. package/src/virtual/Gallery.tsx +349 -0
  93. package/src/virtual/List.tsx +351 -0
  94. package/src/virtual/Scroller.tsx +81 -0
@@ -0,0 +1,70 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
25
+ import { type ColumnType } from "./types";
26
+
27
+ export const HeaderCell = ({
28
+ label,
29
+ icon,
30
+ iconBg,
31
+ iconColor,
32
+ rtlFlip,
33
+ actions,
34
+ locked,
35
+ resizable,
36
+ width,
37
+ maxWidth,
38
+ minWidth,
39
+ }: ColumnType) => {
40
+ return (
41
+ <div
42
+ className="group font-medium border-e flex flex-nowrap text-start items-center"
43
+ style={{
44
+ width: width ?? "12rem",
45
+ minWidth: minWidth ?? "2rem",
46
+ maxWidth: maxWidth ?? "32rem",
47
+ }}
48
+ >
49
+ <div className="flex-1 px-2 py-1 text-sm truncate sticky start-0">
50
+ {icon && (
51
+ <Icon icon={icon} bg={iconBg} color={iconColor} rtlFlip={rtlFlip} />
52
+ )}
53
+ <span>{label ?? " "}</span>
54
+ </div>
55
+ <Dropdown placement="bottom-start">
56
+ <Icon
57
+ icon={CoreIcons.caretDown}
58
+ className="outline bg-base me-0.5 flex-content hidden group-hover:inline-block data-[dropdown-open]:inline-block"
59
+ />
60
+ <Menu className="text-sm">
61
+ <MenuItem label="Sort ascending" icon={CoreIcons.sortAsc} />
62
+ <MenuItem label="Sort descending" icon={CoreIcons.sortDesc} />
63
+ </Menu>
64
+ </Dropdown>
65
+ {resizable && (
66
+ <div className="cursor-col-resize border-e bg-transparent border-tint-200 w-[5px] self-stretch" />
67
+ )}
68
+ </div>
69
+ );
70
+ };
@@ -0,0 +1,143 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { useCallback, useMemo } from "react";
25
+ import { BodyCell } from "./BodyCell";
26
+ import { HeaderCell } from "./HeaderCell";
27
+ import { type ColumnType, type TableProps } from "./types";
28
+ import { useTableColumns } from "./useTableColumns";
29
+ import { useVirtualTable } from "./useVirtualTable";
30
+ import classNames from "classnames";
31
+
32
+ export const Table = <T extends KeyValue = KeyValue>({
33
+ data,
34
+ columns,
35
+ }: TableProps<T>) => {
36
+ const { columnMap } = useTableColumns(columns);
37
+ const { scrollerRef, items, getData, totalSize, measureElement } =
38
+ useVirtualTable(data);
39
+
40
+ const wrapperStart = useMemo(
41
+ () => "bg-inherit sticky start-0 flex flex-nowrap z-1",
42
+ [],
43
+ );
44
+ const wrapperEnd = useMemo(
45
+ () => "bg-inherit sticky end-0 flex flex-nowrap z-1 border-s -ms-px",
46
+ [],
47
+ );
48
+ const emptyCol = useCallback(
49
+ (col: ColumnType, idc: number) => (
50
+ <div
51
+ key={`blank:${idc}`}
52
+ className="overflow-hidden border-e"
53
+ style={{
54
+ width: col.width ?? "12rem",
55
+ minWidth: col.minWidth ?? "2rem",
56
+ maxWidth: col.maxWidth ?? "32rem",
57
+ }}
58
+ />
59
+ ),
60
+ [],
61
+ );
62
+
63
+ return (
64
+ <div
65
+ className="grid area-content overflow-auto relative"
66
+ style={{
67
+ gridTemplate: `"head" auto "content" 1fr "foot" auto/1fr`,
68
+ }}
69
+ ref={scrollerRef}
70
+ >
71
+ <div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
72
+ <div className={wrapperStart}>
73
+ {columnMap.start?.map((col, idx) => (
74
+ <HeaderCell key={idx} {...col} />
75
+ ))}
76
+ </div>
77
+ {columnMap.cols?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
78
+ <div className={wrapperEnd}>
79
+ {columnMap.end?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
80
+ </div>
81
+ </div>
82
+ <div
83
+ className="area-content flex flex-col flex-nowrap"
84
+ style={{ height: totalSize() }}
85
+ >
86
+ <div style={{ height: items[0]?.start }} />
87
+ {items.map(({ index, key }) => (
88
+ <div
89
+ key={key}
90
+ className={classNames(
91
+ "flex flex-nowrap",
92
+ index % 2 ? "bg-even" : "bg-odd",
93
+ )}
94
+ data-index={index}
95
+ ref={measureElement}
96
+ >
97
+ <div className={wrapperStart}>
98
+ {columnMap.start?.map((col, ids) => (
99
+ <BodyCell
100
+ key={`${key}:${ids}`}
101
+ column={col}
102
+ item={getData(index)}
103
+ />
104
+ ))}
105
+ </div>
106
+ {columnMap.cols?.map((col, idc) => (
107
+ <BodyCell
108
+ key={`${key}:${idc}`}
109
+ column={col}
110
+ item={getData(index)}
111
+ />
112
+ ))}
113
+ <div className={wrapperEnd}>
114
+ {columnMap.end?.map((col, ide) => (
115
+ <BodyCell
116
+ key={`${key}:${ide}`}
117
+ column={col}
118
+ item={getData(index)}
119
+ />
120
+ ))}
121
+ </div>
122
+ </div>
123
+ ))}
124
+ <div className="flex-1 flex flex-nowrap">
125
+ <div className={wrapperStart}>{columnMap.start?.map(emptyCol)}</div>
126
+ {columnMap.cols?.map(emptyCol)}
127
+ <div className={wrapperEnd}>{columnMap.end?.map(emptyCol)}</div>
128
+ </div>
129
+ </div>
130
+ <div className="area-foot bg-dimmed border-t sticky bottom-0 flex flex-nowrap z-2">
131
+ <div className={wrapperStart}>
132
+ {columnMap.start?.map((col, idx) => (
133
+ <HeaderCell key={idx} {...col} />
134
+ ))}
135
+ </div>
136
+ {columnMap.cols?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
137
+ <div className={wrapperEnd}>
138
+ {columnMap.end?.map((col, idx) => <HeaderCell key={idx} {...col} />)}
139
+ </div>
140
+ </div>
141
+ </div>
142
+ );
143
+ };
@@ -0,0 +1,65 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ type CssProp,
26
+ type IconProps,
27
+ type TestProps,
28
+ } from "@react-fabric/core/dist/types/types";
29
+ import { type ReactElement } from "react";
30
+
31
+ export interface ColumnType<T = KeyValue>
32
+ extends CssProp,
33
+ IconProps,
34
+ TestProps {
35
+ id: keyof T;
36
+ label?: string;
37
+ actions?: ReactElement;
38
+ align?: "start" | "center" | "end";
39
+ locked?: "start" | "end";
40
+ hidden?: boolean;
41
+ hideable?: boolean;
42
+ resizable?: boolean;
43
+ sortable?: boolean;
44
+ filterable?: boolean;
45
+ filterList?: AnyObject[];
46
+ width?: string | number;
47
+ minWidth?: string | number;
48
+ maxWidth?: string | number;
49
+ dataType?:
50
+ | "string"
51
+ | "number"
52
+ | "boolean"
53
+ | "datetime"
54
+ | "date"
55
+ | "time"
56
+ | "unknown";
57
+ format?: string;
58
+ renderer?: (value: AnyObject, data: T) => void;
59
+ }
60
+
61
+ export interface TableProps<T = KeyValue> {
62
+ columns: Array<ColumnType<T>>;
63
+ data: T[];
64
+ autoColumns?: boolean;
65
+ }
@@ -0,0 +1,52 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { groupBy } from "@react-fabric/utilities";
25
+ import { useEffect, useState } from "react";
26
+ import { type ColumnType } from "./types";
27
+
28
+ export const useTableColumns = (columns: ColumnType[]) => {
29
+ const [columnMap, setColumnMap] = useState<{
30
+ start: ColumnType[];
31
+ end: ColumnType[];
32
+ cols: ColumnType[];
33
+ }>({
34
+ start: [],
35
+ end: [],
36
+ cols: [],
37
+ });
38
+
39
+ useEffect(() => {
40
+ setColumnMap(
41
+ groupBy(
42
+ columns.filter((col) => !(col.hideable && col.hidden)),
43
+ "locked",
44
+ "cols",
45
+ ) as AnyObject,
46
+ );
47
+ }, [columns]);
48
+
49
+ return {
50
+ columnMap,
51
+ };
52
+ };
@@ -0,0 +1,54 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { useVirtualizer } from "@tanstack/react-virtual";
25
+ import { useCallback, useRef } from "react";
26
+
27
+ export const useVirtualTable = (data: KeyValue[]) => {
28
+ // TODO: refactor data into item list, apply grouping if groupColumn provided
29
+ // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
30
+
31
+ const scrollerRef = useRef<HTMLDivElement>(null);
32
+ const virtualizer = useVirtualizer({
33
+ count: data.length,
34
+ getScrollElement: () => scrollerRef.current,
35
+ estimateSize: () => 24,
36
+ });
37
+
38
+ const virtualItems = virtualizer.getVirtualItems();
39
+
40
+ const getData = useCallback(
41
+ (index: number) => {
42
+ return data[index];
43
+ },
44
+ [data],
45
+ );
46
+
47
+ return {
48
+ scrollerRef,
49
+ items: virtualItems,
50
+ getData,
51
+ totalSize: virtualizer.getTotalSize,
52
+ measureElement: virtualizer.measureElement,
53
+ };
54
+ };
@@ -0,0 +1,163 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { Badge, CoreIcons, getBadgeProps, Icon } from "@react-fabric/core";
25
+ import classNames from "classnames";
26
+ import { useMemo } from "react";
27
+ import { type TreeNodeProps } from "./types";
28
+
29
+ export const TreeNode = ({
30
+ node,
31
+ noLines,
32
+ expanders,
33
+ checkable,
34
+ selectable,
35
+ defaultLeafIcon,
36
+ defaultNodeIcon,
37
+ onToggle,
38
+ onSelect,
39
+ onChecked,
40
+ children,
41
+ }: TreeNodeProps) => {
42
+ const badgeProps = useMemo(() => {
43
+ return getBadgeProps(node.badge);
44
+ }, [node.badge]);
45
+
46
+ const filler = useMemo(() => {
47
+ return node.lines.slice(noLines ? 1 : 0).map((line, idx) => (
48
+ <svg
49
+ key={idx}
50
+ viewBox="0 0 24 24"
51
+ className="flex-initial w-6 fill-tint-200"
52
+ >
53
+ {line === 0 && <path d="" />}
54
+ {!noLines && line === 1 && <path d="M7.45 26.1v-28.2h1.1v28.2h-1.1z" />}
55
+ {!noLines && line === 2 && (
56
+ <path d="M14.989 12.389l0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v14.1l0.161 0.389 0.389 0.161h6.6z" />
57
+ )}
58
+ {!noLines && line === 3 && (
59
+ <path d="M8.55 12.55h6.050l0.389-0.161 0.161-0.389-0.161-0.389-0.389-0.161h-6.050v-13.55h-1.1v28.2h1.1z" />
60
+ )}
61
+ </svg>
62
+ ));
63
+ }, [noLines, node.lines]);
64
+
65
+ const nodeIcon = useMemo(() => {
66
+ if (node.icon) return node.icon;
67
+ return node.leaf ? defaultLeafIcon : defaultNodeIcon;
68
+ }, [defaultLeafIcon, defaultNodeIcon, node.icon]);
69
+
70
+ const canCheck = useMemo(() => {
71
+ return checkable === true || (checkable === "leafOnly" && node.leaf);
72
+ }, [node.leaf, checkable]);
73
+
74
+ const canSelect = useMemo(() => {
75
+ return (
76
+ !node.disabled &&
77
+ (selectable === true || (selectable === "leafOnly" && node.leaf))
78
+ );
79
+ }, [node.leaf, node.disabled, selectable]);
80
+
81
+ return (
82
+ <div
83
+ role="treeitem"
84
+ aria-selected={node.selected}
85
+ className="flex flex-nowrap"
86
+ data-testid={node["data-testid"]}
87
+ data-test-value={node["data-test-value"]}
88
+ >
89
+ {filler}
90
+ {!node.leaf && !node.childFiltered && (
91
+ <div className="flex-initial w-6">
92
+ <Icon
93
+ rtlFlip
94
+ onClick={() => !node.leaf && onToggle(node.id)}
95
+ icon={node.open ? expanders[1] : expanders[0]}
96
+ className="text-tint-600 hover:text-primary-600"
97
+ />
98
+ </div>
99
+ )}
100
+ {canCheck && (
101
+ <div className="flex-initial w-6">
102
+ <Icon
103
+ onClick={() => !node.disabled && onChecked(node.id)}
104
+ icon={
105
+ node.checked === 2
106
+ ? CoreIcons.checkboxInt
107
+ : node.checked === 1
108
+ ? CoreIcons.checkboxOn
109
+ : CoreIcons.checkboxOff
110
+ }
111
+ className={classNames(
112
+ node.disabled
113
+ ? "text-muted"
114
+ : "text-primary-500 hover:text-primary-600",
115
+ )}
116
+ />
117
+ </div>
118
+ )}
119
+ <div
120
+ role="none"
121
+ className={classNames(
122
+ "group data-[selected]:bg-primary-100 flex flex-nowrap flex-1 items-center overflow-hidden select-none",
123
+ node.childSelected && "font-medium",
124
+ canSelect && "hover:bg-primary-50 cursor-pointer",
125
+ )}
126
+ data-selected={node.selected}
127
+ onClick={() =>
128
+ canSelect
129
+ ? onSelect(node.id)
130
+ : canCheck
131
+ ? onChecked(node.id)
132
+ : onToggle(node.id)
133
+ }
134
+ >
135
+ {nodeIcon && (
136
+ <div className="flex-content w-6">
137
+ <Icon
138
+ icon={nodeIcon}
139
+ bg={node.iconBg}
140
+ color={node.iconColor}
141
+ rtlFlip={node.rtlFlip}
142
+ />
143
+ </div>
144
+ )}
145
+ {node.childSelected && !node.open && (
146
+ <span className=" flex-content text-primary-500">•</span>
147
+ )}
148
+ <div role="none" className="flex-1 truncate">
149
+ {children}
150
+ </div>
151
+ {node.badge && (
152
+ <Badge
153
+ {...badgeProps}
154
+ className={classNames(
155
+ "me-1 self-center flex-content bg-opacity-50",
156
+ badgeProps.className,
157
+ )}
158
+ />
159
+ )}
160
+ </div>
161
+ </div>
162
+ );
163
+ };