@alaarab/ogrid-core 1.0.0 → 1.2.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.
Files changed (72) hide show
  1. package/README.md +12 -0
  2. package/dist/esm/components/GridContextMenu.js +25 -0
  3. package/dist/esm/components/OGridLayout.js +16 -0
  4. package/dist/esm/components/StatusBar.js +6 -0
  5. package/dist/esm/hooks/index.js +16 -0
  6. package/dist/esm/hooks/useActiveCell.js +28 -0
  7. package/dist/esm/hooks/useCellEditing.js +11 -0
  8. package/dist/esm/hooks/useCellSelection.js +86 -0
  9. package/dist/esm/hooks/useClipboard.js +129 -0
  10. package/dist/esm/hooks/useColumnChooserState.js +62 -0
  11. package/dist/esm/hooks/useColumnHeaderFilterState.js +207 -0
  12. package/dist/esm/hooks/useColumnResize.js +48 -0
  13. package/dist/esm/hooks/useContextMenu.js +16 -0
  14. package/dist/esm/hooks/useDataGridState.js +273 -0
  15. package/dist/esm/hooks/useDebounce.js +35 -0
  16. package/dist/esm/hooks/useFillHandle.js +93 -0
  17. package/dist/esm/hooks/useInlineCellEditorState.js +42 -0
  18. package/dist/esm/hooks/useKeyboardNavigation.js +364 -0
  19. package/dist/esm/hooks/useOGrid.js +356 -0
  20. package/dist/esm/hooks/useRowSelection.js +68 -0
  21. package/dist/esm/hooks/useUndoRedo.js +52 -0
  22. package/dist/esm/index.js +7 -3
  23. package/dist/esm/storybook/index.js +1 -0
  24. package/dist/esm/storybook/mockData.js +73 -0
  25. package/dist/esm/types/dataGridTypes.js +17 -0
  26. package/dist/esm/types/index.js +1 -1
  27. package/dist/esm/utils/cellValue.js +8 -0
  28. package/dist/esm/utils/columnUtils.js +19 -0
  29. package/dist/esm/utils/dataGridStatusBar.js +15 -0
  30. package/dist/esm/utils/dataGridViewModel.js +121 -0
  31. package/dist/esm/utils/exportToCsv.js +6 -1
  32. package/dist/esm/utils/gridContextMenuHelpers.js +31 -0
  33. package/dist/esm/utils/index.js +8 -0
  34. package/dist/esm/utils/ogridHelpers.js +49 -0
  35. package/dist/esm/utils/paginationHelpers.js +52 -0
  36. package/dist/esm/utils/statusBarHelpers.js +18 -0
  37. package/dist/types/components/GridContextMenu.d.ts +14 -0
  38. package/dist/types/components/OGridLayout.d.ts +25 -0
  39. package/dist/types/components/StatusBar.d.ts +15 -0
  40. package/dist/types/hooks/index.d.ts +31 -0
  41. package/dist/types/hooks/useActiveCell.d.ts +13 -0
  42. package/dist/types/hooks/useCellEditing.d.ts +12 -0
  43. package/dist/types/hooks/useCellSelection.d.ts +14 -0
  44. package/dist/types/hooks/useClipboard.d.ts +18 -0
  45. package/dist/types/hooks/useColumnChooserState.d.ts +27 -0
  46. package/dist/types/hooks/useColumnHeaderFilterState.d.ts +64 -0
  47. package/dist/types/hooks/useColumnResize.d.ts +16 -0
  48. package/dist/types/hooks/useContextMenu.d.ts +14 -0
  49. package/dist/types/hooks/useDataGridState.d.ts +95 -0
  50. package/dist/types/hooks/useDebounce.d.ts +9 -0
  51. package/dist/types/hooks/useFillHandle.d.ts +25 -0
  52. package/dist/types/hooks/useInlineCellEditorState.d.ts +24 -0
  53. package/dist/types/hooks/useKeyboardNavigation.d.ts +33 -0
  54. package/dist/types/hooks/useOGrid.d.ts +24 -0
  55. package/dist/types/hooks/useRowSelection.d.ts +17 -0
  56. package/dist/types/hooks/useUndoRedo.d.ts +17 -0
  57. package/dist/types/index.d.ts +12 -6
  58. package/dist/types/storybook/index.d.ts +2 -0
  59. package/dist/types/storybook/mockData.d.ts +37 -0
  60. package/dist/types/types/columnTypes.d.ts +49 -1
  61. package/dist/types/types/dataGridTypes.d.ts +173 -4
  62. package/dist/types/types/index.d.ts +3 -3
  63. package/dist/types/utils/cellValue.d.ts +5 -0
  64. package/dist/types/utils/columnUtils.d.ts +6 -0
  65. package/dist/types/utils/dataGridStatusBar.d.ts +6 -0
  66. package/dist/types/utils/dataGridViewModel.d.ts +93 -0
  67. package/dist/types/utils/gridContextMenuHelpers.d.ts +23 -0
  68. package/dist/types/utils/index.d.ts +12 -0
  69. package/dist/types/utils/ogridHelpers.d.ts +9 -0
  70. package/dist/types/utils/paginationHelpers.d.ts +23 -0
  71. package/dist/types/utils/statusBarHelpers.d.ts +18 -0
  72. package/package.json +43 -32
