@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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/histogram/Histogram.js +89 -0
- package/dist/cjs/histogram/Histogram.module.css +1 -0
- package/dist/cjs/hooks/useFilteredList.js +56 -0
- package/dist/cjs/hooks/usePagination.js +85 -0
- package/dist/cjs/i18n/ar.json.js +56 -0
- package/dist/cjs/i18n/en.json.js +56 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/json/JsonViewer.js +246 -0
- package/dist/cjs/pagination/Pagination.js +100 -0
- package/dist/cjs/table/BodyCell.js +47 -0
- package/dist/cjs/table/HeaderCell.js +79 -0
- package/dist/cjs/table/Table.js +133 -0
- package/dist/cjs/table/useTableColumns.js +26 -0
- package/dist/cjs/table/useVirtualTable.js +56 -0
- package/dist/cjs/tree/TreeNode.js +107 -0
- package/dist/cjs/tree/TreePanel.js +136 -0
- package/dist/cjs/tree/types.js +30 -0
- package/dist/cjs/tree/useTree.js +181 -0
- package/dist/cjs/tree/utils.js +145 -0
- package/dist/cjs/virtual/Gallery.js +229 -0
- package/dist/cjs/virtual/List.js +218 -0
- package/dist/cjs/virtual/Scroller.js +77 -0
- package/dist/esm/histogram/Histogram.js +89 -0
- package/dist/esm/histogram/Histogram.module.css +1 -0
- package/dist/esm/hooks/useFilteredList.js +56 -0
- package/dist/esm/hooks/usePagination.js +85 -0
- package/dist/esm/i18n/ar.json.js +47 -0
- package/dist/esm/i18n/en.json.js +47 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/json/JsonViewer.js +246 -0
- package/dist/esm/pagination/Pagination.js +100 -0
- package/dist/esm/table/BodyCell.js +47 -0
- package/dist/esm/table/HeaderCell.js +79 -0
- package/dist/esm/table/Table.js +133 -0
- package/dist/esm/table/useTableColumns.js +26 -0
- package/dist/esm/table/useVirtualTable.js +56 -0
- package/dist/esm/tree/TreeNode.js +107 -0
- package/dist/esm/tree/TreePanel.js +136 -0
- package/dist/esm/tree/types.js +29 -0
- package/dist/esm/tree/useTree.js +181 -0
- package/dist/esm/tree/utils.js +140 -0
- package/dist/esm/virtual/Gallery.js +229 -0
- package/dist/esm/virtual/List.js +218 -0
- package/dist/esm/virtual/Scroller.js +77 -0
- package/dist/types/histogram/Histogram.d.ts +41 -0
- package/dist/types/hooks/useFilteredList.d.ts +7 -0
- package/dist/types/hooks/usePagination.d.ts +25 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/json/JsonViewer.d.ts +59 -0
- package/dist/types/pagination/Pagination.d.ts +24 -0
- package/dist/types/table/BodyCell.d.ts +5 -0
- package/dist/types/table/HeaderCell.d.ts +2 -0
- package/dist/types/table/Table.d.ts +2 -0
- package/dist/types/table/types.d.ts +26 -0
- package/dist/types/table/useTableColumns.d.ts +8 -0
- package/dist/types/table/useVirtualTable.d.ts +8 -0
- package/dist/types/tree/TreeNode.d.ts +2 -0
- package/dist/types/tree/TreePanel.d.ts +2 -0
- package/dist/types/tree/types.d.ts +138 -0
- package/dist/types/tree/useTree.d.ts +20 -0
- package/dist/types/tree/utils.d.ts +10 -0
- package/dist/types/virtual/Gallery.d.ts +55 -0
- package/dist/types/virtual/List.d.ts +59 -0
- package/dist/types/virtual/Scroller.d.ts +8 -0
- package/package.json +55 -0
- package/src/histogram/Histogram.module.css +57 -0
- package/src/histogram/Histogram.tsx +176 -0
- package/src/hooks/useFilteredList.tsx +78 -0
- package/src/hooks/usePagination.tsx +135 -0
- package/src/i18n/ar.json +36 -0
- package/src/i18n/en.json +36 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +35 -0
- package/src/json/JsonViewer.tsx +381 -0
- package/src/pagination/Pagination.tsx +157 -0
- package/src/table/BodyCell.tsx +54 -0
- package/src/table/HeaderCell.tsx +70 -0
- package/src/table/Table.tsx +143 -0
- package/src/table/types.ts +65 -0
- package/src/table/useTableColumns.ts +52 -0
- package/src/table/useVirtualTable.ts +54 -0
- package/src/tree/TreeNode.tsx +163 -0
- package/src/tree/TreePanel.tsx +157 -0
- package/src/tree/types.ts +181 -0
- package/src/tree/useTree.ts +227 -0
- package/src/tree/utils.ts +185 -0
- package/src/virtual/Gallery.tsx +349 -0
- package/src/virtual/List.tsx +351 -0
- package/src/virtual/Scroller.tsx +81 -0
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
Button,
|
|
26
|
+
ButtonGroup,
|
|
27
|
+
CoreIcons,
|
|
28
|
+
Header,
|
|
29
|
+
Icon,
|
|
30
|
+
Section,
|
|
31
|
+
} from "@react-fabric/core";
|
|
32
|
+
import { Search } from "@react-fabric/form";
|
|
33
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
34
|
+
import { useMemo, useRef } from "react";
|
|
35
|
+
import { TreeNode } from "./TreeNode";
|
|
36
|
+
import { iconCollapseAll, iconExpandAll, type TreePanelProps } from "./types";
|
|
37
|
+
import { useTree } from "./useTree";
|
|
38
|
+
|
|
39
|
+
// TODO: implement keyboard handler for navigating tree
|
|
40
|
+
|
|
41
|
+
export const TreePanel = <T extends KeyValue>({
|
|
42
|
+
items,
|
|
43
|
+
selected,
|
|
44
|
+
checked,
|
|
45
|
+
checkable,
|
|
46
|
+
noLines,
|
|
47
|
+
renderer,
|
|
48
|
+
searchable,
|
|
49
|
+
selectable,
|
|
50
|
+
matcher,
|
|
51
|
+
filterPlaceholder,
|
|
52
|
+
defaultLeafIcon,
|
|
53
|
+
defaultNodeIcon,
|
|
54
|
+
expander = "box",
|
|
55
|
+
onChecked,
|
|
56
|
+
onLoad,
|
|
57
|
+
onSelect,
|
|
58
|
+
...aria
|
|
59
|
+
}: TreePanelProps<T>) => {
|
|
60
|
+
const {
|
|
61
|
+
tree,
|
|
62
|
+
toggleExpand,
|
|
63
|
+
toggleCheck,
|
|
64
|
+
expandAll,
|
|
65
|
+
collapseAll,
|
|
66
|
+
select,
|
|
67
|
+
onFilter,
|
|
68
|
+
} = useTree({
|
|
69
|
+
items,
|
|
70
|
+
checked,
|
|
71
|
+
selected,
|
|
72
|
+
matcher,
|
|
73
|
+
onSelect,
|
|
74
|
+
onChecked,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
78
|
+
const virtualizer = useVirtualizer({
|
|
79
|
+
count: tree.length,
|
|
80
|
+
getScrollElement: () => scrollerRef.current,
|
|
81
|
+
estimateSize: () => 24,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const virtualItems = virtualizer.getVirtualItems();
|
|
85
|
+
|
|
86
|
+
const expanders = useMemo<[string, string]>(() => {
|
|
87
|
+
if (expander === "caret")
|
|
88
|
+
return [CoreIcons.caretRight, CoreIcons.caretDown];
|
|
89
|
+
if (expander === "chevron")
|
|
90
|
+
return [CoreIcons.chevronRight, CoreIcons.chevronDown];
|
|
91
|
+
if (expander === "folder")
|
|
92
|
+
return [CoreIcons.folderClosed, CoreIcons.folderOpen];
|
|
93
|
+
return [CoreIcons.expand, CoreIcons.collapse];
|
|
94
|
+
}, [expander]);
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<Section>
|
|
98
|
+
<Header
|
|
99
|
+
flex
|
|
100
|
+
className="items-center p-px justify-end overflow-hidden border-b"
|
|
101
|
+
>
|
|
102
|
+
{searchable && (
|
|
103
|
+
<Search
|
|
104
|
+
onSearch={onFilter}
|
|
105
|
+
allowClear
|
|
106
|
+
placeholder={filterPlaceholder}
|
|
107
|
+
{...{ noBorder: true }}
|
|
108
|
+
decorateStart={<Icon icon={CoreIcons.filter} className="p-1" />}
|
|
109
|
+
/>
|
|
110
|
+
)}
|
|
111
|
+
<ButtonGroup variant="link" className="m-0">
|
|
112
|
+
<Button
|
|
113
|
+
size="sm"
|
|
114
|
+
aria-label="expand all"
|
|
115
|
+
icon={iconExpandAll}
|
|
116
|
+
onClick={expandAll}
|
|
117
|
+
/>
|
|
118
|
+
<Button
|
|
119
|
+
size="sm"
|
|
120
|
+
aria-label="collapse all"
|
|
121
|
+
icon={iconCollapseAll}
|
|
122
|
+
onClick={collapseAll}
|
|
123
|
+
/>
|
|
124
|
+
</ButtonGroup>
|
|
125
|
+
</Header>
|
|
126
|
+
<div
|
|
127
|
+
ref={scrollerRef}
|
|
128
|
+
role="tree"
|
|
129
|
+
className="area-content overflow-auto p-1"
|
|
130
|
+
{...aria}
|
|
131
|
+
>
|
|
132
|
+
<div style={{ height: virtualizer.getTotalSize() }}>
|
|
133
|
+
<div style={{ height: virtualItems[0]?.start }} />
|
|
134
|
+
{virtualItems.map(({ index, key }) => (
|
|
135
|
+
<div key={key} data-index={index} ref={virtualizer.measureElement}>
|
|
136
|
+
<TreeNode
|
|
137
|
+
node={tree[index]}
|
|
138
|
+
noLines={noLines}
|
|
139
|
+
expanders={expanders}
|
|
140
|
+
checkable={checkable}
|
|
141
|
+
selectable={selectable}
|
|
142
|
+
defaultLeafIcon={defaultLeafIcon}
|
|
143
|
+
defaultNodeIcon={defaultNodeIcon}
|
|
144
|
+
onToggle={toggleExpand}
|
|
145
|
+
onChecked={toggleCheck}
|
|
146
|
+
onSelect={select}
|
|
147
|
+
>
|
|
148
|
+
{(tree[index].data && renderer?.(tree[index].data)) ||
|
|
149
|
+
tree[index].label}
|
|
150
|
+
</TreeNode>
|
|
151
|
+
</div>
|
|
152
|
+
))}
|
|
153
|
+
</div>
|
|
154
|
+
</div>
|
|
155
|
+
</Section>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
@@ -0,0 +1,181 @@
|
|
|
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 BadgeType,
|
|
26
|
+
type ChildProp,
|
|
27
|
+
type CssProp,
|
|
28
|
+
type Elements,
|
|
29
|
+
type IconProps,
|
|
30
|
+
type TestProps,
|
|
31
|
+
} from "@react-fabric/core/dist/types/types";
|
|
32
|
+
|
|
33
|
+
interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
|
|
34
|
+
id: string;
|
|
35
|
+
/**
|
|
36
|
+
* display label
|
|
37
|
+
*/
|
|
38
|
+
label: Elements<JSX.Element>;
|
|
39
|
+
/**
|
|
40
|
+
* full text to be used for querying purposes
|
|
41
|
+
*/
|
|
42
|
+
queryable?: string;
|
|
43
|
+
/**
|
|
44
|
+
* element badge
|
|
45
|
+
*/
|
|
46
|
+
badge?: string | number | BadgeType;
|
|
47
|
+
/**
|
|
48
|
+
* is element `node` or `leaf`
|
|
49
|
+
*/
|
|
50
|
+
leaf?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* disable selection
|
|
53
|
+
*/
|
|
54
|
+
disabled?: boolean;
|
|
55
|
+
data?: T;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type TreeNodeType<T extends KeyValue = KeyValue> = TreeBaseNode<T> &
|
|
59
|
+
(
|
|
60
|
+
| { leaf: true; children?: never }
|
|
61
|
+
| {
|
|
62
|
+
leaf?: false;
|
|
63
|
+
children?: Array<TreeNodeType<T>>;
|
|
64
|
+
open?: boolean;
|
|
65
|
+
iconOpen?: string;
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
export interface InternalNode extends IconProps, CssProp, TestProps {
|
|
70
|
+
id: string;
|
|
71
|
+
label: Elements<JSX.Element>;
|
|
72
|
+
queryable?: string;
|
|
73
|
+
badge?: string | number | BadgeType;
|
|
74
|
+
disabled?: boolean;
|
|
75
|
+
leaf: boolean;
|
|
76
|
+
open: boolean;
|
|
77
|
+
children?: InternalNode[];
|
|
78
|
+
level: number;
|
|
79
|
+
loading: boolean;
|
|
80
|
+
filtered: boolean;
|
|
81
|
+
childFiltered?: boolean;
|
|
82
|
+
parentFiltered?: boolean;
|
|
83
|
+
loaded: boolean;
|
|
84
|
+
parent?: string;
|
|
85
|
+
empty?: boolean;
|
|
86
|
+
errored?: string;
|
|
87
|
+
selected?: true;
|
|
88
|
+
childSelected?: true;
|
|
89
|
+
checked: 0 | 1 | 2;
|
|
90
|
+
data?: AnyObject;
|
|
91
|
+
last?: boolean;
|
|
92
|
+
lines: Array<0 | 1 | 2 | 3>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface TreeNodeProps extends ChildProp {
|
|
96
|
+
node: InternalNode;
|
|
97
|
+
expanders: [string, string];
|
|
98
|
+
defaultNodeIcon?: string;
|
|
99
|
+
defaultLeafIcon?: string;
|
|
100
|
+
noLines?: boolean;
|
|
101
|
+
selectable?: true | "leafOnly";
|
|
102
|
+
checkable?: true | "leafOnly";
|
|
103
|
+
onToggle: (id: string) => void;
|
|
104
|
+
onSelect: (id: string) => void;
|
|
105
|
+
onChecked: (id: string) => void;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface TreePanelProps<T extends KeyValue = KeyValue>
|
|
109
|
+
extends TestProps {
|
|
110
|
+
items?: Array<TreeNodeType<T>>;
|
|
111
|
+
/**
|
|
112
|
+
* make tree searchable
|
|
113
|
+
*/
|
|
114
|
+
searchable?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* filter input placeholder
|
|
117
|
+
*/
|
|
118
|
+
filterPlaceholder?: string;
|
|
119
|
+
/**
|
|
120
|
+
* enable select
|
|
121
|
+
*/
|
|
122
|
+
selectable?: true | "leafOnly";
|
|
123
|
+
/**
|
|
124
|
+
* enable checkable
|
|
125
|
+
*/
|
|
126
|
+
checkable?: true | "leafOnly";
|
|
127
|
+
/**
|
|
128
|
+
* selected item id
|
|
129
|
+
*/
|
|
130
|
+
selected?: string;
|
|
131
|
+
/**
|
|
132
|
+
* checked items
|
|
133
|
+
*/
|
|
134
|
+
checked?: string[];
|
|
135
|
+
/**
|
|
136
|
+
* default node icon
|
|
137
|
+
*/
|
|
138
|
+
defaultNodeIcon?: string;
|
|
139
|
+
/**
|
|
140
|
+
* default leaf icon
|
|
141
|
+
*/
|
|
142
|
+
defaultLeafIcon?: string;
|
|
143
|
+
/**
|
|
144
|
+
* hide level indicator lines
|
|
145
|
+
*/
|
|
146
|
+
noLines?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* expander icons
|
|
149
|
+
*/
|
|
150
|
+
expander?: "box" | "caret" | "chevron" | "folder";
|
|
151
|
+
/**
|
|
152
|
+
* callback for lazy loading tree items
|
|
153
|
+
*/
|
|
154
|
+
onLoad?: (
|
|
155
|
+
id: string,
|
|
156
|
+
) =>
|
|
157
|
+
| Promise<Array<TreeNodeType<T>> | undefined>
|
|
158
|
+
| Array<TreeNodeType<T>>
|
|
159
|
+
| undefined;
|
|
160
|
+
/**
|
|
161
|
+
* callback on selection of tree node
|
|
162
|
+
*/
|
|
163
|
+
onSelect?: (id: string) => void;
|
|
164
|
+
/**
|
|
165
|
+
* callback on change of checked list
|
|
166
|
+
*/
|
|
167
|
+
onChecked?: (leafs: string[], nodes: string[]) => void;
|
|
168
|
+
/**
|
|
169
|
+
* item label renderer
|
|
170
|
+
*/
|
|
171
|
+
renderer?: (data: T) => JSX.Element;
|
|
172
|
+
/**
|
|
173
|
+
* item filter matcher
|
|
174
|
+
*/
|
|
175
|
+
matcher?: (data: T, query: string) => boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const iconExpandAll =
|
|
179
|
+
"M12.984 9v6h3l-3.984 3.984-3.984-3.984h3v-6h-3l3.984-3.984 3.984 3.984h-3zM3.984 2.016h16.031v1.969h-16.031v-1.969zM3.984 20.016h16.031v1.969h-16.031v-1.969z";
|
|
180
|
+
export const iconCollapseAll =
|
|
181
|
+
"M3.984 11.016h16.031v1.969h-16.031v-1.969zM15.984 5.016l-3.984 3.984-3.984-3.984h3v-4.031h1.969v4.031h3zM8.016 18.984l3.984-3.984 3.984 3.984h-3v4.031h-1.969v-4.031h-3z";
|
|
@@ -0,0 +1,227 @@
|
|
|
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 { useDebounce } from "@react-fabric/core";
|
|
25
|
+
import { EMPTY_ARRAY } from "@react-fabric/utilities";
|
|
26
|
+
import memoizeOne from "memoize-one";
|
|
27
|
+
import { useCallback, useEffect, useReducer, useRef } from "react";
|
|
28
|
+
import { type InternalNode, type TreeNodeType } from "./types";
|
|
29
|
+
import {
|
|
30
|
+
filterTree,
|
|
31
|
+
flattenTree,
|
|
32
|
+
makeTreeMap,
|
|
33
|
+
refactorTree,
|
|
34
|
+
updateChecked,
|
|
35
|
+
updateSelection,
|
|
36
|
+
} from "./utils";
|
|
37
|
+
|
|
38
|
+
type TreeActions =
|
|
39
|
+
/**
|
|
40
|
+
* load tree items
|
|
41
|
+
*/
|
|
42
|
+
| { type: "load"; items: TreeNodeType[] }
|
|
43
|
+
/**
|
|
44
|
+
* filter tree nodes
|
|
45
|
+
*/
|
|
46
|
+
| { type: "filter"; query?: string }
|
|
47
|
+
/**
|
|
48
|
+
* update selected item
|
|
49
|
+
*/
|
|
50
|
+
| { type: "select"; id: string }
|
|
51
|
+
/**
|
|
52
|
+
* update item check state
|
|
53
|
+
*/
|
|
54
|
+
| { type: "checked"; id: string[] }
|
|
55
|
+
/**
|
|
56
|
+
* toggle item check, used as action handler
|
|
57
|
+
*/
|
|
58
|
+
| { type: "toggleCheck"; id: string }
|
|
59
|
+
/**
|
|
60
|
+
* expand single item
|
|
61
|
+
*/
|
|
62
|
+
| { type: "expand"; id: string }
|
|
63
|
+
/**
|
|
64
|
+
* collapse single item
|
|
65
|
+
*/
|
|
66
|
+
| { type: "toggleExpand"; id: string }
|
|
67
|
+
/**
|
|
68
|
+
* expand all nodes
|
|
69
|
+
*/
|
|
70
|
+
| { type: "expandAll" }
|
|
71
|
+
/**
|
|
72
|
+
* collapse all nodes
|
|
73
|
+
*/
|
|
74
|
+
| { type: "collapseAll" };
|
|
75
|
+
|
|
76
|
+
interface TreeState {
|
|
77
|
+
items: InternalNode[];
|
|
78
|
+
itemMap: Map<string, InternalNode>;
|
|
79
|
+
tree: InternalNode[];
|
|
80
|
+
selected?: string;
|
|
81
|
+
checked?: string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const createItemList = memoizeOne((items) => items);
|
|
85
|
+
|
|
86
|
+
export const useTree = <T extends KeyValue>({
|
|
87
|
+
items = EMPTY_ARRAY,
|
|
88
|
+
checked = EMPTY_ARRAY,
|
|
89
|
+
selected,
|
|
90
|
+
matcher,
|
|
91
|
+
onSelect,
|
|
92
|
+
onChecked,
|
|
93
|
+
}: {
|
|
94
|
+
items?: Array<TreeNodeType<T>>;
|
|
95
|
+
selected?: string;
|
|
96
|
+
checked?: string[];
|
|
97
|
+
onSelect?: (id: string) => void;
|
|
98
|
+
onChecked?: (leafs: string[], nodes: string[]) => void;
|
|
99
|
+
matcher?: (data: T, query: string) => boolean;
|
|
100
|
+
}) => {
|
|
101
|
+
const componentEvents = useRef({ onSelect, onChecked });
|
|
102
|
+
|
|
103
|
+
const itemList = createItemList(items);
|
|
104
|
+
|
|
105
|
+
const [state, dispatch] = useReducer(
|
|
106
|
+
(state: TreeState, action: TreeActions) => {
|
|
107
|
+
if (action.type === "load") {
|
|
108
|
+
state.items = refactorTree(action.items);
|
|
109
|
+
state.itemMap = makeTreeMap(state.items);
|
|
110
|
+
state.tree = flattenTree(state.items);
|
|
111
|
+
return { ...state };
|
|
112
|
+
}
|
|
113
|
+
if (action.type === "toggleExpand") {
|
|
114
|
+
const node = state.itemMap.get(action.id);
|
|
115
|
+
node && (node.open = !node.open);
|
|
116
|
+
state.tree = flattenTree(state.items);
|
|
117
|
+
return { ...state };
|
|
118
|
+
}
|
|
119
|
+
if (action.type === "expand") {
|
|
120
|
+
// TODO: check if children available, if not call onLoad to fetch node children
|
|
121
|
+
const node = state.itemMap.get(action.id);
|
|
122
|
+
node && (node.open = true);
|
|
123
|
+
state.tree = flattenTree(state.items);
|
|
124
|
+
return { ...state };
|
|
125
|
+
}
|
|
126
|
+
if (action.type === "expandAll") {
|
|
127
|
+
// TODO: prevent opening dynamic nodes that dont have children
|
|
128
|
+
state.itemMap.forEach((node) => {
|
|
129
|
+
!node.leaf && (node.open = true);
|
|
130
|
+
});
|
|
131
|
+
state.tree = flattenTree(state.items);
|
|
132
|
+
return { ...state };
|
|
133
|
+
}
|
|
134
|
+
if (action.type === "collapseAll") {
|
|
135
|
+
state.itemMap.forEach((node) => {
|
|
136
|
+
!node.leaf && (node.open = false);
|
|
137
|
+
});
|
|
138
|
+
state.tree = flattenTree(state.items);
|
|
139
|
+
return { ...state };
|
|
140
|
+
}
|
|
141
|
+
if (action.type === "filter") {
|
|
142
|
+
filterTree(state.itemMap, action.query, matcher);
|
|
143
|
+
state.tree = flattenTree(state.items);
|
|
144
|
+
return { ...state };
|
|
145
|
+
}
|
|
146
|
+
if (action.type === "select") {
|
|
147
|
+
if (state.selected) {
|
|
148
|
+
updateSelection(state.itemMap, state.selected);
|
|
149
|
+
}
|
|
150
|
+
updateSelection(state.itemMap, action.id, true);
|
|
151
|
+
state.tree = flattenTree(state.items);
|
|
152
|
+
componentEvents.current.onSelect?.(action.id);
|
|
153
|
+
return { ...state, selected: action.id };
|
|
154
|
+
}
|
|
155
|
+
if (action.type === "toggleCheck") {
|
|
156
|
+
updateChecked(state.itemMap, action.id);
|
|
157
|
+
const checkList: string[][] = [[], []];
|
|
158
|
+
Array.from(state.itemMap.values())
|
|
159
|
+
.filter((node) => node.checked === 1)
|
|
160
|
+
.forEach((node) => checkList[node.leaf ? 0 : 1].push(node.id));
|
|
161
|
+
componentEvents.current.onChecked?.(checkList[0], checkList[1]);
|
|
162
|
+
return { ...state };
|
|
163
|
+
}
|
|
164
|
+
if (action.type === "checked") {
|
|
165
|
+
action.id.forEach((id) => updateChecked(state.itemMap, id));
|
|
166
|
+
return { ...state, checked: action.id };
|
|
167
|
+
}
|
|
168
|
+
return state;
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
itemMap: new Map(),
|
|
172
|
+
items: [],
|
|
173
|
+
tree: [],
|
|
174
|
+
checked: [],
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
componentEvents.current = { onSelect, onChecked };
|
|
180
|
+
}, [onSelect, onChecked]);
|
|
181
|
+
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
dispatch({ type: "load", items: itemList });
|
|
184
|
+
}, [itemList]);
|
|
185
|
+
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
selected && dispatch({ type: "select", id: selected });
|
|
188
|
+
}, [selected]);
|
|
189
|
+
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
checked && dispatch({ type: "checked", id: checked });
|
|
192
|
+
}, [checked]);
|
|
193
|
+
|
|
194
|
+
const toggleExpand = useCallback((id: string) => {
|
|
195
|
+
dispatch({ type: "toggleExpand", id });
|
|
196
|
+
}, []);
|
|
197
|
+
|
|
198
|
+
const toggleCheck = useCallback((id: string) => {
|
|
199
|
+
dispatch({ type: "toggleCheck", id });
|
|
200
|
+
}, []);
|
|
201
|
+
|
|
202
|
+
const select = useCallback((id: string) => {
|
|
203
|
+
dispatch({ type: "select", id });
|
|
204
|
+
}, []);
|
|
205
|
+
|
|
206
|
+
const expandAll = useCallback(() => {
|
|
207
|
+
dispatch({ type: "expandAll" });
|
|
208
|
+
}, []);
|
|
209
|
+
|
|
210
|
+
const collapseAll = useCallback(() => {
|
|
211
|
+
dispatch({ type: "collapseAll" });
|
|
212
|
+
}, []);
|
|
213
|
+
|
|
214
|
+
const onFilter = useDebounce((query: string) => {
|
|
215
|
+
dispatch({ type: "filter", query });
|
|
216
|
+
}, []);
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
tree: state.tree,
|
|
220
|
+
toggleExpand,
|
|
221
|
+
expandAll,
|
|
222
|
+
collapseAll,
|
|
223
|
+
toggleCheck,
|
|
224
|
+
select,
|
|
225
|
+
onFilter,
|
|
226
|
+
};
|
|
227
|
+
};
|