@react-fabric/data 1.0.0-beta.0 → 1.0.0-beta.1
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/table/AddColumn.js +56 -0
- package/dist/cjs/table/BodyCell.js +13 -4
- package/dist/cjs/table/CheckboxCell.js +44 -0
- package/dist/cjs/table/Context.js +43 -0
- package/dist/cjs/table/EmptyCell.js +52 -0
- package/dist/cjs/table/HeaderCell.js +77 -32
- package/dist/cjs/table/Table.js +136 -94
- package/dist/cjs/table/types.js +32 -0
- package/dist/cjs/table/useResizer.js +65 -0
- package/dist/cjs/table/useTableColumns.js +45 -8
- package/dist/cjs/table/useVirtualTable.js +42 -25
- package/dist/esm/table/AddColumn.js +56 -0
- package/dist/esm/table/BodyCell.js +13 -4
- package/dist/esm/table/CheckboxCell.js +44 -0
- package/dist/esm/table/Context.js +42 -0
- package/dist/esm/table/EmptyCell.js +52 -0
- package/dist/esm/table/HeaderCell.js +79 -34
- package/dist/esm/table/Table.js +137 -95
- package/dist/esm/table/types.js +30 -0
- package/dist/esm/table/useResizer.js +65 -0
- package/dist/esm/table/useTableColumns.js +46 -9
- package/dist/esm/table/useVirtualTable.js +43 -26
- package/dist/types/table/AddColumn.d.ts +5 -0
- package/dist/types/table/CheckboxCell.d.ts +4 -0
- package/dist/types/table/Context.d.ts +8 -0
- package/dist/types/table/EmptyCell.d.ts +2 -0
- 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 +23 -1
- package/dist/types/table/useResizer.d.ts +1 -0
- package/dist/types/table/useTableColumns.d.ts +6 -4
- package/dist/types/table/useVirtualTable.d.ts +9 -1
- package/package.json +5 -5
- package/src/table/AddColumn.tsx +55 -0
- package/src/table/BodyCell.tsx +18 -4
- package/src/table/CheckboxCell.tsx +48 -0
- package/src/table/Context.tsx +70 -0
- package/src/table/EmptyCell.tsx +57 -0
- package/src/table/HeaderCell.tsx +92 -25
- package/src/table/Table.tsx +149 -89
- package/src/table/types.ts +20 -1
- package/src/table/useResizer.ts +66 -0
- package/src/table/useTableColumns.ts +51 -18
- package/src/table/useVirtualTable.ts +47 -2
package/src/table/Table.tsx
CHANGED
|
@@ -21,21 +21,49 @@
|
|
|
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 { EmptyContent, Loading, useDebounce } from "@react-fabric/core";
|
|
25
|
+
import classNames from "classnames";
|
|
26
|
+
import { useMemo } from "react";
|
|
27
|
+
import { useTranslation } from "react-i18next";
|
|
28
|
+
import { AddColumn } from "./AddColumn";
|
|
25
29
|
import { BodyCell } from "./BodyCell";
|
|
30
|
+
import { CheckboxCell } from "./CheckboxCell";
|
|
31
|
+
import { TableProvider } from "./Context";
|
|
32
|
+
import { EmptyCell } from "./EmptyCell";
|
|
26
33
|
import { HeaderCell } from "./HeaderCell";
|
|
27
|
-
import { type
|
|
34
|
+
import { type TableProps } from "./types";
|
|
28
35
|
import { useTableColumns } from "./useTableColumns";
|
|
29
36
|
import { useVirtualTable } from "./useVirtualTable";
|
|
30
|
-
import classNames from "classnames";
|
|
31
37
|
|
|
32
38
|
export const Table = <T extends KeyValue = KeyValue>({
|
|
33
39
|
data,
|
|
34
40
|
columns,
|
|
41
|
+
keyProperty,
|
|
42
|
+
checkableRows,
|
|
43
|
+
hideableColumns,
|
|
44
|
+
sort,
|
|
45
|
+
loading,
|
|
46
|
+
onSort,
|
|
47
|
+
onRowClick,
|
|
48
|
+
onCheckedChanged,
|
|
35
49
|
}: TableProps<T>) => {
|
|
36
|
-
const {
|
|
37
|
-
const
|
|
38
|
-
|
|
50
|
+
const { t } = useTranslation("data");
|
|
51
|
+
const fireCheckChanged = useDebounce(
|
|
52
|
+
(checkedIds: Array<keyof T>) => onCheckedChanged?.(checkedIds),
|
|
53
|
+
[],
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const { state, onHide } = useTableColumns(columns);
|
|
57
|
+
const {
|
|
58
|
+
scrollerRef,
|
|
59
|
+
items,
|
|
60
|
+
checkState,
|
|
61
|
+
toggleChecked,
|
|
62
|
+
toggleAllChecked,
|
|
63
|
+
getData,
|
|
64
|
+
totalSize,
|
|
65
|
+
measureElement,
|
|
66
|
+
} = useVirtualTable(data, keyProperty, fireCheckChanged);
|
|
39
67
|
|
|
40
68
|
const wrapperStart = useMemo(
|
|
41
69
|
() => "bg-inherit sticky start-0 flex flex-nowrap z-1",
|
|
@@ -45,99 +73,131 @@ export const Table = <T extends KeyValue = KeyValue>({
|
|
|
45
73
|
() => "bg-inherit sticky end-0 flex flex-nowrap z-1 border-s -ms-px",
|
|
46
74
|
[],
|
|
47
75
|
);
|
|
48
|
-
|
|
49
|
-
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<TableProvider>
|
|
50
79
|
<div
|
|
51
|
-
|
|
52
|
-
className="overflow-hidden border-e"
|
|
80
|
+
className="grid area-content overflow-auto relative"
|
|
53
81
|
style={{
|
|
54
|
-
|
|
55
|
-
minWidth: col.minWidth ?? "2rem",
|
|
56
|
-
maxWidth: col.maxWidth ?? "32rem",
|
|
82
|
+
gridTemplate: `"head" auto "loader" auto "content" 1fr "foot" auto/1fr`,
|
|
57
83
|
}}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
<HeaderCell key={idx} {...col} />
|
|
84
|
+
ref={scrollerRef}
|
|
85
|
+
>
|
|
86
|
+
<div className="bg-base font-medium sticky top-0 area-head border-b select-none flex flex-nowrap z-2">
|
|
87
|
+
<div className={wrapperStart}>
|
|
88
|
+
{checkableRows && (
|
|
89
|
+
<CheckboxCell
|
|
90
|
+
state={checkState.allChecked}
|
|
91
|
+
onClick={toggleAllChecked}
|
|
92
|
+
/>
|
|
93
|
+
)}
|
|
94
|
+
{state.start?.map((col, idx) => (
|
|
95
|
+
<HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
|
|
96
|
+
))}
|
|
97
|
+
</div>
|
|
98
|
+
{state.cols?.map((col, idx) => (
|
|
99
|
+
<HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
|
|
75
100
|
))}
|
|
101
|
+
<div className={wrapperEnd}>
|
|
102
|
+
{state.end?.map((col, idx) => (
|
|
103
|
+
<HeaderCell key={idx} {...col} sort={sort} onSort={onSort} />
|
|
104
|
+
))}
|
|
105
|
+
|
|
106
|
+
{hideableColumns && (
|
|
107
|
+
<AddColumn
|
|
108
|
+
onClick={onHide}
|
|
109
|
+
columns={state.columns.filter((col) => col.hideable !== false)}
|
|
110
|
+
/>
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
76
113
|
</div>
|
|
77
|
-
{
|
|
78
|
-
|
|
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 }) => (
|
|
114
|
+
{loading && <Loading />}
|
|
115
|
+
{data.length > 0 && (
|
|
88
116
|
<div
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"flex flex-nowrap",
|
|
92
|
-
index % 2 ? "bg-even" : "bg-odd",
|
|
93
|
-
)}
|
|
94
|
-
data-index={index}
|
|
95
|
-
ref={measureElement}
|
|
117
|
+
className="area-content flex flex-col flex-nowrap"
|
|
118
|
+
style={{ height: totalSize() }}
|
|
96
119
|
>
|
|
97
|
-
<div
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
<div style={{ height: items[0]?.start }} />
|
|
121
|
+
{items.map(({ index, key }) => {
|
|
122
|
+
const data = getData(index);
|
|
123
|
+
return (
|
|
124
|
+
<div
|
|
125
|
+
key={key}
|
|
126
|
+
role="none"
|
|
127
|
+
className={classNames(
|
|
128
|
+
"flex flex-nowrap",
|
|
129
|
+
index % 2 ? "bg-even" : "bg-odd",
|
|
130
|
+
onRowClick && "hover:bg-primary-50 active:bg-primary-100",
|
|
131
|
+
)}
|
|
132
|
+
onClick={() =>
|
|
133
|
+
checkableRows && !onRowClick
|
|
134
|
+
? toggleChecked(data[keyProperty])
|
|
135
|
+
: onRowClick?.(data)
|
|
136
|
+
}
|
|
137
|
+
data-index={index}
|
|
138
|
+
ref={measureElement}
|
|
139
|
+
>
|
|
140
|
+
<div className={wrapperStart}>
|
|
141
|
+
{checkableRows && (
|
|
142
|
+
<CheckboxCell
|
|
143
|
+
onClick={() => toggleChecked(data[keyProperty])}
|
|
144
|
+
state={
|
|
145
|
+
checkState.checked.includes(data[keyProperty]) ? 1 : 0
|
|
146
|
+
}
|
|
147
|
+
/>
|
|
148
|
+
)}
|
|
149
|
+
{state.start?.map((col, ids) => (
|
|
150
|
+
<BodyCell
|
|
151
|
+
key={`${key}:${ids}`}
|
|
152
|
+
column={col}
|
|
153
|
+
item={data}
|
|
154
|
+
/>
|
|
155
|
+
))}
|
|
156
|
+
</div>
|
|
157
|
+
{state.cols?.map((col, idc) => (
|
|
158
|
+
<BodyCell key={`${key}:${idc}`} column={col} item={data} />
|
|
159
|
+
))}
|
|
160
|
+
<div className={wrapperEnd}>
|
|
161
|
+
{state.end?.map((col, ide) => (
|
|
162
|
+
<BodyCell
|
|
163
|
+
key={`${key}:${ide}`}
|
|
164
|
+
column={col}
|
|
165
|
+
item={data}
|
|
166
|
+
/>
|
|
167
|
+
))}
|
|
168
|
+
{hideableColumns && <div className="w-6" />}
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
);
|
|
172
|
+
})}
|
|
173
|
+
<div className="flex-1 flex flex-nowrap">
|
|
174
|
+
<div className={wrapperStart}>
|
|
175
|
+
{checkableRows && <div className="w-6" />}
|
|
176
|
+
{state.start?.map((col) => (
|
|
177
|
+
<EmptyCell key={String(col.id)} {...col} />
|
|
178
|
+
))}
|
|
179
|
+
</div>
|
|
180
|
+
{state.cols?.map((col) => (
|
|
181
|
+
<EmptyCell key={String(col.id)} {...col} />
|
|
120
182
|
))}
|
|
183
|
+
<div className={wrapperEnd}>
|
|
184
|
+
{state.end?.map((col) => (
|
|
185
|
+
<EmptyCell key={String(col.id)} {...col} />
|
|
186
|
+
))}
|
|
187
|
+
|
|
188
|
+
{hideableColumns && <div className="w-6" />}
|
|
189
|
+
</div>
|
|
121
190
|
</div>
|
|
122
191
|
</div>
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
<
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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>
|
|
192
|
+
)}
|
|
193
|
+
{!loading && data.length === 0 && (
|
|
194
|
+
<EmptyContent
|
|
195
|
+
size="sm"
|
|
196
|
+
className="bg-dimmed"
|
|
197
|
+
message={t("datagrid.empty")}
|
|
198
|
+
/>
|
|
199
|
+
)}
|
|
140
200
|
</div>
|
|
141
|
-
</
|
|
201
|
+
</TableProvider>
|
|
142
202
|
);
|
|
143
203
|
};
|
package/src/table/types.ts
CHANGED
|
@@ -28,6 +28,10 @@ import {
|
|
|
28
28
|
} from "@react-fabric/core/dist/types/types";
|
|
29
29
|
import { type ReactElement } from "react";
|
|
30
30
|
|
|
31
|
+
export const COL_MIN_WIDTH = "3rem";
|
|
32
|
+
export const COL_MAX_WIDTH = "64rem";
|
|
33
|
+
export const COL_DEFAULT_WIDTH = "12rem";
|
|
34
|
+
|
|
31
35
|
export interface ColumnType<T = KeyValue>
|
|
32
36
|
extends CssProp,
|
|
33
37
|
IconProps,
|
|
@@ -61,5 +65,20 @@ export interface ColumnType<T = KeyValue>
|
|
|
61
65
|
export interface TableProps<T = KeyValue> {
|
|
62
66
|
columns: Array<ColumnType<T>>;
|
|
63
67
|
data: T[];
|
|
64
|
-
|
|
68
|
+
keyProperty: keyof T;
|
|
69
|
+
/**
|
|
70
|
+
* checkable rows
|
|
71
|
+
*/
|
|
72
|
+
checkableRows?: boolean;
|
|
73
|
+
onCheckedChanged?: (rows: Array<keyof T>) => void;
|
|
74
|
+
/**
|
|
75
|
+
* sorting
|
|
76
|
+
*/
|
|
77
|
+
sort?: { id: string; order: "asc" | "desc" };
|
|
78
|
+
onSort?: (sort: { id: string; order: "asc" | "desc" }) => void;
|
|
79
|
+
|
|
80
|
+
loading?: boolean;
|
|
81
|
+
hideableColumns?: boolean;
|
|
82
|
+
|
|
83
|
+
onRowClick?: (data: T) => void;
|
|
65
84
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
export const useResizer = (
|
|
25
|
+
colEl: HTMLElement,
|
|
26
|
+
ghostEl: HTMLElement,
|
|
27
|
+
callback: (width: number) => void,
|
|
28
|
+
) => {
|
|
29
|
+
const placeholder = ghostEl.firstElementChild as HTMLElement;
|
|
30
|
+
const isRtl = getComputedStyle(colEl).direction === "rtl";
|
|
31
|
+
|
|
32
|
+
const onResize = (evt: MouseEvent) => {
|
|
33
|
+
/** ***************** check if reverse enabled of RTL *******************/
|
|
34
|
+
const box = colEl.getBoundingClientRect();
|
|
35
|
+
const x = isRtl ? box.left - evt.clientX : evt.clientX - box.right;
|
|
36
|
+
placeholder.style.width = `${colEl.offsetWidth + x}px`;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** ***************** dettach handlers on mouseup *******************/
|
|
40
|
+
const onResizeEnd = () => {
|
|
41
|
+
callback(placeholder.offsetWidth);
|
|
42
|
+
ghostEl.style.display = "none";
|
|
43
|
+
document.removeEventListener("mousemove", onResize);
|
|
44
|
+
document.removeEventListener("mouseup", onResizeEnd);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** ***************** attach handlers *******************/
|
|
48
|
+
const box = colEl.getBoundingClientRect();
|
|
49
|
+
if (colEl.parentElement != null) {
|
|
50
|
+
const parentBox = colEl.parentElement?.getBoundingClientRect();
|
|
51
|
+
if (isRtl) {
|
|
52
|
+
placeholder.style.left = "unset";
|
|
53
|
+
placeholder.style.right = `${box.right - parentBox.right}px`;
|
|
54
|
+
} else {
|
|
55
|
+
placeholder.style.right = "unset";
|
|
56
|
+
placeholder.style.left = `${box.left - parentBox.left}px`;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
placeholder.style.width = `${colEl.offsetWidth}px`;
|
|
60
|
+
placeholder.style.minWidth = colEl.style.minWidth || "48px";
|
|
61
|
+
placeholder.style.maxWidth = colEl.style.maxWidth || "75vw";
|
|
62
|
+
ghostEl.style.display = "block";
|
|
63
|
+
|
|
64
|
+
document.addEventListener("mousemove", onResize);
|
|
65
|
+
document.addEventListener("mouseup", onResizeEnd);
|
|
66
|
+
};
|
|
@@ -22,31 +22,64 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { groupBy } from "@react-fabric/utilities";
|
|
25
|
-
import { useEffect,
|
|
25
|
+
import { useEffect, useReducer } from "react";
|
|
26
26
|
import { type ColumnType } from "./types";
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
start: [],
|
|
35
|
-
end: [],
|
|
36
|
-
cols: [],
|
|
37
|
-
});
|
|
28
|
+
interface ColumnState {
|
|
29
|
+
columns: ColumnType[];
|
|
30
|
+
start: ColumnType[];
|
|
31
|
+
end: ColumnType[];
|
|
32
|
+
cols: ColumnType[];
|
|
33
|
+
}
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
type ColumnActions =
|
|
36
|
+
| { type: "columns"; columns: ColumnType[] }
|
|
37
|
+
| { type: "hide"; id: string };
|
|
38
|
+
|
|
39
|
+
export const useTableColumns = (columns: ColumnType[]) => {
|
|
40
|
+
const [state, dispatch] = useReducer(
|
|
41
|
+
(state: ColumnState, action: ColumnActions) => {
|
|
42
|
+
if (action.type === "columns") {
|
|
43
|
+
state.columns = action.columns;
|
|
44
|
+
}
|
|
45
|
+
if (action.type === "hide") {
|
|
46
|
+
const col = state.columns.find((col) => col.id === action.id);
|
|
47
|
+
col &&
|
|
48
|
+
state.columns.splice(state.columns.indexOf(col), 1, {
|
|
49
|
+
...col,
|
|
50
|
+
hidden: col.hidden !== true,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const {
|
|
54
|
+
start = [],
|
|
55
|
+
end = [],
|
|
56
|
+
cols = [],
|
|
57
|
+
} = groupBy(
|
|
58
|
+
state.columns.filter((col) => col.hidden !== true),
|
|
43
59
|
"locked",
|
|
44
60
|
"cols",
|
|
45
|
-
)
|
|
46
|
-
|
|
61
|
+
);
|
|
62
|
+
return {
|
|
63
|
+
...state,
|
|
64
|
+
start,
|
|
65
|
+
end,
|
|
66
|
+
cols,
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
columns: [],
|
|
71
|
+
start: [],
|
|
72
|
+
end: [],
|
|
73
|
+
cols: [],
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
dispatch({ type: "columns", columns });
|
|
47
79
|
}, [columns]);
|
|
48
80
|
|
|
49
81
|
return {
|
|
50
|
-
|
|
82
|
+
state,
|
|
83
|
+
onHide: (id: string) => dispatch({ type: "hide", id }),
|
|
51
84
|
};
|
|
52
85
|
};
|
|
@@ -22,9 +22,20 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
25
|
-
import { useCallback, useRef } from "react";
|
|
25
|
+
import { useCallback, useReducer, useRef } from "react";
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
interface DataState {
|
|
28
|
+
checked: Key[];
|
|
29
|
+
allChecked: 0 | 1 | 2;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type DataAction = { type: "check"; key: Key } | { type: "checkall" };
|
|
33
|
+
|
|
34
|
+
export const useVirtualTable = (
|
|
35
|
+
data: KeyValue[],
|
|
36
|
+
keyProperty: Key,
|
|
37
|
+
onCheckChanged: (checked: Key[]) => void,
|
|
38
|
+
) => {
|
|
28
39
|
// TODO: refactor data into item list, apply grouping if groupColumn provided
|
|
29
40
|
// TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
|
|
30
41
|
|
|
@@ -35,6 +46,37 @@ export const useVirtualTable = (data: KeyValue[]) => {
|
|
|
35
46
|
estimateSize: () => 24,
|
|
36
47
|
});
|
|
37
48
|
|
|
49
|
+
const [state, dispatch] = useReducer(
|
|
50
|
+
(state: DataState, action: DataAction) => {
|
|
51
|
+
if (action.type === "check") {
|
|
52
|
+
if (state.checked.includes(action.key))
|
|
53
|
+
state.checked.splice(state.checked.indexOf(action.key), 1);
|
|
54
|
+
else state.checked.push(action.key);
|
|
55
|
+
}
|
|
56
|
+
if (action.type === "checkall") {
|
|
57
|
+
if (state.allChecked !== 0) {
|
|
58
|
+
state.checked = [];
|
|
59
|
+
} else {
|
|
60
|
+
state.checked = data.map((item) => item[keyProperty]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
onCheckChanged(state.checked);
|
|
64
|
+
return {
|
|
65
|
+
...state,
|
|
66
|
+
allChecked:
|
|
67
|
+
state.checked.length === data.length
|
|
68
|
+
? 1
|
|
69
|
+
: state.checked.length
|
|
70
|
+
? 2
|
|
71
|
+
: 0,
|
|
72
|
+
} as DataState;
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
checked: [],
|
|
76
|
+
allChecked: 0,
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
|
|
38
80
|
const virtualItems = virtualizer.getVirtualItems();
|
|
39
81
|
|
|
40
82
|
const getData = useCallback(
|
|
@@ -47,8 +89,11 @@ export const useVirtualTable = (data: KeyValue[]) => {
|
|
|
47
89
|
return {
|
|
48
90
|
scrollerRef,
|
|
49
91
|
items: virtualItems,
|
|
92
|
+
checkState: state,
|
|
50
93
|
getData,
|
|
51
94
|
totalSize: virtualizer.getTotalSize,
|
|
52
95
|
measureElement: virtualizer.measureElement,
|
|
96
|
+
toggleChecked: (key: Key) => dispatch({ type: "check", key }),
|
|
97
|
+
toggleAllChecked: () => dispatch({ type: "checkall" }),
|
|
53
98
|
};
|
|
54
99
|
};
|