@@ -0,0 +1,364 @@
1
+ import { useCallback } from 'react';
2
+ import { normalizeSelectionRange } from '../types';
3
+ import { getCellValue } from '../utils';
4
+ export function useKeyboardNavigation(params) {
5
+ const { items, visibleCols, colOffset, hasCheckboxCol, visibleColumnCount, activeCell, setActiveCell, selectionRange, setSelectionRange, editable, onCellValueChanged, getRowId, editingCell, setEditingCell, rowSelection, selectedRowIds, handleRowCheckboxChange, handleCopy, handleCut, handlePaste, setContextMenu, wrapperRef, onUndo, onRedo, } = params;
6
+ const maxRowIndex = items.length - 1;
7
+ const maxColIndex = visibleColumnCount - 1 + colOffset;
8
+ const handleGridKeyDown = useCallback((e) => {
9
+ if (items.length === 0)
10
+ return;
11
+ if (activeCell === null) {
12
+ if ([
13
+ 'ArrowDown',
14
+ 'ArrowUp',
15
+ 'ArrowLeft',
16
+ 'ArrowRight',
17
+ 'Tab',
18
+ 'Enter',
19
+ 'Home',
20
+ 'End',
21
+ ].includes(e.key)) {
22
+ setActiveCell({ rowIndex: 0, columnIndex: colOffset });
23
+ e.preventDefault();
24
+ }
25
+ return;
26
+ }
27
+ const { rowIndex, columnIndex } = activeCell;
28
+ const dataColIndex = columnIndex - colOffset;
29
+ const shift = e.shiftKey;
30
+ switch (e.key) {
31
+ case 'c':
32
+ if (e.ctrlKey || e.metaKey) {
33
+ e.preventDefault();
34
+ handleCopy();
35
+ }
36
+ break;
37
+ case 'x':
38
+ if (e.ctrlKey || e.metaKey) {
39
+ e.preventDefault();
40
+ handleCut();
41
+ }
42
+ break;
43
+ case 'v':
44
+ if (e.ctrlKey || e.metaKey) {
45
+ e.preventDefault();
46
+ void handlePaste();
47
+ }
48
+ break;
49
+ case 'ArrowDown': {
50
+ e.preventDefault();
51
+ const newRow = Math.min(rowIndex + 1, maxRowIndex);
52
+ if (shift) {
53
+ setSelectionRange(normalizeSelectionRange({
54
+ startRow: selectionRange?.startRow ?? rowIndex,
55
+ startCol: selectionRange?.startCol ?? dataColIndex,
56
+ endRow: newRow,
57
+ endCol: selectionRange?.endCol ?? dataColIndex,
58
+ }));
59
+ }
60
+ else {
61
+ setSelectionRange({
62
+ startRow: newRow,
63
+ startCol: dataColIndex,
64
+ endRow: newRow,
65
+ endCol: dataColIndex,
66
+ });
67
+ }
68
+ setActiveCell({ rowIndex: newRow, columnIndex });
69
+ break;
70
+ }
71
+ case 'ArrowUp': {
72
+ e.preventDefault();
73
+ const newRowUp = Math.max(rowIndex - 1, 0);
74
+ if (shift) {
75
+ setSelectionRange(normalizeSelectionRange({
76
+ startRow: selectionRange?.startRow ?? rowIndex,
77
+ startCol: selectionRange?.startCol ?? dataColIndex,
78
+ endRow: newRowUp,
79
+ endCol: selectionRange?.endCol ?? dataColIndex,
80
+ }));
81
+ }
82
+ else {
83
+ setSelectionRange({
84
+ startRow: newRowUp,
85
+ startCol: dataColIndex,
86
+ endRow: newRowUp,
87
+ endCol: dataColIndex,
88
+ });
89
+ }
90
+ setActiveCell({ rowIndex: newRowUp, columnIndex });
91
+ break;
92
+ }
93
+ case 'ArrowRight': {
94
+ e.preventDefault();
95
+ const newCol = Math.min(columnIndex + 1, maxColIndex);
96
+ const newDataCol = newCol - colOffset;
97
+ if (shift) {
98
+ setSelectionRange(normalizeSelectionRange({
99
+ startRow: selectionRange?.startRow ?? rowIndex,
100
+ startCol: selectionRange?.startCol ?? dataColIndex,
101
+ endRow: selectionRange?.endRow ?? rowIndex,
102
+ endCol: newDataCol,
103
+ }));
104
+ }
105
+ else {
106
+ setSelectionRange({
107
+ startRow: rowIndex,
108
+ startCol: newDataCol,
109
+ endRow: rowIndex,
110
+ endCol: newDataCol,
111
+ });
112
+ }
113
+ setActiveCell({ rowIndex, columnIndex: newCol });
114
+ break;
115
+ }
116
+ case 'ArrowLeft': {
117
+ e.preventDefault();
118
+ const newColLeft = Math.max(columnIndex - 1, colOffset);
119
+ const newDataColLeft = newColLeft - colOffset;
120
+ if (shift) {
121
+ setSelectionRange(normalizeSelectionRange({
122
+ startRow: selectionRange?.startRow ?? rowIndex,
123
+ startCol: selectionRange?.startCol ?? dataColIndex,
124
+ endRow: selectionRange?.endRow ?? rowIndex,
125
+ endCol: newDataColLeft,
126
+ }));
127
+ }
128
+ else {
129
+ setSelectionRange({
130
+ startRow: rowIndex,
131
+ startCol: newDataColLeft,
132
+ endRow: rowIndex,
133
+ endCol: newDataColLeft,
134
+ });
135
+ }
136
+ setActiveCell({ rowIndex, columnIndex: newColLeft });
137
+ break;
138
+ }
139
+ case 'Tab': {
140
+ e.preventDefault();
141
+ let newRowTab = rowIndex;
142
+ let newColTab = columnIndex;
143
+ if (e.shiftKey) {
144
+ if (columnIndex > colOffset) {
145
+ newColTab = columnIndex - 1;
146
+ }
147
+ else if (rowIndex > 0) {
148
+ newRowTab = rowIndex - 1;
149
+ newColTab = maxColIndex;
150
+ }
151
+ }
152
+ else {
153
+ if (columnIndex < maxColIndex) {
154
+ newColTab = columnIndex + 1;
155
+ }
156
+ else if (rowIndex < maxRowIndex) {
157
+ newRowTab = rowIndex + 1;
158
+ newColTab = colOffset;
159
+ }
160
+ }
161
+ const newDataColTab = newColTab - colOffset;
162
+ setSelectionRange({
163
+ startRow: newRowTab,
164
+ startCol: newDataColTab,
165
+ endRow: newRowTab,
166
+ endCol: newDataColTab,
167
+ });
168
+ setActiveCell({ rowIndex: newRowTab, columnIndex: newColTab });
169
+ break;
170
+ }
171
+ case 'Home': {
172
+ e.preventDefault();
173
+ const newRowHome = e.ctrlKey ? 0 : rowIndex;
174
+ setSelectionRange({
175
+ startRow: newRowHome,
176
+ startCol: 0,
177
+ endRow: newRowHome,
178
+ endCol: 0,
179
+ });
180
+ setActiveCell({ rowIndex: newRowHome, columnIndex: colOffset });
181
+ break;
182
+ }
183
+ case 'End': {
184
+ e.preventDefault();
185
+ const newRowEnd = e.ctrlKey ? maxRowIndex : rowIndex;
186
+ setSelectionRange({
187
+ startRow: newRowEnd,
188
+ startCol: visibleColumnCount - 1,
189
+ endRow: newRowEnd,
190
+ endCol: visibleColumnCount - 1,
191
+ });
192
+ setActiveCell({ rowIndex: newRowEnd, columnIndex: maxColIndex });
193
+ break;
194
+ }
195
+ case 'Enter':
196
+ case 'F2': {
197
+ e.preventDefault();
198
+ if (dataColIndex >= 0 && dataColIndex < visibleCols.length) {
199
+ const col = visibleCols[dataColIndex];
200
+ const item = items[rowIndex];
201
+ if (item && col) {
202
+ const colEditable = col.editable === true ||
203
+ (typeof col.editable === 'function' && col.editable(item));
204
+ if (editable !== false &&
205
+ colEditable &&
206
+ onCellValueChanged != null) {
207
+ setEditingCell({ rowId: getRowId(item), columnId: col.columnId });
208
+ }
209
+ }
210
+ }
211
+ break;
212
+ }
213
+ case 'Escape':
214
+ e.preventDefault();
215
+ if (editingCell != null) {
216
+ setEditingCell(null);
217
+ }
218
+ else {
219
+ setActiveCell(null);
220
+ setSelectionRange(null);
221
+ }
222
+ break;
223
+ case ' ':
224
+ if (rowSelection !== 'none' &&
225
+ columnIndex === 0 &&
226
+ hasCheckboxCol) {
227
+ e.preventDefault();
228
+ const item = items[rowIndex];
229
+ if (item) {
230
+ const id = getRowId(item);
231
+ const isSelected = selectedRowIds.has(id);
232
+ handleRowCheckboxChange(id, !isSelected, rowIndex, e.shiftKey);
233
+ }
234
+ }
235
+ break;
236
+ case 'z':
237
+ if (e.ctrlKey || e.metaKey) {
238
+ if (editingCell == null) {
239
+ if (e.shiftKey && onRedo) {
240
+ e.preventDefault();
241
+ onRedo();
242
+ }
243
+ else if (!e.shiftKey && onUndo) {
244
+ e.preventDefault();
245
+ onUndo();
246
+ }
247
+ }
248
+ }
249
+ break;
250
+ case 'y':
251
+ if (e.ctrlKey || e.metaKey) {
252
+ if (editingCell == null && onRedo) {
253
+ e.preventDefault();
254
+ onRedo();
255
+ }
256
+ }
257
+ break;
258
+ case 'a':
259
+ if (e.ctrlKey || e.metaKey) {
260
+ e.preventDefault();
261
+ if (items.length > 0 && visibleColumnCount > 0) {
262
+ setSelectionRange({
263
+ startRow: 0,
264
+ startCol: 0,
265
+ endRow: items.length - 1,
266
+ endCol: visibleColumnCount - 1,
267
+ });
268
+ setActiveCell({ rowIndex: 0, columnIndex: colOffset });
269
+ }
270
+ }
271
+ break;
272
+ case 'Delete':
273
+ case 'Backspace': {
274
+ if (editingCell != null)
275
+ break;
276
+ if (onCellValueChanged == null)
277
+ break;
278
+ const range = selectionRange ??
279
+ (activeCell != null
280
+ ? {
281
+ startRow: activeCell.rowIndex,
282
+ startCol: activeCell.columnIndex - colOffset,
283
+ endRow: activeCell.rowIndex,
284
+ endCol: activeCell.columnIndex - colOffset,
285
+ }
286
+ : null);
287
+ if (range == null)
288
+ break;
289
+ e.preventDefault();
290
+ const norm = normalizeSelectionRange(range);
291
+ for (let r = norm.startRow; r <= norm.endRow; r++) {
292
+ for (let c = norm.startCol; c <= norm.endCol; c++) {
293
+ if (r >= items.length || c >= visibleCols.length)
294
+ continue;
295
+ const item = items[r];
296
+ const col = visibleCols[c];
297
+ const oldValue = getCellValue(item, col);
298
+ onCellValueChanged({
299
+ item,
300
+ columnId: col.columnId,
301
+ field: col.columnId,
302
+ oldValue,
303
+ newValue: '',
304
+ rowIndex: r,
305
+ });
306
+ }
307
+ }
308
+ break;
309
+ }
310
+ case 'F10':
311
+ if (e.shiftKey) {
312
+ e.preventDefault();
313
+ if (activeCell != null && wrapperRef.current) {
314
+ const sel = `[data-row-index="${activeCell.rowIndex}"][data-col-index="${activeCell.columnIndex}"]`;
315
+ const cell = wrapperRef.current.querySelector(sel);
316
+ if (cell) {
317
+ const rect = cell.getBoundingClientRect();
318
+ setContextMenu({
319
+ x: rect.left + rect.width / 2,
320
+ y: rect.top + rect.height / 2,
321
+ });
322
+ }
323
+ else {
324
+ setContextMenu({ x: 100, y: 100 });
325
+ }
326
+ }
327
+ else {
328
+ setContextMenu({ x: 100, y: 100 });
329
+ }
330
+ }
331
+ break;
332
+ default:
333
+ break;
334
+ }
335
+ }, [
336
+ items,
337
+ activeCell,
338
+ hasCheckboxCol,
339
+ visibleColumnCount,
340
+ visibleCols,
341
+ colOffset,
342
+ editable,
343
+ onCellValueChanged,
344
+ getRowId,
345
+ editingCell,
346
+ rowSelection,
347
+ selectedRowIds,
348
+ handleRowCheckboxChange,
349
+ handleCopy,
350
+ handleCut,
351
+ handlePaste,
352
+ selectionRange,
353
+ setActiveCell,
354
+ setSelectionRange,
355
+ maxRowIndex,
356
+ maxColIndex,
357
+ setEditingCell,
358
+ setContextMenu,
359
+ wrapperRef,
360
+ onUndo,
361
+ onRedo,
362
+ ]);
363
+ return { handleGridKeyDown };
364
+ }