@opencorestack/opengridx 0.1.7 → 0.1.9
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/CHANGELOG.md +31 -1
- package/dist/index.d.ts +6 -1
- package/dist/opengridx.css +1 -1
- package/dist/opengridx.es.js +3780 -3610
- package/dist/opengridx.es.js.map +1 -1
- package/dist/opengridx.umd.js +12 -12
- package/dist/opengridx.umd.js.map +1 -1
- package/lib/components/Cell/Cell.tsx +53 -55
- package/lib/components/DataGrid/DataGrid.smoke.test.tsx +58 -0
- package/lib/components/DataGrid/DataGrid.tsx +96 -656
- package/lib/components/FilterPanel/FilterPanel.tsx +46 -15
- package/lib/components/Row/Row.tsx +39 -38
- package/lib/components/Toolbar/GlobalSearch.css +0 -3
- package/lib/components/Toolbar/GlobalSearch.tsx +78 -31
- package/lib/components/Toolbar/GridToolbar.css +17 -0
- package/lib/components/Toolbar/GridToolbar.tsx +54 -28
- package/lib/components/Toolbar/PivotPanel.tsx +7 -2
- package/lib/components/ui/DragHandleIcon.tsx +1 -1
- package/lib/hooks/core/useDataGrid.ts +2 -2
- package/lib/hooks/core/useGridKeyboardNavigation.ts +371 -0
- package/lib/hooks/core/useLayout.ts +354 -0
- package/lib/hooks/features/useAggregation.test.ts +135 -0
- package/lib/hooks/features/useGridClipboard.ts +1 -1
- package/lib/hooks/features/useGridDataSource.ts +1 -1
- package/lib/hooks/features/usePivot.ts +25 -0
- package/lib/state/useGridStateStorage.ts +1 -1
- package/lib/utils/export/index.ts +1 -1
- package/lib/utils/filtering/filtering.test.ts +178 -0
- package/lib/utils/sorting/sorting.test.ts +88 -0
- package/package.json +10 -4
- package/lib/utils/virtualization/index.ts +0 -106
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
import type { GridRowModel, GridRowId, GridColDef, GridCellParams, GridSortItem, GridSortDirection } from '../../types';
|
|
3
|
+
import type { GridEditingState } from '../features/useGridEditing';
|
|
4
|
+
|
|
5
|
+
interface ColumnMetrics {
|
|
6
|
+
leftPinnedWidth: number;
|
|
7
|
+
rightPinnedWidth: number;
|
|
8
|
+
unpinnedAccWidths: number[];
|
|
9
|
+
unpinnedCols: { field: string }[];
|
|
10
|
+
totalSpecialsWidth: number;
|
|
11
|
+
pinnedSpecialsWidth: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface VirtualizationSnapshot {
|
|
15
|
+
cumulativeHeights: number[];
|
|
16
|
+
pinnedTopHeight: number;
|
|
17
|
+
columnMetrics: ColumnMetrics | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface UseGridKeyboardNavigationParams<R extends GridRowModel> {
|
|
21
|
+
allRenderableRows: R[];
|
|
22
|
+
navigationColumns: { field: string; editable?: boolean; sortable?: boolean }[];
|
|
23
|
+
checkboxSelection: boolean;
|
|
24
|
+
selectedRowIds: Set<GridRowId>;
|
|
25
|
+
handleSelectionChange: (id: GridRowId, selected: boolean) => void;
|
|
26
|
+
handleDetailPanelToggle: (id: GridRowId) => void;
|
|
27
|
+
editingHandlers: {
|
|
28
|
+
editingCell: GridEditingState['editingCell'];
|
|
29
|
+
startCellEdit: (params: { id: GridRowId; field: string; value: unknown }) => void;
|
|
30
|
+
stopCellEdit: (params?: { cancel?: boolean }) => void;
|
|
31
|
+
};
|
|
32
|
+
setKeyboardMode: (mode: boolean) => void;
|
|
33
|
+
sortModel: GridSortItem[];
|
|
34
|
+
handleSort: (field: string, direction: GridSortDirection) => void;
|
|
35
|
+
isCellEditable?: (params: GridCellParams<R>) => boolean;
|
|
36
|
+
pagination: boolean;
|
|
37
|
+
pageSize: number;
|
|
38
|
+
virtualization: VirtualizationSnapshot;
|
|
39
|
+
viewportRef: React.RefObject<HTMLDivElement | null>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface FocusedCell {
|
|
43
|
+
id: GridRowId;
|
|
44
|
+
field: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface UseGridKeyboardNavigationReturn {
|
|
48
|
+
focusedCell: FocusedCell | null;
|
|
49
|
+
setFocusedCell: React.Dispatch<React.SetStateAction<FocusedCell | null>>;
|
|
50
|
+
handleFocus: (event: React.FocusEvent<HTMLDivElement>) => void;
|
|
51
|
+
handleBlur: (event: React.FocusEvent<HTMLDivElement>) => void;
|
|
52
|
+
handleKeyDown: (event: React.KeyboardEvent) => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function useGridKeyboardNavigation<R extends GridRowModel>(
|
|
56
|
+
params: UseGridKeyboardNavigationParams<R>
|
|
57
|
+
): UseGridKeyboardNavigationReturn {
|
|
58
|
+
const {
|
|
59
|
+
allRenderableRows,
|
|
60
|
+
navigationColumns,
|
|
61
|
+
checkboxSelection,
|
|
62
|
+
selectedRowIds,
|
|
63
|
+
handleSelectionChange,
|
|
64
|
+
handleDetailPanelToggle,
|
|
65
|
+
editingHandlers,
|
|
66
|
+
setKeyboardMode,
|
|
67
|
+
sortModel,
|
|
68
|
+
handleSort,
|
|
69
|
+
isCellEditable,
|
|
70
|
+
pagination,
|
|
71
|
+
pageSize,
|
|
72
|
+
virtualization,
|
|
73
|
+
viewportRef,
|
|
74
|
+
} = params;
|
|
75
|
+
|
|
76
|
+
const [focusedCell, setFocusedCell] = useState<FocusedCell | null>(null);
|
|
77
|
+
|
|
78
|
+
const handleFocus = useCallback((event: React.FocusEvent<HTMLDivElement>) => {
|
|
79
|
+
if (event.target === event.currentTarget) {
|
|
80
|
+
setFocusedCell(prev => {
|
|
81
|
+
if (prev) return prev;
|
|
82
|
+
if (checkboxSelection) {
|
|
83
|
+
return { id: 'HEADER', field: '__checkbox_col__' };
|
|
84
|
+
}
|
|
85
|
+
const firstRow = allRenderableRows[0];
|
|
86
|
+
const firstCol = navigationColumns[0];
|
|
87
|
+
if (firstRow && firstCol) {
|
|
88
|
+
return { id: firstRow.id, field: firstCol.field };
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}, [allRenderableRows, navigationColumns, checkboxSelection]);
|
|
94
|
+
|
|
95
|
+
const handleBlur = useCallback((event: React.FocusEvent<HTMLDivElement>) => {
|
|
96
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node)) {
|
|
97
|
+
setFocusedCell(null);
|
|
98
|
+
}
|
|
99
|
+
}, []);
|
|
100
|
+
|
|
101
|
+
const handleKeyDown = useCallback((event: React.KeyboardEvent) => {
|
|
102
|
+
setKeyboardMode(true);
|
|
103
|
+
|
|
104
|
+
if (!focusedCell) return;
|
|
105
|
+
|
|
106
|
+
const { id, field } = focusedCell;
|
|
107
|
+
const isEditing = Boolean(editingHandlers.editingCell);
|
|
108
|
+
|
|
109
|
+
if (id === 'HEADER' && (event.key === 'Enter' || event.key === ' ')) {
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
const col = navigationColumns.find(c => c.field === field);
|
|
112
|
+
if (col && col.sortable !== false) {
|
|
113
|
+
const currentSort = sortModel.find(item => item.field === field);
|
|
114
|
+
let newDirection: GridSortDirection = 'asc';
|
|
115
|
+
if (currentSort) {
|
|
116
|
+
newDirection = currentSort.sort === 'asc' ? 'desc' : null;
|
|
117
|
+
}
|
|
118
|
+
handleSort(field, newDirection);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (event.key === 'Enter') {
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
if (isEditing) {
|
|
126
|
+
editingHandlers.stopCellEdit();
|
|
127
|
+
} else {
|
|
128
|
+
const col = navigationColumns.find(c => c.field === field);
|
|
129
|
+
if (col?.editable) {
|
|
130
|
+
const row = allRenderableRows.find(r => r.id === id);
|
|
131
|
+
if (row) {
|
|
132
|
+
editingHandlers.startCellEdit({ id, field, value: (row as R & Record<string, unknown>)[field] });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (event.key === ' ' || event.key === 'Spacebar') {
|
|
140
|
+
if (!isEditing) {
|
|
141
|
+
if (field === '__checkbox_col__') {
|
|
142
|
+
event.preventDefault();
|
|
143
|
+
handleSelectionChange(id, !selectedRowIds.has(id));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (field === '__expand_col__') {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
handleDetailPanelToggle(id);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (event.key === 'Escape') {
|
|
155
|
+
if (isEditing) {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
editingHandlers.stopCellEdit({ cancel: true });
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (isEditing && event.key !== 'Tab') return;
|
|
163
|
+
|
|
164
|
+
const isHeader = id === 'HEADER';
|
|
165
|
+
const rowIndex = isHeader ? -1 : allRenderableRows.findIndex(r => r.id === id);
|
|
166
|
+
const colIndex = navigationColumns.findIndex(c => c.field === field);
|
|
167
|
+
|
|
168
|
+
if (!isHeader && rowIndex === -1) return;
|
|
169
|
+
if (colIndex === -1) return;
|
|
170
|
+
|
|
171
|
+
const findNextCell = (
|
|
172
|
+
startRow: number,
|
|
173
|
+
startCol: number,
|
|
174
|
+
deltaRow: number,
|
|
175
|
+
deltaCol: number,
|
|
176
|
+
wrapRow: boolean,
|
|
177
|
+
allowHeader = false
|
|
178
|
+
) => {
|
|
179
|
+
let r = startRow + deltaRow;
|
|
180
|
+
let c = startCol + deltaCol;
|
|
181
|
+
|
|
182
|
+
if (wrapRow && deltaCol !== 0) {
|
|
183
|
+
if (deltaCol > 0 && c >= navigationColumns.length) { c = 0; r++; }
|
|
184
|
+
else if (deltaCol < 0 && c < 0) { c = navigationColumns.length - 1; r--; }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!wrapRow && (c < 0 || c >= navigationColumns.length)) return null;
|
|
188
|
+
if (r < (allowHeader ? -1 : 0) || r >= allRenderableRows.length) return null;
|
|
189
|
+
|
|
190
|
+
return { r, c };
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const findNextEditable = (
|
|
194
|
+
startRow: number,
|
|
195
|
+
startCol: number,
|
|
196
|
+
deltaRow: number,
|
|
197
|
+
deltaCol: number,
|
|
198
|
+
wrapRow: boolean,
|
|
199
|
+
allowHeader = false
|
|
200
|
+
) => {
|
|
201
|
+
let r = startRow;
|
|
202
|
+
let c = startCol;
|
|
203
|
+
let steps = 0;
|
|
204
|
+
const maxSteps = allRenderableRows.length * navigationColumns.length + navigationColumns.length;
|
|
205
|
+
|
|
206
|
+
while (steps < maxSteps) {
|
|
207
|
+
steps++;
|
|
208
|
+
r += deltaRow;
|
|
209
|
+
c += deltaCol;
|
|
210
|
+
|
|
211
|
+
if (wrapRow && deltaCol !== 0) {
|
|
212
|
+
if (deltaCol > 0) {
|
|
213
|
+
if (c >= navigationColumns.length) { c = 0; r++; }
|
|
214
|
+
} else {
|
|
215
|
+
if (c < 0) { c = navigationColumns.length - 1; r--; }
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
if (c < 0 || c >= navigationColumns.length) return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (r < -1 || r >= allRenderableRows.length) return null;
|
|
222
|
+
|
|
223
|
+
if (r === -1) {
|
|
224
|
+
if (!allowHeader) continue;
|
|
225
|
+
const col = navigationColumns[c];
|
|
226
|
+
const isSortable = col.sortable !== false;
|
|
227
|
+
if (['__checkbox_col__'].includes(col.field) || isSortable) return { r, c };
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const row = allRenderableRows[r];
|
|
232
|
+
const col = navigationColumns[c];
|
|
233
|
+
const isInteractable = ['__checkbox_col__', '__expand_col__', '__reorder_col__'].includes(col.field);
|
|
234
|
+
let cellEditable = col.editable || isInteractable;
|
|
235
|
+
|
|
236
|
+
if (isCellEditable && !isInteractable) {
|
|
237
|
+
try {
|
|
238
|
+
cellEditable = isCellEditable({
|
|
239
|
+
row,
|
|
240
|
+
field: col.field,
|
|
241
|
+
value: (row as R & Record<string, unknown>)[col.field],
|
|
242
|
+
colDef: col as GridColDef<R>,
|
|
243
|
+
rowIndex: r,
|
|
244
|
+
colIndex: c
|
|
245
|
+
});
|
|
246
|
+
} catch {
|
|
247
|
+
// isCellEditable threw — treat cell as non-editable
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (cellEditable) return { r, c };
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
let nextRowIndex = rowIndex;
|
|
257
|
+
let nextColIndex = colIndex;
|
|
258
|
+
let handled = false;
|
|
259
|
+
|
|
260
|
+
if (event.key === 'Tab') {
|
|
261
|
+
const dir = event.shiftKey ? -1 : 1;
|
|
262
|
+
const res = findNextEditable(rowIndex, colIndex, 0, dir, true, false);
|
|
263
|
+
if (res) { nextRowIndex = res.r; nextColIndex = res.c; handled = true; }
|
|
264
|
+
} else if (event.key === 'ArrowRight') {
|
|
265
|
+
const res = findNextCell(rowIndex, colIndex, 0, 1, true, true);
|
|
266
|
+
if (res) { nextRowIndex = res.r; nextColIndex = res.c; handled = true; }
|
|
267
|
+
} else if (event.key === 'ArrowLeft') {
|
|
268
|
+
const res = findNextCell(rowIndex, colIndex, 0, -1, true, true);
|
|
269
|
+
if (res) { nextRowIndex = res.r; nextColIndex = res.c; handled = true; }
|
|
270
|
+
} else if (event.key === 'ArrowDown') {
|
|
271
|
+
const res = findNextCell(rowIndex, colIndex, 1, 0, false, true);
|
|
272
|
+
if (res) { nextRowIndex = res.r; nextColIndex = res.c; handled = true; }
|
|
273
|
+
} else if (event.key === 'ArrowUp') {
|
|
274
|
+
const res = findNextCell(rowIndex, colIndex, -1, 0, false, true);
|
|
275
|
+
if (res) { nextRowIndex = res.r; nextColIndex = res.c; handled = true; }
|
|
276
|
+
} else if (event.key === 'Home') {
|
|
277
|
+
nextColIndex = 0;
|
|
278
|
+
if (event.ctrlKey || event.metaKey) nextRowIndex = -1;
|
|
279
|
+
handled = true;
|
|
280
|
+
} else if (event.key === 'End') {
|
|
281
|
+
nextColIndex = navigationColumns.length - 1;
|
|
282
|
+
if (event.ctrlKey || event.metaKey) nextRowIndex = allRenderableRows.length - 1;
|
|
283
|
+
handled = true;
|
|
284
|
+
} else if (event.key === 'PageUp') {
|
|
285
|
+
const pg = pagination ? pageSize : 10;
|
|
286
|
+
nextRowIndex = Math.max(-1, nextRowIndex - pg);
|
|
287
|
+
handled = true;
|
|
288
|
+
} else if (event.key === 'PageDown') {
|
|
289
|
+
const pg = pagination ? pageSize : 10;
|
|
290
|
+
nextRowIndex = Math.min(allRenderableRows.length - 1, nextRowIndex + pg);
|
|
291
|
+
handled = true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (handled) {
|
|
295
|
+
event.preventDefault();
|
|
296
|
+
|
|
297
|
+
if (isEditing && event.key === 'Tab') {
|
|
298
|
+
editingHandlers.stopCellEdit();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (nextRowIndex >= -1 && nextRowIndex < allRenderableRows.length &&
|
|
302
|
+
nextColIndex >= 0 && nextColIndex < navigationColumns.length) {
|
|
303
|
+
|
|
304
|
+
const nextCol = navigationColumns[nextColIndex];
|
|
305
|
+
|
|
306
|
+
if (nextRowIndex === -1) {
|
|
307
|
+
setFocusedCell({ id: 'HEADER', field: nextCol.field });
|
|
308
|
+
} else {
|
|
309
|
+
setFocusedCell({ id: allRenderableRows[nextRowIndex].id, field: nextCol.field });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const el = viewportRef.current;
|
|
313
|
+
if (el) {
|
|
314
|
+
const { cumulativeHeights, pinnedTopHeight, columnMetrics } = virtualization;
|
|
315
|
+
const { clientHeight, clientWidth, scrollTop, scrollLeft } = el;
|
|
316
|
+
|
|
317
|
+
const rowTop = nextRowIndex <= 0 ? 0 : cumulativeHeights[nextRowIndex - 1];
|
|
318
|
+
const rowBottom = cumulativeHeights[nextRowIndex];
|
|
319
|
+
const visibleTop = scrollTop + pinnedTopHeight;
|
|
320
|
+
const visibleBottom = scrollTop + clientHeight;
|
|
321
|
+
|
|
322
|
+
let newScrollTop = scrollTop;
|
|
323
|
+
if (rowTop < visibleTop) {
|
|
324
|
+
newScrollTop = Math.max(0, rowTop - pinnedTopHeight);
|
|
325
|
+
} else if (rowBottom > visibleBottom) {
|
|
326
|
+
newScrollTop = rowBottom - clientHeight;
|
|
327
|
+
}
|
|
328
|
+
if (newScrollTop !== scrollTop) el.scrollTop = newScrollTop;
|
|
329
|
+
|
|
330
|
+
if (columnMetrics) {
|
|
331
|
+
const { leftPinnedWidth, rightPinnedWidth, unpinnedAccWidths, unpinnedCols, totalSpecialsWidth, pinnedSpecialsWidth } = columnMetrics;
|
|
332
|
+
const unpinnedIndex = unpinnedCols.findIndex(c => c.field === nextCol.field);
|
|
333
|
+
|
|
334
|
+
if (unpinnedIndex !== -1) {
|
|
335
|
+
const colLeft = totalSpecialsWidth + leftPinnedWidth + (unpinnedIndex > 0 ? unpinnedAccWidths[unpinnedIndex - 1] : 0);
|
|
336
|
+
const colRight = totalSpecialsWidth + leftPinnedWidth + unpinnedAccWidths[unpinnedIndex];
|
|
337
|
+
const visibleStart = scrollLeft + pinnedSpecialsWidth + leftPinnedWidth;
|
|
338
|
+
const visibleEnd = scrollLeft + clientWidth - rightPinnedWidth;
|
|
339
|
+
|
|
340
|
+
let newScrollLeft = scrollLeft;
|
|
341
|
+
if (colLeft < visibleStart) {
|
|
342
|
+
newScrollLeft = Math.max(0, colLeft - pinnedSpecialsWidth - leftPinnedWidth);
|
|
343
|
+
} else if (colRight > visibleEnd) {
|
|
344
|
+
newScrollLeft = colRight - clientWidth + rightPinnedWidth;
|
|
345
|
+
}
|
|
346
|
+
if (newScrollLeft !== scrollLeft) el.scrollLeft = newScrollLeft;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}, [
|
|
353
|
+
focusedCell,
|
|
354
|
+
allRenderableRows,
|
|
355
|
+
navigationColumns,
|
|
356
|
+
editingHandlers,
|
|
357
|
+
virtualization,
|
|
358
|
+
selectedRowIds,
|
|
359
|
+
handleSelectionChange,
|
|
360
|
+
handleDetailPanelToggle,
|
|
361
|
+
setKeyboardMode,
|
|
362
|
+
sortModel,
|
|
363
|
+
handleSort,
|
|
364
|
+
isCellEditable,
|
|
365
|
+
pagination,
|
|
366
|
+
pageSize,
|
|
367
|
+
viewportRef,
|
|
368
|
+
]);
|
|
369
|
+
|
|
370
|
+
return { focusedCell, setFocusedCell, handleFocus, handleBlur, handleKeyDown };
|
|
371
|
+
}
|