@react-fabric/data 1.0.0-beta.3 → 1.0.0-beta.31
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/json/JsonViewer.js +30 -23
- 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 +43 -29
- package/dist/cjs/virtual/List.js +43 -31
- 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/json/JsonViewer.js +31 -24
- 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 +43 -29
- package/dist/esm/virtual/List.js +43 -31
- 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/json/JsonViewer.tsx +31 -31
- 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 +45 -34
- package/src/virtual/List.tsx +40 -27
package/src/table/Table.tsx
CHANGED
|
@@ -21,12 +21,25 @@
|
|
|
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 {
|
|
24
|
+
import {
|
|
25
|
+
CoreIcons,
|
|
26
|
+
EmptyContent,
|
|
27
|
+
Icon,
|
|
28
|
+
Loading,
|
|
29
|
+
useDebounce,
|
|
30
|
+
} from "@react-fabric/core";
|
|
31
|
+
import { debounce } from "@react-fabric/utilities";
|
|
25
32
|
import classNames from "classnames";
|
|
26
|
-
import {
|
|
33
|
+
import {
|
|
34
|
+
useEffect,
|
|
35
|
+
useImperativeHandle,
|
|
36
|
+
useLayoutEffect,
|
|
37
|
+
useMemo,
|
|
38
|
+
useRef,
|
|
39
|
+
} from "react";
|
|
27
40
|
import { useTranslation } from "react-i18next";
|
|
28
41
|
import { AddColumn } from "./AddColumn";
|
|
29
|
-
import {
|
|
42
|
+
import { BodyRow } from "./BodyRow";
|
|
30
43
|
import { CheckboxCell } from "./CheckboxCell";
|
|
31
44
|
import { TableProvider } from "./Context";
|
|
32
45
|
import { EmptyCell } from "./EmptyCell";
|
|
@@ -39,12 +52,20 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
39
52
|
ref,
|
|
40
53
|
data,
|
|
41
54
|
columns,
|
|
42
|
-
keyProperty,
|
|
55
|
+
keyProperty = "id",
|
|
56
|
+
groupProperty,
|
|
43
57
|
checkableRows,
|
|
58
|
+
initialScroll,
|
|
44
59
|
hideableColumns,
|
|
45
60
|
sort,
|
|
46
61
|
loading,
|
|
62
|
+
checked,
|
|
63
|
+
emptyDisplay,
|
|
64
|
+
groupRenderer,
|
|
65
|
+
canExpand,
|
|
66
|
+
children,
|
|
47
67
|
onSort,
|
|
68
|
+
onScroll,
|
|
48
69
|
onRowClick,
|
|
49
70
|
onCheckedChanged,
|
|
50
71
|
}: TableProps<T>) => {
|
|
@@ -58,13 +79,24 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
58
79
|
const {
|
|
59
80
|
scrollerRef,
|
|
60
81
|
items,
|
|
82
|
+
top,
|
|
61
83
|
checkState,
|
|
84
|
+
virtualizer,
|
|
85
|
+
hasActiveSticky,
|
|
86
|
+
toggleExpand,
|
|
62
87
|
toggleChecked,
|
|
63
88
|
toggleAllChecked,
|
|
89
|
+
toggleGroupExpand,
|
|
64
90
|
getData,
|
|
65
91
|
totalSize,
|
|
92
|
+
isActiveSticky,
|
|
66
93
|
measureElement,
|
|
67
|
-
} = useVirtualTable(data,
|
|
94
|
+
} = useVirtualTable(data, {
|
|
95
|
+
checked,
|
|
96
|
+
keyProperty,
|
|
97
|
+
groupProperty,
|
|
98
|
+
onCheckChanged: fireCheckChanged,
|
|
99
|
+
});
|
|
68
100
|
const refBody = useRef<HTMLDivElement>(null);
|
|
69
101
|
|
|
70
102
|
const wrapperStart = useMemo(
|
|
@@ -76,32 +108,51 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
76
108
|
[],
|
|
77
109
|
);
|
|
78
110
|
|
|
111
|
+
useLayoutEffect(() => {
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
initialScroll &&
|
|
114
|
+
virtualizer.scrollToIndex(initialScroll, { align: "start" });
|
|
115
|
+
}, 100);
|
|
116
|
+
}, []);
|
|
117
|
+
|
|
79
118
|
useImperativeHandle(
|
|
80
119
|
ref,
|
|
81
120
|
() => ({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
?.querySelectorAll<HTMLElement>(`.datatable-row[data-hilight="true"]`)
|
|
85
|
-
.forEach((el) => el && (el.dataset.hilight = undefined));
|
|
86
|
-
const el = refBody.current?.querySelector<HTMLElement>(
|
|
87
|
-
`[data-row="${row}"]`,
|
|
88
|
-
);
|
|
89
|
-
el?.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
90
|
-
el && (el.dataset.hilight = "true");
|
|
121
|
+
scrollTo: (row: number) => {
|
|
122
|
+
virtualizer.scrollToIndex(row, { align: "start" });
|
|
91
123
|
},
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
?.
|
|
95
|
-
.
|
|
124
|
+
hilight(row) {
|
|
125
|
+
scrollerRef.current
|
|
126
|
+
?.querySelector(".hilight")
|
|
127
|
+
?.classList.remove("hilight", "ring-1");
|
|
128
|
+
virtualizer.scrollToIndex(Math.floor(row), {
|
|
129
|
+
align: "start",
|
|
130
|
+
});
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
scrollerRef.current
|
|
133
|
+
?.querySelector(`[data-index="${row}"]`)
|
|
134
|
+
?.classList.add("hilight", "ring-1");
|
|
135
|
+
}, 100);
|
|
96
136
|
},
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
137
|
+
unhilight() {
|
|
138
|
+
scrollerRef.current
|
|
139
|
+
?.querySelector(".hilight")
|
|
140
|
+
?.classList.remove("hilight", "ring-1");
|
|
100
141
|
},
|
|
101
142
|
}),
|
|
102
143
|
[],
|
|
103
144
|
);
|
|
104
145
|
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
const handle = debounce((top: number) => {
|
|
148
|
+
onScroll?.(top ?? 0);
|
|
149
|
+
});
|
|
150
|
+
handle(top);
|
|
151
|
+
return () => {
|
|
152
|
+
handle.cancel();
|
|
153
|
+
};
|
|
154
|
+
}, [top]);
|
|
155
|
+
|
|
105
156
|
return (
|
|
106
157
|
<TableProvider>
|
|
107
158
|
<div
|
|
@@ -109,10 +160,15 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
109
160
|
style={{
|
|
110
161
|
gridTemplate: `"head" auto "loader" auto "content" 1fr "foot" auto/1fr`,
|
|
111
162
|
}}
|
|
163
|
+
data-ref="tableScroller"
|
|
112
164
|
ref={scrollerRef}
|
|
113
165
|
>
|
|
114
|
-
<div
|
|
166
|
+
<div
|
|
167
|
+
data-ref="tableHeader"
|
|
168
|
+
className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2"
|
|
169
|
+
>
|
|
115
170
|
<div className={wrapperStart}>
|
|
171
|
+
{canExpand && <div className="w-6" />}
|
|
116
172
|
{checkableRows && (
|
|
117
173
|
<CheckboxCell
|
|
118
174
|
state={checkState.allChecked}
|
|
@@ -146,62 +202,72 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
146
202
|
style={{ height: totalSize() }}
|
|
147
203
|
ref={refBody}
|
|
148
204
|
>
|
|
149
|
-
<div
|
|
205
|
+
<div
|
|
206
|
+
style={{
|
|
207
|
+
height: items[hasActiveSticky ? 1 : 0]?.start,
|
|
208
|
+
}}
|
|
209
|
+
/>
|
|
150
210
|
{items.map(({ index, key }) => {
|
|
151
211
|
const data = getData(index);
|
|
152
|
-
return (
|
|
212
|
+
return "__GROUP__" in data ? (
|
|
153
213
|
<div
|
|
154
214
|
key={key}
|
|
155
|
-
|
|
156
|
-
data-
|
|
215
|
+
ref={measureElement}
|
|
216
|
+
data-index={index}
|
|
157
217
|
className={classNames(
|
|
158
|
-
"
|
|
159
|
-
index
|
|
160
|
-
onRowClick && "hover:bg-primary-50 active:bg-primary-100",
|
|
218
|
+
"px-2 py-1 text-md bg-base border-y border-tint-100 cursor-pointer flex items-center gap-1",
|
|
219
|
+
isActiveSticky(index) && "sticky top-8 z-30",
|
|
161
220
|
)}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
? toggleChecked(data[keyProperty])
|
|
165
|
-
: onRowClick?.(data)
|
|
166
|
-
}
|
|
167
|
-
data-index={index}
|
|
168
|
-
ref={measureElement}
|
|
221
|
+
role="none"
|
|
222
|
+
onClick={() => toggleGroupExpand(data.key)}
|
|
169
223
|
>
|
|
170
|
-
<
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
column={col}
|
|
183
|
-
item={data}
|
|
184
|
-
/>
|
|
185
|
-
))}
|
|
186
|
-
</div>
|
|
187
|
-
{state.cols?.map((col, idc) => (
|
|
188
|
-
<BodyCell key={`${key}:${idc}`} column={col} item={data} />
|
|
189
|
-
))}
|
|
190
|
-
<div className={wrapperEnd}>
|
|
191
|
-
{state.end?.map((col, ide) => (
|
|
192
|
-
<BodyCell
|
|
193
|
-
key={`${key}:${ide}`}
|
|
194
|
-
column={col}
|
|
195
|
-
item={data}
|
|
196
|
-
/>
|
|
197
|
-
))}
|
|
198
|
-
{hideableColumns && <div className="w-6" />}
|
|
199
|
-
</div>
|
|
224
|
+
<Icon
|
|
225
|
+
rtlFlip
|
|
226
|
+
icon={
|
|
227
|
+
data.open ? CoreIcons.chevronDown : CoreIcons.chevronRight
|
|
228
|
+
}
|
|
229
|
+
/>
|
|
230
|
+
{groupRenderer?.(data) ?? (
|
|
231
|
+
<div className="flex gap-1 items-center">
|
|
232
|
+
<span>{data.key}</span>
|
|
233
|
+
<span className="text-sm">({data.itemCount})</span>
|
|
234
|
+
</div>
|
|
235
|
+
)}
|
|
200
236
|
</div>
|
|
237
|
+
) : (
|
|
238
|
+
<BodyRow
|
|
239
|
+
rowKey={key}
|
|
240
|
+
index={index}
|
|
241
|
+
checkableRows={checkableRows}
|
|
242
|
+
checked={
|
|
243
|
+
keyProperty &&
|
|
244
|
+
checkState.checked.includes(data[keyProperty])
|
|
245
|
+
? 1
|
|
246
|
+
: 0
|
|
247
|
+
}
|
|
248
|
+
expanded={
|
|
249
|
+
!!keyProperty &&
|
|
250
|
+
checkState.expanded.includes(data[keyProperty])
|
|
251
|
+
}
|
|
252
|
+
data={data}
|
|
253
|
+
hideableColumns={hideableColumns}
|
|
254
|
+
keyProperty={keyProperty}
|
|
255
|
+
measureElement={measureElement}
|
|
256
|
+
onRowClick={onRowClick}
|
|
257
|
+
state={state}
|
|
258
|
+
canExpand={canExpand}
|
|
259
|
+
toggleExpand={toggleExpand}
|
|
260
|
+
expandRow={children}
|
|
261
|
+
toggleChecked={toggleChecked}
|
|
262
|
+
wrapperEnd={wrapperEnd}
|
|
263
|
+
wrapperStart={wrapperStart}
|
|
264
|
+
key={key}
|
|
265
|
+
/>
|
|
201
266
|
);
|
|
202
267
|
})}
|
|
203
268
|
<div className="flex-1 flex flex-nowrap">
|
|
204
269
|
<div className={wrapperStart}>
|
|
270
|
+
{canExpand && <div className="w-6" />}
|
|
205
271
|
{checkableRows && <div className="w-6" />}
|
|
206
272
|
{state.start?.map((col) => (
|
|
207
273
|
<EmptyCell key={String(col.id)} {...col} />
|
|
@@ -220,13 +286,15 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
220
286
|
</div>
|
|
221
287
|
</div>
|
|
222
288
|
)}
|
|
223
|
-
{!loading &&
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
289
|
+
{!loading &&
|
|
290
|
+
data.length === 0 &&
|
|
291
|
+
(emptyDisplay ?? (
|
|
292
|
+
<EmptyContent
|
|
293
|
+
size="sm"
|
|
294
|
+
className="bg-dimmed"
|
|
295
|
+
message={t("datagrid.empty")}
|
|
296
|
+
/>
|
|
297
|
+
))}
|
|
230
298
|
</div>
|
|
231
299
|
</TableProvider>
|
|
232
300
|
);
|
package/src/table/types.ts
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
import {
|
|
25
25
|
type CssProp,
|
|
26
|
+
type Elements,
|
|
26
27
|
type IconProps,
|
|
27
28
|
type RefProp,
|
|
28
29
|
type TestProps,
|
|
@@ -33,16 +34,18 @@ export const COL_MIN_WIDTH = "3rem";
|
|
|
33
34
|
export const COL_MAX_WIDTH = "64rem";
|
|
34
35
|
export const COL_DEFAULT_WIDTH = "12rem";
|
|
35
36
|
|
|
36
|
-
export interface
|
|
37
|
+
export interface TableColumn<T = KeyValue>
|
|
37
38
|
extends CssProp,
|
|
38
39
|
IconProps,
|
|
39
40
|
TestProps {
|
|
40
|
-
id: keyof T;
|
|
41
|
+
id: keyof T | string;
|
|
41
42
|
label?: string;
|
|
42
|
-
|
|
43
|
+
tooltip?: string;
|
|
44
|
+
actions?: ReactElement[];
|
|
43
45
|
align?: "start" | "center" | "end";
|
|
44
46
|
locked?: "start" | "end";
|
|
45
47
|
hidden?: boolean;
|
|
48
|
+
truncate?: boolean;
|
|
46
49
|
hideable?: boolean;
|
|
47
50
|
resizable?: boolean;
|
|
48
51
|
sortable?: boolean;
|
|
@@ -60,7 +63,8 @@ export interface ColumnType<T = KeyValue>
|
|
|
60
63
|
| "time"
|
|
61
64
|
| "unknown";
|
|
62
65
|
format?: string;
|
|
63
|
-
|
|
66
|
+
valueMap?: KeyValue;
|
|
67
|
+
renderer?: (value: AnyObject, data: T, index: number) => Elements<AnyObject>;
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
export interface TableRef {
|
|
@@ -70,14 +74,19 @@ export interface TableRef {
|
|
|
70
74
|
}
|
|
71
75
|
|
|
72
76
|
export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
|
|
73
|
-
columns: Array<
|
|
77
|
+
columns: Array<TableColumn<T>>;
|
|
74
78
|
data: T[];
|
|
75
|
-
keyProperty
|
|
79
|
+
keyProperty?: keyof T;
|
|
80
|
+
groupProperty?: keyof T;
|
|
81
|
+
groupRenderer?: (key: AnyObject) => ReactElement;
|
|
82
|
+
emptyDisplay?: ReactElement;
|
|
83
|
+
|
|
76
84
|
/**
|
|
77
85
|
* checkable rows
|
|
78
86
|
*/
|
|
87
|
+
checked?: AnyObject[];
|
|
79
88
|
checkableRows?: boolean;
|
|
80
|
-
onCheckedChanged?: (rows:
|
|
89
|
+
onCheckedChanged?: (rows: AnyObject[]) => void;
|
|
81
90
|
/**
|
|
82
91
|
* sorting
|
|
83
92
|
*/
|
|
@@ -88,4 +97,10 @@ export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
|
|
|
88
97
|
hideableColumns?: boolean;
|
|
89
98
|
|
|
90
99
|
onRowClick?: (data: T) => void;
|
|
100
|
+
|
|
101
|
+
initialScroll?: number;
|
|
102
|
+
onScroll?: (firstItem: number) => void;
|
|
103
|
+
|
|
104
|
+
canExpand?: (record: T) => boolean;
|
|
105
|
+
children?: (record: T) => Elements<AnyObject>;
|
|
91
106
|
}
|
package/src/table/useResizer.ts
CHANGED
|
@@ -47,13 +47,15 @@ export const useResizer = (
|
|
|
47
47
|
/** ***************** attach handlers *******************/
|
|
48
48
|
const box = colEl.getBoundingClientRect();
|
|
49
49
|
if (colEl.parentElement != null) {
|
|
50
|
-
const parentBox = colEl
|
|
50
|
+
const parentBox = colEl
|
|
51
|
+
.closest("[data-ref='tableScroller']")
|
|
52
|
+
?.getBoundingClientRect() as unknown as DOMRect;
|
|
51
53
|
if (isRtl) {
|
|
52
54
|
placeholder.style.left = "unset";
|
|
53
|
-
placeholder.style.right = `${
|
|
55
|
+
placeholder.style.right = `${parentBox.width - box.right}px`;
|
|
54
56
|
} else {
|
|
55
57
|
placeholder.style.right = "unset";
|
|
56
|
-
placeholder.style.left = `${box.left
|
|
58
|
+
placeholder.style.left = `${box.left}px`;
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
placeholder.style.width = `${colEl.offsetWidth}px`;
|
|
@@ -23,20 +23,20 @@
|
|
|
23
23
|
|
|
24
24
|
import { groupBy } from "@react-fabric/utilities";
|
|
25
25
|
import { useEffect, useReducer } from "react";
|
|
26
|
-
import { type
|
|
26
|
+
import { type TableColumn } from "./types";
|
|
27
27
|
|
|
28
28
|
interface ColumnState {
|
|
29
|
-
columns:
|
|
30
|
-
start:
|
|
31
|
-
end:
|
|
32
|
-
cols:
|
|
29
|
+
columns: TableColumn[];
|
|
30
|
+
start: TableColumn[];
|
|
31
|
+
end: TableColumn[];
|
|
32
|
+
cols: TableColumn[];
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
type ColumnActions =
|
|
36
|
-
| { type: "columns"; columns:
|
|
36
|
+
| { type: "columns"; columns: TableColumn[] }
|
|
37
37
|
| { type: "hide"; id: string };
|
|
38
38
|
|
|
39
|
-
export const useTableColumns = (columns:
|
|
39
|
+
export const useTableColumns = (columns: TableColumn[]) => {
|
|
40
40
|
const [state, dispatch] = useReducer(
|
|
41
41
|
(state: ColumnState, action: ColumnActions) => {
|
|
42
42
|
if (action.type === "columns") {
|
|
@@ -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
|
};
|