@etsoo/react 1.7.93 → 1.7.95
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/README.md +2 -1
- package/__tests__/EventWatcher.tsx +21 -21
- package/__tests__/ReactUtils.ts +4 -4
- package/__tests__/tsconfig.json +17 -17
- package/babel.config.json +8 -8
- package/lib/app/CoreConstants.js +2 -2
- package/lib/app/EventWatcher.d.ts +1 -1
- package/lib/app/EventWatcher.js +3 -5
- package/lib/app/InputDialogProps.d.ts +2 -2
- package/lib/app/ReactUtils.d.ts +2 -2
- package/lib/app/ReactUtils.js +20 -19
- package/lib/components/DnDList.d.ts +3 -3
- package/lib/components/DnDList.js +4 -4
- package/lib/components/DynamicRouter.d.ts +9 -1
- package/lib/components/DynamicRouter.js +18 -2
- package/lib/components/GridColumn.d.ts +6 -6
- package/lib/components/GridColumn.js +7 -7
- package/lib/components/GridLoader.d.ts +4 -4
- package/lib/components/GridLoader.js +2 -2
- package/lib/components/GridMethodRef.d.ts +2 -2
- package/lib/components/ListItemReact.d.ts +2 -2
- package/lib/components/ScrollRestoration.js +3 -3
- package/lib/components/ScrollerGrid.d.ts +7 -12
- package/lib/components/ScrollerGrid.js +13 -17
- package/lib/components/ScrollerList.d.ts +6 -10
- package/lib/components/ScrollerList.js +15 -14
- package/lib/custom/CustomFieldReact.d.ts +1 -1
- package/lib/index.d.ts +32 -32
- package/lib/index.js +31 -31
- package/lib/notifier/Notifier.d.ts +5 -5
- package/lib/notifier/Notifier.js +7 -7
- package/lib/states/CultureState.d.ts +3 -3
- package/lib/states/CultureState.js +3 -3
- package/lib/states/IState.d.ts +2 -2
- package/lib/states/PageState.d.ts +2 -2
- package/lib/states/PageState.js +2 -3
- package/lib/states/State.d.ts +3 -3
- package/lib/states/State.js +2 -2
- package/lib/states/UserState.d.ts +2 -2
- package/lib/states/UserState.js +5 -5
- package/lib/uses/useAsyncState.d.ts +1 -1
- package/lib/uses/useAsyncState.js +1 -1
- package/lib/uses/useCombinedRefs.js +2 -2
- package/lib/uses/useDelayedExecutor.d.ts +1 -1
- package/lib/uses/useDelayedExecutor.js +2 -2
- package/lib/uses/useDimensions.d.ts +1 -1
- package/lib/uses/useDimensions.js +3 -3
- package/lib/uses/useParamsEx.d.ts +1 -1
- package/lib/uses/useParamsEx.js +2 -2
- package/lib/uses/useRefs.d.ts +2 -2
- package/lib/uses/useRefs.js +1 -1
- package/lib/uses/useSearchParamsEx.d.ts +1 -1
- package/lib/uses/useSearchParamsEx.js +3 -3
- package/lib/uses/useTimeout.js +2 -2
- package/lib/uses/useWindowScroll.js +3 -3
- package/lib/uses/useWindowSize.js +4 -5
- package/package.json +72 -74
- package/src/app/CoreConstants.ts +8 -8
- package/src/app/EventWatcher.ts +50 -52
- package/src/app/InputDialogProps.ts +16 -16
- package/src/app/ReactUtils.ts +206 -208
- package/src/components/DnDList.tsx +268 -283
- package/src/components/DynamicRouter.tsx +46 -21
- package/src/components/GridColumn.ts +201 -201
- package/src/components/GridLoader.ts +121 -121
- package/src/components/GridMethodRef.ts +26 -26
- package/src/components/ListItemReact.ts +2 -2
- package/src/components/ScrollRestoration.tsx +24 -24
- package/src/components/ScrollerGrid.tsx +428 -448
- package/src/components/ScrollerList.tsx +320 -332
- package/src/custom/CustomFieldReact.ts +12 -12
- package/src/index.ts +35 -35
- package/src/notifier/Notifier.ts +229 -240
- package/src/states/CultureState.ts +51 -52
- package/src/states/IState.ts +19 -19
- package/src/states/PageState.ts +63 -66
- package/src/states/State.tsx +47 -51
- package/src/states/UserState.ts +98 -98
- package/src/uses/useAsyncState.ts +37 -39
- package/src/uses/useCombinedRefs.ts +16 -16
- package/src/uses/useDelayedExecutor.ts +8 -8
- package/src/uses/useDimensions.ts +102 -103
- package/src/uses/useParamsEx.ts +6 -6
- package/src/uses/useRefs.ts +6 -6
- package/src/uses/useSearchParamsEx.ts +13 -13
- package/src/uses/useTimeout.ts +17 -17
- package/src/uses/useWindowScroll.ts +43 -43
- package/src/uses/useWindowSize.ts +46 -49
- package/tsconfig.json +17 -17
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -38
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
|
@@ -1,156 +1,145 @@
|
|
|
1
|
-
import { DataTypes } from
|
|
2
|
-
import React from
|
|
1
|
+
import { DataTypes } from "@etsoo/shared";
|
|
2
|
+
import React from "react";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from
|
|
4
|
+
Align,
|
|
5
|
+
GridChildComponentProps,
|
|
6
|
+
GridOnItemsRenderedProps,
|
|
7
|
+
VariableSizeGrid,
|
|
8
|
+
VariableSizeGridProps
|
|
9
|
+
} from "react-window";
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from
|
|
16
|
-
import { GridMethodRef } from
|
|
11
|
+
GridLoadDataProps,
|
|
12
|
+
GridLoader,
|
|
13
|
+
GridLoaderPartialStates,
|
|
14
|
+
GridLoaderStates
|
|
15
|
+
} from "./GridLoader";
|
|
16
|
+
import { GridMethodRef } from "./GridMethodRef";
|
|
17
17
|
|
|
18
18
|
export type ScrollerGridItemRendererProps<T> = Omit<
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
GridChildComponentProps<T>,
|
|
20
|
+
"data"
|
|
21
21
|
> & {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Selected items
|
|
24
|
+
*/
|
|
25
|
+
selectedItems: T[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Set items for rerenderer
|
|
29
|
+
* @param callback Callback
|
|
30
|
+
*/
|
|
31
|
+
setItems: (
|
|
32
|
+
callback: (
|
|
33
|
+
items: T[],
|
|
34
|
+
ref: ScrollerGridForwardRef<T>
|
|
35
|
+
) => T[] | undefined | void
|
|
36
|
+
) => void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Data
|
|
40
|
+
*/
|
|
41
|
+
data?: T;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Scroller vertical grid props
|
|
46
46
|
*/
|
|
47
47
|
export interface ScrollerGridProps<T extends object>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
mRef?: React.Ref<ScrollerGridForwardRef<T>>;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* On items select change
|
|
88
|
-
*/
|
|
89
|
-
onSelectChange?: (selectedItems: T[]) => void;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Returns the height of the specified row.
|
|
93
|
-
*/
|
|
94
|
-
rowHeight?: ((index: number) => number) | number;
|
|
48
|
+
extends GridLoader<T>,
|
|
49
|
+
Omit<VariableSizeGridProps<T>, "children" | "rowCount" | "rowHeight"> {
|
|
50
|
+
/**
|
|
51
|
+
* Footer renderer
|
|
52
|
+
*/
|
|
53
|
+
footerRenderer?: (rows: T[], states: GridLoaderStates<T>) => React.ReactNode;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Header renderer
|
|
57
|
+
*/
|
|
58
|
+
headerRenderer?: (states: GridLoaderStates<T>) => React.ReactNode;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Id field
|
|
62
|
+
*/
|
|
63
|
+
idField?: DataTypes.Keys<T>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Item renderer
|
|
67
|
+
*/
|
|
68
|
+
itemRenderer: (props: ScrollerGridItemRendererProps<T>) => React.ReactElement;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Methods
|
|
72
|
+
*/
|
|
73
|
+
mRef?: React.Ref<ScrollerGridForwardRef<T>>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* On items select change
|
|
77
|
+
*/
|
|
78
|
+
onSelectChange?: (selectedItems: T[]) => void;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Returns the height of the specified row.
|
|
82
|
+
*/
|
|
83
|
+
rowHeight?: ((index: number) => number) | number;
|
|
95
84
|
}
|
|
96
85
|
|
|
97
86
|
/**
|
|
98
87
|
* Scroller grid forward ref
|
|
99
88
|
*/
|
|
100
89
|
export interface ScrollerGridForwardRef<T> extends GridMethodRef<T> {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Scroll to the specified offsets
|
|
92
|
+
*/
|
|
93
|
+
scrollTo(params: { scrollLeft: number; scrollTop: number }): void;
|
|
94
|
+
|
|
95
|
+
scrollToItem(params: {
|
|
96
|
+
align?: Align | undefined;
|
|
97
|
+
columnIndex?: number | undefined;
|
|
98
|
+
rowIndex?: number | undefined;
|
|
99
|
+
}): void;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Scroll to the specified item
|
|
103
|
+
*/
|
|
104
|
+
scrollToItem(params: {
|
|
105
|
+
align?: Align | undefined;
|
|
106
|
+
columnIndex?: number | undefined;
|
|
107
|
+
rowIndex?: number | undefined;
|
|
108
|
+
}): void;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Select the item
|
|
112
|
+
* @param rowIndex Row index
|
|
113
|
+
*/
|
|
114
|
+
select(rowIndex: number): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Select or unselect all items
|
|
118
|
+
* @param checked Checked
|
|
119
|
+
*/
|
|
120
|
+
selectAll(checked: boolean): void;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Select item
|
|
124
|
+
* @param item Item
|
|
125
|
+
* @param checked Checked
|
|
126
|
+
*/
|
|
127
|
+
selectItem(item: any, checked: boolean): void;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param index
|
|
132
|
+
* @param shouldForceUpdate
|
|
133
|
+
*/
|
|
134
|
+
resetAfterColumnIndex(index: number, shouldForceUpdate?: boolean): void;
|
|
135
|
+
|
|
136
|
+
resetAfterIndices(params: {
|
|
137
|
+
columnIndex: number;
|
|
138
|
+
rowIndex: number;
|
|
139
|
+
shouldForceUpdate?: boolean | undefined;
|
|
140
|
+
}): void;
|
|
141
|
+
|
|
142
|
+
resetAfterRowIndex(index: number, shouldForceUpdate?: boolean): void;
|
|
154
143
|
}
|
|
155
144
|
|
|
156
145
|
/**
|
|
@@ -159,327 +148,318 @@ export interface ScrollerGridForwardRef<T> extends GridMethodRef<T> {
|
|
|
159
148
|
* @returns Component
|
|
160
149
|
*/
|
|
161
150
|
export const ScrollerGrid = <T extends object>(props: ScrollerGridProps<T>) => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
151
|
+
// Destruct
|
|
152
|
+
const {
|
|
153
|
+
autoLoad = true,
|
|
154
|
+
defaultOrderBy,
|
|
155
|
+
footerRenderer,
|
|
156
|
+
headerRenderer,
|
|
157
|
+
itemRenderer,
|
|
158
|
+
idField = "id" as DataTypes.Keys<T>,
|
|
159
|
+
loadBatchSize,
|
|
160
|
+
loadData,
|
|
161
|
+
mRef,
|
|
162
|
+
onItemsRendered,
|
|
163
|
+
onSelectChange,
|
|
164
|
+
rowHeight = 53,
|
|
165
|
+
threshold = 6,
|
|
166
|
+
width,
|
|
167
|
+
onInitLoad,
|
|
168
|
+
onUpdateRows,
|
|
169
|
+
...rest
|
|
170
|
+
} = props;
|
|
171
|
+
|
|
172
|
+
// Rows
|
|
173
|
+
const [rows, updateRows] = React.useState<T[]>([]);
|
|
174
|
+
const setRows = (rows: T[], reset: boolean = false) => {
|
|
175
|
+
refs.current.loadedItems = rows.length;
|
|
176
|
+
updateRows(rows);
|
|
177
|
+
|
|
178
|
+
if (!reset && onUpdateRows) onUpdateRows(rows, refs.current);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// Refs
|
|
182
|
+
const refs = React.useRef<GridLoaderStates<T>>({
|
|
183
|
+
queryPaging: {
|
|
184
|
+
currentPage: 0,
|
|
185
|
+
orderBy: defaultOrderBy,
|
|
186
|
+
batchSize: 10
|
|
187
|
+
},
|
|
188
|
+
autoLoad,
|
|
189
|
+
hasNextPage: true,
|
|
190
|
+
isNextPageLoading: false,
|
|
191
|
+
loadedItems: 0,
|
|
192
|
+
selectedItems: [],
|
|
193
|
+
idCache: {}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const ref = React.useRef<VariableSizeGrid<T>>(null);
|
|
197
|
+
|
|
198
|
+
// Load data
|
|
199
|
+
const loadDataLocal = (pageAdd: number = 1) => {
|
|
200
|
+
// Prevent multiple loadings
|
|
201
|
+
if (!refs.current.hasNextPage || refs.current.isNextPageLoading) return;
|
|
202
|
+
|
|
203
|
+
// Update state
|
|
204
|
+
refs.current.isNextPageLoading = true;
|
|
205
|
+
|
|
206
|
+
// Parameters
|
|
207
|
+
const { queryPaging, data } = refs.current;
|
|
208
|
+
|
|
209
|
+
const loadProps: GridLoadDataProps = {
|
|
210
|
+
queryPaging,
|
|
211
|
+
data
|
|
191
212
|
};
|
|
192
213
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const { queryPaging, data } = refs.current;
|
|
221
|
-
|
|
222
|
-
const loadProps: GridLoadDataProps = {
|
|
223
|
-
queryPaging,
|
|
224
|
-
data
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
loadData(loadProps).then((result) => {
|
|
228
|
-
if (result == null || refs.current.isMounted === false) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
refs.current.isMounted = true;
|
|
232
|
-
|
|
233
|
-
const newItems = result.length;
|
|
234
|
-
refs.current.lastLoadedItems = newItems;
|
|
235
|
-
refs.current.isNextPageLoading = false;
|
|
236
|
-
refs.current.hasNextPage =
|
|
237
|
-
newItems >= refs.current.queryPaging.batchSize;
|
|
238
|
-
|
|
239
|
-
if (pageAdd === 0) {
|
|
240
|
-
// New items
|
|
241
|
-
const newRows = refs.current.lastLoadedItems
|
|
242
|
-
? [...rows]
|
|
243
|
-
.splice(
|
|
244
|
-
rows.length - refs.current.lastLoadedItems,
|
|
245
|
-
refs.current.lastLoadedItems
|
|
246
|
-
)
|
|
247
|
-
.concat(result)
|
|
248
|
-
: result;
|
|
249
|
-
|
|
250
|
-
refs.current.idCache = {};
|
|
251
|
-
for (const row of newRows) {
|
|
252
|
-
const id = row[idField] as any;
|
|
253
|
-
refs.current.idCache[id] = null;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Update rows
|
|
257
|
-
setRows(newRows);
|
|
258
|
-
} else {
|
|
259
|
-
// Set current page
|
|
260
|
-
refs.current.queryPaging.currentPage += pageAdd;
|
|
261
|
-
|
|
262
|
-
// Update rows, avoid duplicate items
|
|
263
|
-
const newRows = [...rows];
|
|
264
|
-
|
|
265
|
-
for (const item of result) {
|
|
266
|
-
const id = item[idField] as any;
|
|
267
|
-
if (refs.current.idCache[id] === undefined) {
|
|
268
|
-
newRows.push(item);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
setRows(newRows);
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
// Item renderer
|
|
278
|
-
const itemRendererLocal = (
|
|
279
|
-
itemProps: GridChildComponentProps<T>,
|
|
280
|
-
state: GridLoaderStates<T>
|
|
281
|
-
) => {
|
|
282
|
-
// Custom render
|
|
283
|
-
const data =
|
|
284
|
-
itemProps.rowIndex < rows.length
|
|
285
|
-
? rows[itemProps.rowIndex]
|
|
286
|
-
: undefined;
|
|
287
|
-
return itemRenderer({
|
|
288
|
-
...itemProps,
|
|
289
|
-
data,
|
|
290
|
-
selectedItems: state.selectedItems,
|
|
291
|
-
setItems: (
|
|
292
|
-
callback: (
|
|
293
|
-
items: T[],
|
|
294
|
-
ref: ScrollerGridForwardRef<T>
|
|
295
|
-
) => T[] | undefined | void
|
|
296
|
-
) => {
|
|
297
|
-
const result = callback(rows, instance);
|
|
298
|
-
if (result == null) return;
|
|
299
|
-
setRows(result);
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
};
|
|
214
|
+
loadData(loadProps).then((result) => {
|
|
215
|
+
if (result == null || refs.current.isMounted === false) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
refs.current.isMounted = true;
|
|
219
|
+
|
|
220
|
+
const newItems = result.length;
|
|
221
|
+
refs.current.lastLoadedItems = newItems;
|
|
222
|
+
refs.current.isNextPageLoading = false;
|
|
223
|
+
refs.current.hasNextPage = newItems >= refs.current.queryPaging.batchSize;
|
|
224
|
+
|
|
225
|
+
if (pageAdd === 0) {
|
|
226
|
+
// New items
|
|
227
|
+
const newRows = refs.current.lastLoadedItems
|
|
228
|
+
? [...rows]
|
|
229
|
+
.splice(
|
|
230
|
+
rows.length - refs.current.lastLoadedItems,
|
|
231
|
+
refs.current.lastLoadedItems
|
|
232
|
+
)
|
|
233
|
+
.concat(result)
|
|
234
|
+
: result;
|
|
235
|
+
|
|
236
|
+
refs.current.idCache = {};
|
|
237
|
+
for (const row of newRows) {
|
|
238
|
+
const id = row[idField] as any;
|
|
239
|
+
refs.current.idCache[id] = null;
|
|
240
|
+
}
|
|
303
241
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
242
|
+
// Update rows
|
|
243
|
+
setRows(newRows);
|
|
244
|
+
} else {
|
|
245
|
+
// Set current page
|
|
246
|
+
if (refs.current.queryPaging.currentPage == null)
|
|
247
|
+
refs.current.queryPaging.currentPage = pageAdd;
|
|
248
|
+
else refs.current.queryPaging.currentPage += pageAdd;
|
|
249
|
+
|
|
250
|
+
// Update rows, avoid duplicate items
|
|
251
|
+
const newRows = [...rows];
|
|
252
|
+
|
|
253
|
+
for (const item of result) {
|
|
254
|
+
const id = item[idField] as any;
|
|
255
|
+
if (refs.current.idCache[id] === undefined) {
|
|
256
|
+
newRows.push(item);
|
|
257
|
+
}
|
|
314
258
|
}
|
|
315
259
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
};
|
|
260
|
+
setRows(newRows);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// Item renderer
|
|
266
|
+
const itemRendererLocal = (
|
|
267
|
+
itemProps: GridChildComponentProps<T>,
|
|
268
|
+
state: GridLoaderStates<T>
|
|
269
|
+
) => {
|
|
270
|
+
// Custom render
|
|
271
|
+
const data =
|
|
272
|
+
itemProps.rowIndex < rows.length ? rows[itemProps.rowIndex] : undefined;
|
|
273
|
+
return itemRenderer({
|
|
274
|
+
...itemProps,
|
|
275
|
+
data,
|
|
276
|
+
selectedItems: state.selectedItems,
|
|
277
|
+
setItems: (
|
|
278
|
+
callback: (
|
|
279
|
+
items: T[],
|
|
280
|
+
ref: ScrollerGridForwardRef<T>
|
|
281
|
+
) => T[] | undefined | void
|
|
282
|
+
) => {
|
|
283
|
+
const result = callback(rows, instance);
|
|
284
|
+
if (result == null) return;
|
|
285
|
+
setRows(result);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// Local items renderer callback
|
|
291
|
+
const onItemsRenderedLocal = (props: GridOnItemsRenderedProps) => {
|
|
292
|
+
// No items, means no necessary to load more data during reset
|
|
293
|
+
const itemCount = rows.length;
|
|
294
|
+
if (itemCount > 0 && props.visibleRowStopIndex + threshold > itemCount) {
|
|
295
|
+
// Auto load next page
|
|
296
|
+
loadDataLocal();
|
|
297
|
+
}
|
|
319
298
|
|
|
320
|
-
//
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
...queryPaging
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
// Reset items
|
|
338
|
-
if (refs.current.isMounted !== false) setRows(items, true);
|
|
299
|
+
// Custom
|
|
300
|
+
if (onItemsRendered) onItemsRendered(props);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// Reset the state and load again
|
|
304
|
+
const reset = (add?: GridLoaderPartialStates<T>, items: T[] = []) => {
|
|
305
|
+
const { queryPaging, ...rest } = add ?? {};
|
|
306
|
+
const resetState: GridLoaderPartialStates<T> = {
|
|
307
|
+
autoLoad: true,
|
|
308
|
+
loadedItems: 0,
|
|
309
|
+
hasNextPage: true,
|
|
310
|
+
isNextPageLoading: false,
|
|
311
|
+
lastLoadedItems: undefined,
|
|
312
|
+
...rest
|
|
339
313
|
};
|
|
314
|
+
Object.assign(refs.current, resetState);
|
|
315
|
+
Object.assign(refs.current.queryPaging, {
|
|
316
|
+
currentPage: 0,
|
|
317
|
+
...queryPaging
|
|
318
|
+
});
|
|
340
319
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
},
|
|
397
|
-
selectItem(item: T, checked: boolean) {
|
|
398
|
-
const selectedItems = refs.current.selectedItems;
|
|
399
|
-
const index = selectedItems.findIndex(
|
|
400
|
-
(selectedItem) => selectedItem[idField] === item[idField]
|
|
401
|
-
);
|
|
402
|
-
|
|
403
|
-
if (checked) {
|
|
404
|
-
if (index === -1) selectedItems.push(item);
|
|
405
|
-
} else {
|
|
406
|
-
if (index !== -1) selectedItems.splice(index, 1);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
if (onSelectChange) onSelectChange(selectedItems);
|
|
410
|
-
},
|
|
411
|
-
reset,
|
|
412
|
-
resetAfterColumnIndex(index: number, shouldForceUpdate?: boolean) {
|
|
413
|
-
ref.current?.resetAfterColumnIndex(index, shouldForceUpdate);
|
|
414
|
-
},
|
|
415
|
-
resetAfterIndices(params: {
|
|
416
|
-
columnIndex: number;
|
|
417
|
-
rowIndex: number;
|
|
418
|
-
shouldForceUpdate?: boolean | undefined;
|
|
419
|
-
}) {
|
|
420
|
-
ref.current?.resetAfterIndices(params);
|
|
421
|
-
},
|
|
422
|
-
resetAfterRowIndex(index: number, shouldForceUpdate?: boolean) {
|
|
423
|
-
ref.current?.resetAfterRowIndex(index, shouldForceUpdate);
|
|
320
|
+
// Reset items
|
|
321
|
+
if (refs.current.isMounted !== false) setRows(items, true);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const instance: ScrollerGridForwardRef<T> = {
|
|
325
|
+
delete(index) {
|
|
326
|
+
const item = rows.at(index);
|
|
327
|
+
if (item) {
|
|
328
|
+
const newRows = [...rows];
|
|
329
|
+
newRows.splice(index, 1);
|
|
330
|
+
setRows(newRows);
|
|
331
|
+
}
|
|
332
|
+
return item;
|
|
333
|
+
},
|
|
334
|
+
insert(item, start) {
|
|
335
|
+
const newRows = [...rows];
|
|
336
|
+
newRows.splice(start, 0, item);
|
|
337
|
+
setRows(newRows);
|
|
338
|
+
},
|
|
339
|
+
scrollTo(params: { scrollLeft: number; scrollTop: number }) {
|
|
340
|
+
ref.current?.scrollTo(params);
|
|
341
|
+
},
|
|
342
|
+
scrollToItem(params: {
|
|
343
|
+
align?: Align | undefined;
|
|
344
|
+
columnIndex?: number | undefined;
|
|
345
|
+
rowIndex?: number | undefined;
|
|
346
|
+
}) {
|
|
347
|
+
ref.current?.scrollToItem(params);
|
|
348
|
+
},
|
|
349
|
+
scrollToRef(scrollOffset: number): void {
|
|
350
|
+
ref.current?.scrollTo({ scrollLeft: 0, scrollTop: scrollOffset });
|
|
351
|
+
},
|
|
352
|
+
|
|
353
|
+
scrollToItemRef(index: number, align?: Align): void {
|
|
354
|
+
ref.current?.scrollToItem({ rowIndex: index, align });
|
|
355
|
+
},
|
|
356
|
+
select(rowIndex: number) {
|
|
357
|
+
// Select only one item
|
|
358
|
+
const selectedItems = refs.current.selectedItems;
|
|
359
|
+
selectedItems[0] = rows[rowIndex];
|
|
360
|
+
|
|
361
|
+
if (onSelectChange) onSelectChange(selectedItems);
|
|
362
|
+
},
|
|
363
|
+
selectAll(checked: boolean) {
|
|
364
|
+
const selectedItems = refs.current.selectedItems;
|
|
365
|
+
|
|
366
|
+
rows.forEach((row) => {
|
|
367
|
+
const index = selectedItems.findIndex(
|
|
368
|
+
(selectedItem) => selectedItem[idField] === row[idField]
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
if (checked) {
|
|
372
|
+
if (index === -1) selectedItems.push(row);
|
|
373
|
+
} else if (index !== -1) {
|
|
374
|
+
selectedItems.splice(index, 1);
|
|
424
375
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
},
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
if (onSelectChange) onSelectChange(selectedItems);
|
|
379
|
+
},
|
|
380
|
+
selectItem(item: T, checked: boolean) {
|
|
381
|
+
const selectedItems = refs.current.selectedItems;
|
|
382
|
+
const index = selectedItems.findIndex(
|
|
383
|
+
(selectedItem) => selectedItem[idField] === item[idField]
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
if (checked) {
|
|
387
|
+
if (index === -1) selectedItems.push(item);
|
|
388
|
+
} else {
|
|
389
|
+
if (index !== -1) selectedItems.splice(index, 1);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (onSelectChange) onSelectChange(selectedItems);
|
|
393
|
+
},
|
|
394
|
+
reset,
|
|
395
|
+
resetAfterColumnIndex(index: number, shouldForceUpdate?: boolean) {
|
|
396
|
+
ref.current?.resetAfterColumnIndex(index, shouldForceUpdate);
|
|
397
|
+
},
|
|
398
|
+
resetAfterIndices(params: {
|
|
399
|
+
columnIndex: number;
|
|
400
|
+
rowIndex: number;
|
|
401
|
+
shouldForceUpdate?: boolean | undefined;
|
|
402
|
+
}) {
|
|
403
|
+
ref.current?.resetAfterIndices(params);
|
|
404
|
+
},
|
|
405
|
+
resetAfterRowIndex(index: number, shouldForceUpdate?: boolean) {
|
|
406
|
+
ref.current?.resetAfterRowIndex(index, shouldForceUpdate);
|
|
456
407
|
}
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
React.useImperativeHandle(mRef, () => instance, [rows]);
|
|
457
411
|
|
|
458
|
-
|
|
459
|
-
return (
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
412
|
+
React.useEffect(() => {
|
|
413
|
+
return () => {
|
|
414
|
+
refs.current.isMounted = false;
|
|
415
|
+
};
|
|
416
|
+
}, []);
|
|
417
|
+
|
|
418
|
+
// Force update to work with the new width and rowHeight
|
|
419
|
+
React.useEffect(() => {
|
|
420
|
+
ref.current?.resetAfterIndices({
|
|
421
|
+
columnIndex: 0,
|
|
422
|
+
rowIndex: 0,
|
|
423
|
+
shouldForceUpdate: true
|
|
424
|
+
});
|
|
425
|
+
}, [width, rowHeight]);
|
|
426
|
+
|
|
427
|
+
// Rows
|
|
428
|
+
const rowLength = rows.length;
|
|
429
|
+
|
|
430
|
+
// Row count
|
|
431
|
+
const rowCount = refs.current.hasNextPage ? rowLength + 1 : rowLength;
|
|
432
|
+
|
|
433
|
+
// Auto load data when current page is 0
|
|
434
|
+
if (refs.current.queryPaging.currentPage === 0 && refs.current.autoLoad) {
|
|
435
|
+
const initItems = onInitLoad == null ? undefined : onInitLoad(ref.current);
|
|
436
|
+
if (initItems) reset(initItems[1], initItems[0]);
|
|
437
|
+
else loadDataLocal();
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Layout
|
|
441
|
+
return (
|
|
442
|
+
<React.Fragment>
|
|
443
|
+
{headerRenderer && headerRenderer(refs.current)}
|
|
444
|
+
<VariableSizeGrid<T>
|
|
445
|
+
itemKey={({ columnIndex, rowIndex, data }) => {
|
|
446
|
+
if (data == null) return [rowIndex, columnIndex].join(",");
|
|
447
|
+
// ${data[idField]}-${rowIndex} always unique but no cache for the same item
|
|
448
|
+
return [`${data[idField]}`, columnIndex].join(",");
|
|
449
|
+
}}
|
|
450
|
+
onItemsRendered={onItemsRenderedLocal}
|
|
451
|
+
ref={ref}
|
|
452
|
+
rowCount={rowCount}
|
|
453
|
+
rowHeight={
|
|
454
|
+
typeof rowHeight === "function" ? rowHeight : () => rowHeight
|
|
455
|
+
}
|
|
456
|
+
style={{ overflowX: "hidden" }}
|
|
457
|
+
width={width}
|
|
458
|
+
{...rest}
|
|
459
|
+
>
|
|
460
|
+
{(props) => itemRendererLocal(props, refs.current)}
|
|
461
|
+
</VariableSizeGrid>
|
|
462
|
+
{footerRenderer && footerRenderer(rows, refs.current)}
|
|
463
|
+
</React.Fragment>
|
|
464
|
+
);
|
|
485
465
|
};
|