@react-fabric/data 1.0.0-beta.3 → 1.0.0-beta.30
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/checklist/CheckList.js +143 -0
- package/dist/cjs/histogram/Histogram.js +16 -5
- package/dist/cjs/hooks/usePagination.js +7 -4
- package/dist/cjs/hooks/useSelectableList.js +54 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/table/BodyCell.js +38 -5
- package/dist/cjs/table/BodyRow.js +107 -0
- package/dist/cjs/table/CheckboxCell.js +1 -1
- package/dist/cjs/table/HeaderCell.js +29 -36
- package/dist/cjs/table/Table.js +104 -66
- package/dist/cjs/table/useResizer.js +4 -4
- package/dist/cjs/table/useVirtualTable.js +118 -17
- package/dist/cjs/tree/TreeNode.js +44 -24
- package/dist/cjs/tree/TreePanel.js +28 -10
- package/dist/cjs/tree/useTree.js +96 -14
- package/dist/cjs/tree/utils.js +20 -5
- package/dist/cjs/virtual/Gallery.js +41 -27
- package/dist/cjs/virtual/List.js +41 -29
- package/dist/esm/checklist/CheckList.js +143 -0
- package/dist/esm/histogram/Histogram.js +16 -5
- package/dist/esm/hooks/usePagination.js +7 -4
- package/dist/esm/hooks/useSelectableList.js +54 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/table/BodyCell.js +38 -5
- package/dist/esm/table/BodyRow.js +107 -0
- package/dist/esm/table/CheckboxCell.js +1 -1
- package/dist/esm/table/HeaderCell.js +30 -37
- package/dist/esm/table/Table.js +106 -68
- package/dist/esm/table/useResizer.js +4 -4
- package/dist/esm/table/useVirtualTable.js +120 -19
- package/dist/esm/tree/TreeNode.js +46 -26
- package/dist/esm/tree/TreePanel.js +28 -10
- package/dist/esm/tree/useTree.js +98 -16
- package/dist/esm/tree/utils.js +20 -5
- package/dist/esm/virtual/Gallery.js +41 -27
- package/dist/esm/virtual/List.js +41 -29
- package/dist/types/checklist/CheckList.d.ts +30 -0
- package/dist/types/histogram/Histogram.d.ts +3 -3
- package/dist/types/hooks/useSelectableList.d.ts +28 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/table/AddColumn.d.ts +2 -2
- package/dist/types/table/BodyCell.d.ts +4 -3
- package/dist/types/table/BodyRow.d.ts +20 -0
- package/dist/types/table/EmptyCell.d.ts +2 -2
- package/dist/types/table/HeaderCell.d.ts +2 -2
- package/dist/types/table/Table.d.ts +1 -1
- package/dist/types/table/types.d.ts +19 -8
- package/dist/types/table/useTableColumns.d.ts +6 -6
- package/dist/types/table/useVirtualTable.d.ts +20 -5
- package/dist/types/tree/TreeNode.d.ts +1 -1
- package/dist/types/tree/TreePanel.d.ts +1 -1
- package/dist/types/tree/types.d.ts +25 -5
- package/dist/types/tree/useTree.d.ts +2 -9
- package/dist/types/tree/utils.d.ts +4 -1
- package/dist/types/virtual/Gallery.d.ts +11 -3
- package/dist/types/virtual/List.d.ts +7 -3
- package/package.json +8 -7
- package/src/checklist/CheckList.tsx +222 -0
- package/src/histogram/Histogram.tsx +9 -2
- package/src/hooks/usePagination.tsx +13 -7
- package/src/hooks/useSelectableList.ts +94 -0
- package/src/index.ts +2 -1
- package/src/table/AddColumn.tsx +2 -2
- package/src/table/BodyCell.tsx +34 -5
- package/src/table/BodyRow.tsx +143 -0
- package/src/table/CheckboxCell.tsx +1 -1
- package/src/table/EmptyCell.tsx +2 -2
- package/src/table/HeaderCell.tsx +27 -13
- package/src/table/Table.tsx +141 -73
- package/src/table/types.ts +22 -7
- package/src/table/useResizer.ts +5 -3
- package/src/table/useTableColumns.ts +7 -7
- package/src/table/useVirtualTable.ts +142 -27
- package/src/tree/TreeNode.tsx +90 -56
- package/src/tree/TreePanel.tsx +23 -3
- package/src/tree/types.ts +32 -4
- package/src/tree/useTree.ts +102 -23
- package/src/tree/utils.ts +23 -8
- package/src/virtual/Gallery.tsx +43 -32
- package/src/virtual/List.tsx +38 -25
|
@@ -21,78 +21,193 @@
|
|
|
21
21
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
24
|
+
import { compareValues, groupBy } from "@react-fabric/utilities";
|
|
25
|
+
import {
|
|
26
|
+
defaultRangeExtractor,
|
|
27
|
+
type Range,
|
|
28
|
+
useVirtualizer,
|
|
29
|
+
} from "@tanstack/react-virtual";
|
|
30
|
+
import {
|
|
31
|
+
useCallback,
|
|
32
|
+
useEffect,
|
|
33
|
+
useMemo,
|
|
34
|
+
useReducer,
|
|
35
|
+
useRef,
|
|
36
|
+
useState,
|
|
37
|
+
} from "react";
|
|
26
38
|
|
|
27
|
-
interface DataState {
|
|
28
|
-
|
|
39
|
+
interface DataState<T = KeyValue> {
|
|
40
|
+
items: T[];
|
|
41
|
+
grouped: KeyValue<T[]>;
|
|
42
|
+
groups: AnyObject[];
|
|
43
|
+
expanded: AnyObject[];
|
|
44
|
+
checked: AnyObject[];
|
|
29
45
|
allChecked: 0 | 1 | 2;
|
|
30
46
|
}
|
|
31
47
|
|
|
32
|
-
type DataAction =
|
|
48
|
+
type DataAction =
|
|
49
|
+
| { type: "updateState"; checked: AnyObject[] }
|
|
50
|
+
| { type: "groupExpand"; key: AnyObject }
|
|
51
|
+
| { type: "grouped"; key: AnyObject }
|
|
52
|
+
| { type: "expand"; key: AnyObject }
|
|
53
|
+
| { type: "check"; key: AnyObject }
|
|
54
|
+
| { type: "checkall" };
|
|
33
55
|
|
|
34
|
-
export const useVirtualTable = (
|
|
35
|
-
data:
|
|
36
|
-
|
|
37
|
-
|
|
56
|
+
export const useVirtualTable = <T extends KeyValue = KeyValue>(
|
|
57
|
+
data: T[],
|
|
58
|
+
options: {
|
|
59
|
+
checked?: AnyObject[];
|
|
60
|
+
keyProperty: keyof T;
|
|
61
|
+
groupProperty?: keyof T;
|
|
62
|
+
onCheckChanged?: (checked: AnyObject[]) => void;
|
|
63
|
+
},
|
|
38
64
|
) => {
|
|
39
65
|
// TODO: refactor data into item list, apply grouping if groupColumn provided
|
|
40
66
|
// TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
|
|
41
67
|
|
|
42
|
-
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
43
|
-
const virtualizer = useVirtualizer({
|
|
44
|
-
count: data.length,
|
|
45
|
-
getScrollElement: () => scrollerRef.current,
|
|
46
|
-
estimateSize: () => 24,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
68
|
const [state, dispatch] = useReducer(
|
|
50
|
-
(state: DataState
|
|
69
|
+
(state: DataState<T>, action: DataAction) => {
|
|
70
|
+
if (action.type === "updateState") {
|
|
71
|
+
state.checked = action.checked;
|
|
72
|
+
}
|
|
73
|
+
if (action.type === "expand") {
|
|
74
|
+
if (state.expanded.includes(action.key))
|
|
75
|
+
state.expanded.splice(state.expanded.indexOf(action.key), 1);
|
|
76
|
+
else state.expanded.push(action.key);
|
|
77
|
+
}
|
|
51
78
|
if (action.type === "check") {
|
|
52
79
|
if (state.checked.includes(action.key))
|
|
53
80
|
state.checked.splice(state.checked.indexOf(action.key), 1);
|
|
54
81
|
else state.checked.push(action.key);
|
|
82
|
+
options.onCheckChanged?.(state.checked);
|
|
55
83
|
}
|
|
56
84
|
if (action.type === "checkall") {
|
|
57
85
|
if (state.allChecked !== 0) {
|
|
58
86
|
state.checked = [];
|
|
87
|
+
} else if (options.keyProperty) {
|
|
88
|
+
state.checked = data.map((item) => item[options.keyProperty]);
|
|
89
|
+
}
|
|
90
|
+
options.onCheckChanged?.(state.checked);
|
|
91
|
+
}
|
|
92
|
+
const checkList = data.filter((hit) =>
|
|
93
|
+
state.checked.includes(hit[options.keyProperty]),
|
|
94
|
+
);
|
|
95
|
+
if (action.type === "groupExpand") {
|
|
96
|
+
const group = state.groups.find((group) => group.key === action.key);
|
|
97
|
+
if (group) {
|
|
98
|
+
group.open = !group.open;
|
|
99
|
+
state.groups = state.groups.slice();
|
|
100
|
+
state.items = state.groups
|
|
101
|
+
.map((group) => [
|
|
102
|
+
group,
|
|
103
|
+
group.open ? state.grouped[group.key] : undefined,
|
|
104
|
+
])
|
|
105
|
+
.flat(2)
|
|
106
|
+
.filter(Boolean) as any;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (action.type === "grouped") {
|
|
110
|
+
if (action.key) {
|
|
111
|
+
state.grouped = groupBy<T>(data, action.key);
|
|
112
|
+
state.groups = Object.keys(state.grouped)
|
|
113
|
+
.sort(compareValues("asc"))
|
|
114
|
+
.map((group) => ({
|
|
115
|
+
__GROUP__: true,
|
|
116
|
+
open: true,
|
|
117
|
+
key: group,
|
|
118
|
+
items: state.grouped[group],
|
|
119
|
+
itemCount: state.grouped[group].length,
|
|
120
|
+
}));
|
|
121
|
+
state.items = state.groups
|
|
122
|
+
.map((group) => [group, ...state.grouped[group.key]])
|
|
123
|
+
.flat() as any;
|
|
59
124
|
} else {
|
|
60
|
-
state.
|
|
125
|
+
state.items = data;
|
|
61
126
|
}
|
|
62
127
|
}
|
|
63
|
-
onCheckChanged(state.checked);
|
|
64
128
|
return {
|
|
65
129
|
...state,
|
|
66
130
|
allChecked:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
: state.checked.length
|
|
70
|
-
? 2
|
|
71
|
-
: 0,
|
|
72
|
-
} as DataState;
|
|
131
|
+
checkList.length === data.length ? 1 : state.checked.length ? 2 : 0,
|
|
132
|
+
} as DataState<T>;
|
|
73
133
|
},
|
|
74
134
|
{
|
|
135
|
+
items: data,
|
|
136
|
+
grouped: {},
|
|
137
|
+
groups: [],
|
|
75
138
|
checked: [],
|
|
139
|
+
expanded: [],
|
|
76
140
|
allChecked: 0,
|
|
77
141
|
},
|
|
78
142
|
);
|
|
79
143
|
|
|
144
|
+
const activeStickyIndexRef = useRef(0);
|
|
145
|
+
const stickyIndexes = useMemo<number[]>(
|
|
146
|
+
() =>
|
|
147
|
+
state.items
|
|
148
|
+
.map((hit, idx) => ("__GROUP__" in hit ? idx : undefined))
|
|
149
|
+
.filter(Boolean) as number[],
|
|
150
|
+
[state.items],
|
|
151
|
+
);
|
|
152
|
+
const isActiveSticky = (index: number) =>
|
|
153
|
+
activeStickyIndexRef.current === index;
|
|
154
|
+
|
|
155
|
+
const [hasActiveSticky, setHasActiveSticky] = useState(false);
|
|
156
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
157
|
+
const virtualizer = useVirtualizer({
|
|
158
|
+
count: state.items.length,
|
|
159
|
+
getScrollElement: () => scrollerRef.current,
|
|
160
|
+
estimateSize: () => 32,
|
|
161
|
+
rangeExtractor: useCallback(
|
|
162
|
+
(range: Range) => {
|
|
163
|
+
activeStickyIndexRef.current =
|
|
164
|
+
[...stickyIndexes]
|
|
165
|
+
.reverse()
|
|
166
|
+
.find((index) => range.startIndex > index) ?? 0;
|
|
167
|
+
|
|
168
|
+
setHasActiveSticky(range.startIndex !== activeStickyIndexRef.current);
|
|
169
|
+
|
|
170
|
+
const next = new Set([
|
|
171
|
+
activeStickyIndexRef.current,
|
|
172
|
+
...defaultRangeExtractor(range),
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
return [...next];
|
|
176
|
+
},
|
|
177
|
+
[stickyIndexes],
|
|
178
|
+
),
|
|
179
|
+
});
|
|
180
|
+
|
|
80
181
|
const virtualItems = virtualizer.getVirtualItems();
|
|
81
182
|
|
|
82
183
|
const getData = useCallback(
|
|
83
184
|
(index: number) => {
|
|
84
|
-
return
|
|
185
|
+
return state.items[index];
|
|
85
186
|
},
|
|
86
|
-
[
|
|
187
|
+
[state.items],
|
|
87
188
|
);
|
|
88
189
|
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
dispatch({ type: "grouped", key: options.groupProperty });
|
|
192
|
+
}, [options.groupProperty]);
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
dispatch({ type: "updateState", checked: options.checked ?? [] });
|
|
196
|
+
}, [options.checked]);
|
|
197
|
+
|
|
89
198
|
return {
|
|
90
199
|
scrollerRef,
|
|
91
200
|
items: virtualItems,
|
|
92
201
|
checkState: state,
|
|
93
202
|
getData,
|
|
203
|
+
virtualizer,
|
|
204
|
+
hasActiveSticky,
|
|
205
|
+
isActiveSticky,
|
|
206
|
+
top: virtualizer.range?.startIndex,
|
|
94
207
|
totalSize: virtualizer.getTotalSize,
|
|
95
208
|
measureElement: virtualizer.measureElement,
|
|
209
|
+
toggleGroupExpand: (key: Key) => dispatch({ type: "groupExpand", key }),
|
|
210
|
+
toggleExpand: (key: Key) => dispatch({ type: "expand", key }),
|
|
96
211
|
toggleChecked: (key: Key) => dispatch({ type: "check", key }),
|
|
97
212
|
toggleAllChecked: () => dispatch({ type: "checkall" }),
|
|
98
213
|
};
|
package/src/tree/TreeNode.tsx
CHANGED
|
@@ -22,8 +22,10 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { Badge, CoreIcons, getBadgeProps, Icon } from "@react-fabric/core";
|
|
25
|
+
import { isEmpty, isString } from "@react-fabric/utilities";
|
|
25
26
|
import classNames from "classnames";
|
|
26
|
-
import { useMemo } from "react";
|
|
27
|
+
import { isValidElement, useMemo } from "react";
|
|
28
|
+
import { useTranslation } from "react-i18next";
|
|
27
29
|
import { type TreeNodeProps } from "./types";
|
|
28
30
|
|
|
29
31
|
export const TreeNode = ({
|
|
@@ -35,30 +37,43 @@ export const TreeNode = ({
|
|
|
35
37
|
defaultLeafIcon,
|
|
36
38
|
defaultNodeIcon,
|
|
37
39
|
onToggle,
|
|
40
|
+
onClick,
|
|
38
41
|
onSelect,
|
|
39
42
|
onChecked,
|
|
40
43
|
children,
|
|
44
|
+
leafClassName,
|
|
45
|
+
nodeClassName,
|
|
41
46
|
}: TreeNodeProps) => {
|
|
47
|
+
const { t } = useTranslation("data");
|
|
42
48
|
const badgeProps = useMemo(() => {
|
|
43
49
|
return getBadgeProps(node.badge);
|
|
44
50
|
}, [node.badge]);
|
|
45
51
|
|
|
46
52
|
const filler = useMemo(() => {
|
|
47
|
-
return node.lines.slice(
|
|
48
|
-
<
|
|
53
|
+
return node.lines.slice(1).map((line, idx) => (
|
|
54
|
+
<div
|
|
49
55
|
key={idx}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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" />
|
|
56
|
+
className={classNames(
|
|
57
|
+
"flex-initial self-stretch flex items-center w-6 relative",
|
|
58
|
+
!noLines &&
|
|
59
|
+
line > 0 &&
|
|
60
|
+
"before:absolute before:top-0 before:start-[7px] before:bg-tint-200 before:min-h-[50%] before:w-[2px] before:scale-x-[.625]",
|
|
61
|
+
!noLines && line !== 2 && "before:h-full",
|
|
60
62
|
)}
|
|
61
|
-
|
|
63
|
+
>
|
|
64
|
+
<svg viewBox="0 0 24 24" className="fill-tint-200 rtl:-scale-x-100">
|
|
65
|
+
{line === 0 && <path d="" />}
|
|
66
|
+
{!noLines && line === 1 && (
|
|
67
|
+
<path d="M7.45 26.1v-28.2h1.1v28.2h-1.1z" />
|
|
68
|
+
)}
|
|
69
|
+
{!noLines && line === 2 && (
|
|
70
|
+
<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" />
|
|
71
|
+
)}
|
|
72
|
+
{!noLines && line === 3 && (
|
|
73
|
+
<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" />
|
|
74
|
+
)}
|
|
75
|
+
</svg>
|
|
76
|
+
</div>
|
|
62
77
|
));
|
|
63
78
|
}, [noLines, node.lines]);
|
|
64
79
|
|
|
@@ -82,12 +97,20 @@ export const TreeNode = ({
|
|
|
82
97
|
<div
|
|
83
98
|
role="treeitem"
|
|
84
99
|
aria-selected={node.selected}
|
|
85
|
-
className=
|
|
100
|
+
className={classNames(
|
|
101
|
+
"flex flex-nowrap items-center",
|
|
102
|
+
node.leaf ? leafClassName : nodeClassName,
|
|
103
|
+
)}
|
|
86
104
|
data-testid={node["data-testid"]}
|
|
87
105
|
data-test-value={node["data-test-value"]}
|
|
88
106
|
>
|
|
89
107
|
{filler}
|
|
90
|
-
{
|
|
108
|
+
{node.loading && (
|
|
109
|
+
<div className="flex-initial w-6">
|
|
110
|
+
<Icon icon={CoreIcons.spinner} className="animate-spin text-muted" />
|
|
111
|
+
</div>
|
|
112
|
+
)}
|
|
113
|
+
{!node.leaf && !node.childFiltered && !node.loading && (
|
|
91
114
|
<div className="flex-initial w-6">
|
|
92
115
|
<Icon
|
|
93
116
|
rtlFlip
|
|
@@ -116,48 +139,59 @@ export const TreeNode = ({
|
|
|
116
139
|
/>
|
|
117
140
|
</div>
|
|
118
141
|
)}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
?
|
|
132
|
-
:
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
{!node.empty && (
|
|
143
|
+
<div
|
|
144
|
+
role="none"
|
|
145
|
+
className={classNames(
|
|
146
|
+
"group/tool data-[selected]:bg-primary-100 flex flex-nowrap flex-1 overflow-hidden select-none",
|
|
147
|
+
node.childSelected && "font-medium",
|
|
148
|
+
(canSelect || onClick) && "hover:bg-primary-50 cursor-pointer",
|
|
149
|
+
)}
|
|
150
|
+
data-selected={node.selected}
|
|
151
|
+
onClick={() => {
|
|
152
|
+
onClick?.(node.id, node.data);
|
|
153
|
+
canSelect
|
|
154
|
+
? onSelect(node.id)
|
|
155
|
+
: canCheck
|
|
156
|
+
? onChecked(node.id)
|
|
157
|
+
: onToggle(node.id);
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
{nodeIcon && (
|
|
161
|
+
<div className="flex-content w-6 self-center leading-none">
|
|
162
|
+
{isValidElement(nodeIcon) && nodeIcon}
|
|
163
|
+
{isString(nodeIcon) && (
|
|
164
|
+
<Icon
|
|
165
|
+
icon={nodeIcon}
|
|
166
|
+
bg={node.iconBg}
|
|
167
|
+
color={node.iconColor}
|
|
168
|
+
rtlFlip={node.rtlFlip}
|
|
169
|
+
/>
|
|
170
|
+
)}
|
|
171
|
+
</div>
|
|
172
|
+
)}
|
|
173
|
+
{node.childSelected && !node.open && (
|
|
174
|
+
<span className=" flex-content text-primary-500">•</span>
|
|
175
|
+
)}
|
|
176
|
+
<div role="none" className="flex-1 truncate">
|
|
177
|
+
{children}
|
|
143
178
|
</div>
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
179
|
+
{!isEmpty(node.badge) && (
|
|
180
|
+
<Badge
|
|
181
|
+
{...badgeProps}
|
|
182
|
+
className={classNames(
|
|
183
|
+
"me-1 self-center flex-content bg-opacity-50",
|
|
184
|
+
badgeProps.className,
|
|
185
|
+
)}
|
|
186
|
+
/>
|
|
187
|
+
)}
|
|
150
188
|
</div>
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
)}
|
|
158
|
-
/>
|
|
159
|
-
)}
|
|
160
|
-
</div>
|
|
189
|
+
)}
|
|
190
|
+
{node.empty && (
|
|
191
|
+
<div className="text-muted text-sm select-none">
|
|
192
|
+
{t("tree.noItems")}
|
|
193
|
+
</div>
|
|
194
|
+
)}
|
|
161
195
|
</div>
|
|
162
196
|
);
|
|
163
197
|
};
|
package/src/tree/TreePanel.tsx
CHANGED
|
@@ -55,7 +55,16 @@ export const TreePanel = <T extends KeyValue>({
|
|
|
55
55
|
expander = "box",
|
|
56
56
|
onChecked,
|
|
57
57
|
onLoad,
|
|
58
|
+
onQuery,
|
|
58
59
|
onSelect,
|
|
60
|
+
onClick,
|
|
61
|
+
sorter,
|
|
62
|
+
makeLabel,
|
|
63
|
+
makeIcon,
|
|
64
|
+
defaultExpanded,
|
|
65
|
+
onExpandToggle,
|
|
66
|
+
leafClassName,
|
|
67
|
+
nodeClassName,
|
|
59
68
|
...aria
|
|
60
69
|
}: TreePanelProps<T>) => {
|
|
61
70
|
const {
|
|
@@ -72,8 +81,13 @@ export const TreePanel = <T extends KeyValue>({
|
|
|
72
81
|
checked,
|
|
73
82
|
selected,
|
|
74
83
|
matcher,
|
|
84
|
+
sorter,
|
|
85
|
+
onLoad,
|
|
86
|
+
onQuery,
|
|
75
87
|
onSelect,
|
|
76
88
|
onChecked,
|
|
89
|
+
defaultExpanded,
|
|
90
|
+
onExpandToggle,
|
|
77
91
|
});
|
|
78
92
|
|
|
79
93
|
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
@@ -99,7 +113,7 @@ export const TreePanel = <T extends KeyValue>({
|
|
|
99
113
|
ref,
|
|
100
114
|
() => ({
|
|
101
115
|
open: (id: string) => expand(id),
|
|
102
|
-
select: (id: string) =>
|
|
116
|
+
select: (id: string) => select(id),
|
|
103
117
|
}),
|
|
104
118
|
[],
|
|
105
119
|
);
|
|
@@ -151,12 +165,18 @@ export const TreePanel = <T extends KeyValue>({
|
|
|
151
165
|
checkable={checkable}
|
|
152
166
|
selectable={selectable}
|
|
153
167
|
defaultLeafIcon={defaultLeafIcon}
|
|
154
|
-
defaultNodeIcon={
|
|
168
|
+
defaultNodeIcon={
|
|
169
|
+
makeIcon?.(tree[index].data) ?? defaultNodeIcon
|
|
170
|
+
}
|
|
155
171
|
onToggle={toggleExpand}
|
|
156
172
|
onChecked={toggleCheck}
|
|
157
173
|
onSelect={select}
|
|
174
|
+
onClick={onClick}
|
|
175
|
+
leafClassName={leafClassName}
|
|
176
|
+
nodeClassName={nodeClassName}
|
|
158
177
|
>
|
|
159
|
-
{(tree[index].data && renderer?.(tree[index].data))
|
|
178
|
+
{(tree[index].data && renderer?.(tree[index].data)) ??
|
|
179
|
+
makeLabel?.(tree[index].data) ??
|
|
160
180
|
tree[index].label}
|
|
161
181
|
</TreeNode>
|
|
162
182
|
</div>
|
package/src/tree/types.ts
CHANGED
|
@@ -30,8 +30,9 @@ import {
|
|
|
30
30
|
type RefProp,
|
|
31
31
|
type TestProps,
|
|
32
32
|
} from "@react-fabric/core/dist/types/types";
|
|
33
|
+
import { type ElementType } from "react";
|
|
33
34
|
|
|
34
|
-
interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
|
|
35
|
+
interface TreeBaseNode<T> extends Omit<IconProps, "icon">, CssProp, TestProps {
|
|
35
36
|
id: string;
|
|
36
37
|
/**
|
|
37
38
|
* display label
|
|
@@ -54,20 +55,25 @@ interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
|
|
|
54
55
|
*/
|
|
55
56
|
disabled?: boolean;
|
|
56
57
|
data?: T;
|
|
58
|
+
|
|
59
|
+
icon?: ElementType<IconProps> | string;
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
export type TreeNodeType<T extends KeyValue = KeyValue> = TreeBaseNode<T> &
|
|
60
63
|
(
|
|
61
64
|
| { leaf: true; children?: never }
|
|
62
65
|
| {
|
|
63
|
-
leaf?:
|
|
66
|
+
leaf?: boolean;
|
|
64
67
|
children?: Array<TreeNodeType<T>>;
|
|
65
68
|
open?: boolean;
|
|
66
69
|
iconOpen?: string;
|
|
67
70
|
}
|
|
68
71
|
);
|
|
69
72
|
|
|
70
|
-
export interface InternalNode
|
|
73
|
+
export interface InternalNode
|
|
74
|
+
extends Omit<IconProps, "icon">,
|
|
75
|
+
CssProp,
|
|
76
|
+
TestProps {
|
|
71
77
|
id: string;
|
|
72
78
|
label: Elements<JSX.Element>;
|
|
73
79
|
queryable?: string;
|
|
@@ -84,6 +90,7 @@ export interface InternalNode extends IconProps, CssProp, TestProps {
|
|
|
84
90
|
loaded: boolean;
|
|
85
91
|
parent?: string;
|
|
86
92
|
empty?: boolean;
|
|
93
|
+
icon?: ElementType<IconProps> | string;
|
|
87
94
|
errored?: string;
|
|
88
95
|
selected?: true;
|
|
89
96
|
childSelected?: true;
|
|
@@ -101,9 +108,12 @@ export interface TreeNodeProps extends ChildProp {
|
|
|
101
108
|
noLines?: boolean;
|
|
102
109
|
selectable?: true | "leafOnly";
|
|
103
110
|
checkable?: true | "leafOnly";
|
|
111
|
+
leafClassName?: string;
|
|
112
|
+
nodeClassName?: string;
|
|
104
113
|
onToggle: (id: string) => void;
|
|
105
114
|
onSelect: (id: string) => void;
|
|
106
115
|
onChecked: (id: string) => void;
|
|
116
|
+
onClick?: (id: string, node: any) => void;
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
export interface TreeRef {
|
|
@@ -164,10 +174,18 @@ export interface TreePanelProps<T extends KeyValue = KeyValue>
|
|
|
164
174
|
| Promise<Array<TreeNodeType<T>> | undefined>
|
|
165
175
|
| Array<TreeNodeType<T>>
|
|
166
176
|
| undefined;
|
|
177
|
+
/**
|
|
178
|
+
* callback to load tree using search query
|
|
179
|
+
*/
|
|
180
|
+
onQuery?: (query: string) => void;
|
|
181
|
+
/**
|
|
182
|
+
* callback on click of tree node
|
|
183
|
+
*/
|
|
184
|
+
onClick?: (id: string, data: T) => void;
|
|
167
185
|
/**
|
|
168
186
|
* callback on selection of tree node
|
|
169
187
|
*/
|
|
170
|
-
onSelect?: (id: string) => void;
|
|
188
|
+
onSelect?: (id: string, data: T) => void;
|
|
171
189
|
/**
|
|
172
190
|
* callback on change of checked list
|
|
173
191
|
*/
|
|
@@ -180,6 +198,16 @@ export interface TreePanelProps<T extends KeyValue = KeyValue>
|
|
|
180
198
|
* item filter matcher
|
|
181
199
|
*/
|
|
182
200
|
matcher?: (data: T, query: string) => boolean;
|
|
201
|
+
|
|
202
|
+
sorter?: false | ((a: T, b: T) => number);
|
|
203
|
+
makeLabel?: (node: T) => string;
|
|
204
|
+
makeIcon?: (node: T) => string;
|
|
205
|
+
|
|
206
|
+
defaultExpanded?: string[];
|
|
207
|
+
onExpandToggle?: (id: string[]) => void;
|
|
208
|
+
|
|
209
|
+
leafClassName?: string;
|
|
210
|
+
nodeClassName?: string;
|
|
183
211
|
}
|
|
184
212
|
|
|
185
213
|
export const iconExpandAll =
|