@alaarab/ogrid-core 1.8.2 → 2.0.0-beta

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 (74) hide show
  1. package/README.md +28 -31
  2. package/dist/esm/constants.js +11 -0
  3. package/dist/esm/index.js +6 -11
  4. package/dist/esm/types/index.js +2 -1
  5. package/dist/esm/utils/clientSideData.js +25 -12
  6. package/dist/esm/utils/columnUtils.js +6 -0
  7. package/dist/esm/utils/gridRowComparator.js +78 -0
  8. package/dist/esm/utils/index.js +1 -1
  9. package/dist/esm/utils/ogridHelpers.js +2 -1
  10. package/dist/esm/utils/paginationHelpers.js +7 -1
  11. package/dist/types/constants.d.ts +11 -0
  12. package/dist/types/index.d.ts +3 -16
  13. package/dist/types/types/columnTypes.d.ts +5 -9
  14. package/dist/types/types/dataGridTypes.d.ts +12 -133
  15. package/dist/types/types/index.d.ts +3 -3
  16. package/dist/types/utils/gridRowComparator.d.ts +57 -0
  17. package/dist/types/utils/index.d.ts +2 -2
  18. package/package.json +6 -24
  19. package/dist/esm/components/GridContextMenu.js +0 -34
  20. package/dist/esm/components/MarchingAntsOverlay.js +0 -109
  21. package/dist/esm/components/OGridLayout.js +0 -90
  22. package/dist/esm/components/SideBar.js +0 -100
  23. package/dist/esm/components/StatusBar.js +0 -6
  24. package/dist/esm/hooks/index.js +0 -19
  25. package/dist/esm/hooks/useActiveCell.js +0 -46
  26. package/dist/esm/hooks/useCellEditing.js +0 -11
  27. package/dist/esm/hooks/useCellSelection.js +0 -318
  28. package/dist/esm/hooks/useClipboard.js +0 -162
  29. package/dist/esm/hooks/useColumnChooserState.js +0 -62
  30. package/dist/esm/hooks/useColumnHeaderFilterState.js +0 -228
  31. package/dist/esm/hooks/useColumnResize.js +0 -69
  32. package/dist/esm/hooks/useContextMenu.js +0 -17
  33. package/dist/esm/hooks/useDataGridState.js +0 -366
  34. package/dist/esm/hooks/useDebounce.js +0 -35
  35. package/dist/esm/hooks/useFillHandle.js +0 -191
  36. package/dist/esm/hooks/useFilterOptions.js +0 -40
  37. package/dist/esm/hooks/useInlineCellEditorState.js +0 -44
  38. package/dist/esm/hooks/useKeyboardNavigation.js +0 -436
  39. package/dist/esm/hooks/useOGrid.js +0 -414
  40. package/dist/esm/hooks/useRichSelectState.js +0 -53
  41. package/dist/esm/hooks/useRowSelection.js +0 -68
  42. package/dist/esm/hooks/useSideBarState.js +0 -34
  43. package/dist/esm/hooks/useUndoRedo.js +0 -82
  44. package/dist/esm/storybook/index.js +0 -1
  45. package/dist/esm/storybook/mockData.js +0 -73
  46. package/dist/esm/utils/dataGridViewModel.js +0 -233
  47. package/dist/types/components/GridContextMenu.d.ts +0 -18
  48. package/dist/types/components/MarchingAntsOverlay.d.ts +0 -15
  49. package/dist/types/components/OGridLayout.d.ts +0 -37
  50. package/dist/types/components/SideBar.d.ts +0 -30
  51. package/dist/types/components/StatusBar.d.ts +0 -24
  52. package/dist/types/hooks/index.d.ts +0 -37
  53. package/dist/types/hooks/useActiveCell.d.ts +0 -13
  54. package/dist/types/hooks/useCellEditing.d.ts +0 -12
  55. package/dist/types/hooks/useCellSelection.d.ts +0 -17
  56. package/dist/types/hooks/useClipboard.d.ts +0 -25
  57. package/dist/types/hooks/useColumnChooserState.d.ts +0 -27
  58. package/dist/types/hooks/useColumnHeaderFilterState.d.ts +0 -72
  59. package/dist/types/hooks/useColumnResize.d.ts +0 -18
  60. package/dist/types/hooks/useContextMenu.d.ts +0 -15
  61. package/dist/types/hooks/useDataGridState.d.ts +0 -136
  62. package/dist/types/hooks/useDebounce.d.ts +0 -9
  63. package/dist/types/hooks/useFillHandle.d.ts +0 -28
  64. package/dist/types/hooks/useFilterOptions.d.ts +0 -16
  65. package/dist/types/hooks/useInlineCellEditorState.d.ts +0 -24
  66. package/dist/types/hooks/useKeyboardNavigation.d.ts +0 -34
  67. package/dist/types/hooks/useOGrid.d.ts +0 -31
  68. package/dist/types/hooks/useRichSelectState.d.ts +0 -17
  69. package/dist/types/hooks/useRowSelection.d.ts +0 -17
  70. package/dist/types/hooks/useSideBarState.d.ts +0 -15
  71. package/dist/types/hooks/useUndoRedo.d.ts +0 -21
  72. package/dist/types/storybook/index.d.ts +0 -2
  73. package/dist/types/storybook/mockData.d.ts +0 -37
  74. package/dist/types/utils/dataGridViewModel.d.ts +0 -169
@@ -1,318 +0,0 @@
1
- import { useState, useCallback, useRef, useEffect } from 'react';
2
- import { normalizeSelectionRange } from '../types';
3
- /** Compares two selection ranges by value. */
4
- function rangesEqual(a, b) {
5
- if (a === b)
6
- return true;
7
- if (!a || !b)
8
- return false;
9
- return a.startRow === b.startRow && a.endRow === b.endRow &&
10
- a.startCol === b.startCol && a.endCol === b.endCol;
11
- }
12
- /** DOM attribute name used for drag-range highlighting (bypasses React). */
13
- const DRAG_ATTR = 'data-drag-range';
14
- /** Auto-scroll config */
15
- const AUTO_SCROLL_EDGE = 40; // px from wrapper edge to trigger
16
- const AUTO_SCROLL_MIN_SPEED = 2;
17
- const AUTO_SCROLL_MAX_SPEED = 20;
18
- const AUTO_SCROLL_INTERVAL = 16; // ~60fps
19
- /** Compute scroll speed proportional to distance past the edge, capped. */
20
- function autoScrollSpeed(distance) {
21
- const t = Math.min(distance / AUTO_SCROLL_EDGE, 1);
22
- return AUTO_SCROLL_MIN_SPEED + t * (AUTO_SCROLL_MAX_SPEED - AUTO_SCROLL_MIN_SPEED);
23
- }
24
- export function useCellSelection(params) {
25
- const { colOffset, rowCount, visibleColCount, setActiveCell, wrapperRef } = params;
26
- const [selectionRange, _setSelectionRange] = useState(null);
27
- const isDraggingRef = useRef(false);
28
- const [isDragging, setIsDragging] = useState(false);
29
- /** True once a mousemove has been seen during the current drag gesture. */
30
- const dragMovedRef = useRef(false);
31
- const dragStartRef = useRef(null);
32
- const rafRef = useRef(0);
33
- /** Live drag range kept in a ref — only committed to React state on mouseup. */
34
- const liveDragRangeRef = useRef(null);
35
- /** Auto-scroll interval during drag. */
36
- const autoScrollRef = useRef(null);
37
- // Ref mirror of selectionRange — lets handleCellMouseDown read current value
38
- // without adding selectionRange to its useCallback deps (keeps it stable).
39
- const selectionRangeRef = useRef(selectionRange);
40
- selectionRangeRef.current = selectionRange;
41
- // Deduplicating setter — skips re-render when the range hasn't actually changed.
42
- const setSelectionRange = useCallback((next) => {
43
- if (rangesEqual(selectionRangeRef.current, next))
44
- return;
45
- _setSelectionRange(next);
46
- }, []);
47
- const handleCellMouseDown = useCallback((e, rowIndex, globalColIndex) => {
48
- // Only handle primary (left) button — let middle-click scroll and right-click context menu work natively
49
- if (e.button !== 0)
50
- return;
51
- if (globalColIndex < colOffset)
52
- return;
53
- // Prevent native text selection during cell drag
54
- e.preventDefault();
55
- const dataColIndex = globalColIndex - colOffset;
56
- const currentRange = selectionRangeRef.current;
57
- if (e.shiftKey && currentRange != null) {
58
- setSelectionRange(normalizeSelectionRange({
59
- startRow: currentRange.startRow,
60
- startCol: currentRange.startCol,
61
- endRow: rowIndex,
62
- endCol: dataColIndex,
63
- }));
64
- setActiveCell({ rowIndex, columnIndex: globalColIndex });
65
- }
66
- else {
67
- dragStartRef.current = { row: rowIndex, col: dataColIndex };
68
- dragMovedRef.current = false;
69
- const initial = {
70
- startRow: rowIndex,
71
- startCol: dataColIndex,
72
- endRow: rowIndex,
73
- endCol: dataColIndex,
74
- };
75
- setSelectionRange(initial);
76
- liveDragRangeRef.current = initial;
77
- setActiveCell({ rowIndex, columnIndex: globalColIndex });
78
- // Mark drag as "started" but don't set isDragging state yet —
79
- // setIsDragging(true) is deferred to the first mousemove to avoid
80
- // a true→false toggle on simple clicks (which causes 2 extra renders).
81
- isDraggingRef.current = true;
82
- }
83
- }, [colOffset, setActiveCell]);
84
- const handleSelectAllCells = useCallback(() => {
85
- if (rowCount === 0 || visibleColCount === 0)
86
- return;
87
- setSelectionRange({
88
- startRow: 0,
89
- startCol: 0,
90
- endRow: rowCount - 1,
91
- endCol: visibleColCount - 1,
92
- });
93
- setActiveCell({ rowIndex: 0, columnIndex: colOffset });
94
- }, [rowCount, visibleColCount, colOffset, setActiveCell]);
95
- /** Last known mouse position during drag — used by mouseUp to flush pending RAF work. */
96
- const lastMousePosRef = useRef(null);
97
- // Window mouse move/up for drag selection.
98
- // Performance: during drag, we update a ref + toggle DOM attributes via rAF.
99
- // React state is only committed on mouseup (single re-render instead of 60-120/s).
100
- useEffect(() => {
101
- const colOff = colOffset; // capture for closure
102
- /** Toggle DRAG_ATTR on cell-content divs to show the range highlight via CSS. */
103
- const applyDragAttrs = (range) => {
104
- const wrapper = wrapperRef.current;
105
- if (!wrapper)
106
- return;
107
- const minR = Math.min(range.startRow, range.endRow);
108
- const maxR = Math.max(range.startRow, range.endRow);
109
- const minC = Math.min(range.startCol, range.endCol);
110
- const maxC = Math.max(range.startCol, range.endCol);
111
- const cells = wrapper.querySelectorAll('[data-row-index][data-col-index]');
112
- for (let i = 0; i < cells.length; i++) {
113
- const el = cells[i];
114
- const r = parseInt(el.getAttribute('data-row-index'), 10);
115
- const c = parseInt(el.getAttribute('data-col-index'), 10) - colOff;
116
- const inRange = r >= minR && r <= maxR && c >= minC && c <= maxC;
117
- if (inRange) {
118
- if (!el.hasAttribute(DRAG_ATTR))
119
- el.setAttribute(DRAG_ATTR, '');
120
- }
121
- else {
122
- if (el.hasAttribute(DRAG_ATTR))
123
- el.removeAttribute(DRAG_ATTR);
124
- }
125
- }
126
- };
127
- const clearDragAttrs = () => {
128
- const wrapper = wrapperRef.current;
129
- if (!wrapper)
130
- return;
131
- const marked = wrapper.querySelectorAll(`[${DRAG_ATTR}]`);
132
- for (let i = 0; i < marked.length; i++)
133
- marked[i].removeAttribute(DRAG_ATTR);
134
- };
135
- /** Resolve mouse coordinates to a cell range (shared by RAF callback and mouseUp flush). */
136
- const resolveRange = (cx, cy) => {
137
- if (!dragStartRef.current)
138
- return null;
139
- const target = document.elementFromPoint(cx, cy);
140
- const cell = target?.closest?.('[data-row-index][data-col-index]');
141
- if (!cell)
142
- return null;
143
- const r = parseInt(cell.getAttribute('data-row-index') ?? '', 10);
144
- const c = parseInt(cell.getAttribute('data-col-index') ?? '', 10);
145
- if (Number.isNaN(r) || Number.isNaN(c) || c < colOff)
146
- return null;
147
- const dataCol = c - colOff;
148
- const start = dragStartRef.current;
149
- return normalizeSelectionRange({
150
- startRow: start.row,
151
- startCol: start.col,
152
- endRow: r,
153
- endCol: dataCol,
154
- });
155
- };
156
- /** Start or update auto-scroll interval based on mouse position relative to wrapper edges. */
157
- const updateAutoScroll = () => {
158
- const wrapper = wrapperRef.current;
159
- const pos = lastMousePosRef.current;
160
- if (!wrapper || !pos || !isDraggingRef.current) {
161
- stopAutoScroll();
162
- return;
163
- }
164
- const rect = wrapper.getBoundingClientRect();
165
- let dx = 0;
166
- let dy = 0;
167
- if (pos.cy < rect.top + AUTO_SCROLL_EDGE) {
168
- dy = -autoScrollSpeed(rect.top + AUTO_SCROLL_EDGE - pos.cy);
169
- }
170
- else if (pos.cy > rect.bottom - AUTO_SCROLL_EDGE) {
171
- dy = autoScrollSpeed(pos.cy - (rect.bottom - AUTO_SCROLL_EDGE));
172
- }
173
- if (pos.cx < rect.left + AUTO_SCROLL_EDGE) {
174
- dx = -autoScrollSpeed(rect.left + AUTO_SCROLL_EDGE - pos.cx);
175
- }
176
- else if (pos.cx > rect.right - AUTO_SCROLL_EDGE) {
177
- dx = autoScrollSpeed(pos.cx - (rect.right - AUTO_SCROLL_EDGE));
178
- }
179
- if (dx === 0 && dy === 0) {
180
- stopAutoScroll();
181
- return;
182
- }
183
- // Start interval if not already running
184
- if (!autoScrollRef.current) {
185
- autoScrollRef.current = setInterval(() => {
186
- const w = wrapperRef.current;
187
- const p = lastMousePosRef.current;
188
- if (!w || !p || !isDraggingRef.current) {
189
- stopAutoScroll();
190
- return;
191
- }
192
- const r = w.getBoundingClientRect();
193
- let sdx = 0;
194
- let sdy = 0;
195
- if (p.cy < r.top + AUTO_SCROLL_EDGE)
196
- sdy = -autoScrollSpeed(r.top + AUTO_SCROLL_EDGE - p.cy);
197
- else if (p.cy > r.bottom - AUTO_SCROLL_EDGE)
198
- sdy = autoScrollSpeed(p.cy - (r.bottom - AUTO_SCROLL_EDGE));
199
- if (p.cx < r.left + AUTO_SCROLL_EDGE)
200
- sdx = -autoScrollSpeed(r.left + AUTO_SCROLL_EDGE - p.cx);
201
- else if (p.cx > r.right - AUTO_SCROLL_EDGE)
202
- sdx = autoScrollSpeed(p.cx - (r.right - AUTO_SCROLL_EDGE));
203
- if (sdx === 0 && sdy === 0) {
204
- stopAutoScroll();
205
- return;
206
- }
207
- w.scrollTop += sdy;
208
- w.scrollLeft += sdx;
209
- // After scrolling, re-resolve the cell under the mouse and update drag range
210
- const newRange = resolveRange(p.cx, p.cy);
211
- if (newRange) {
212
- liveDragRangeRef.current = newRange;
213
- applyDragAttrs(newRange);
214
- }
215
- }, AUTO_SCROLL_INTERVAL);
216
- }
217
- };
218
- const stopAutoScroll = () => {
219
- if (autoScrollRef.current) {
220
- clearInterval(autoScrollRef.current);
221
- autoScrollRef.current = null;
222
- }
223
- };
224
- const onMove = (e) => {
225
- if (!isDraggingRef.current || !dragStartRef.current)
226
- return;
227
- // Promote to a real drag on first mousemove (deferred from mouseDown
228
- // to avoid a true→false toggle on simple clicks).
229
- if (!dragMovedRef.current) {
230
- dragMovedRef.current = true;
231
- setIsDragging(true);
232
- }
233
- // Always store latest position so mouseUp can flush if RAF hasn't executed
234
- lastMousePosRef.current = { cx: e.clientX, cy: e.clientY };
235
- // Update auto-scroll based on mouse proximity to edges
236
- updateAutoScroll();
237
- // Cancel previous pending frame
238
- if (rafRef.current)
239
- cancelAnimationFrame(rafRef.current);
240
- rafRef.current = requestAnimationFrame(() => {
241
- rafRef.current = 0;
242
- const pos = lastMousePosRef.current;
243
- if (!pos)
244
- return;
245
- const newRange = resolveRange(pos.cx, pos.cy);
246
- if (!newRange)
247
- return;
248
- // Skip if range unchanged
249
- const prev = liveDragRangeRef.current;
250
- if (prev &&
251
- prev.startRow === newRange.startRow &&
252
- prev.startCol === newRange.startCol &&
253
- prev.endRow === newRange.endRow &&
254
- prev.endCol === newRange.endCol) {
255
- return;
256
- }
257
- liveDragRangeRef.current = newRange;
258
- // DOM-only highlighting — no React state update until mouseup
259
- applyDragAttrs(newRange);
260
- });
261
- };
262
- const onUp = () => {
263
- if (!isDraggingRef.current)
264
- return;
265
- stopAutoScroll();
266
- if (rafRef.current) {
267
- cancelAnimationFrame(rafRef.current);
268
- rafRef.current = 0;
269
- }
270
- isDraggingRef.current = false;
271
- const wasDrag = dragMovedRef.current;
272
- if (wasDrag) {
273
- // Flush: if the last RAF hasn't executed yet, resolve the range now from the
274
- // last known mouse position so the final committed range is always accurate.
275
- const pos = lastMousePosRef.current;
276
- if (pos) {
277
- const flushed = resolveRange(pos.cx, pos.cy);
278
- if (flushed)
279
- liveDragRangeRef.current = flushed;
280
- }
281
- // Commit final range to React state (triggers a single re-render)
282
- const finalRange = liveDragRangeRef.current;
283
- if (finalRange) {
284
- setSelectionRange(finalRange);
285
- setActiveCell({
286
- rowIndex: finalRange.endRow,
287
- columnIndex: finalRange.endCol + colOff,
288
- });
289
- }
290
- }
291
- // For simple clicks (no drag movement), mouseDown already set
292
- // selectionRange + activeCell — skip redundant state updates.
293
- // Clean up DOM attributes — React will apply CSS-module classes on the same paint
294
- clearDragAttrs();
295
- liveDragRangeRef.current = null;
296
- lastMousePosRef.current = null;
297
- dragStartRef.current = null;
298
- if (wasDrag)
299
- setIsDragging(false);
300
- };
301
- window.addEventListener('mousemove', onMove, true);
302
- window.addEventListener('mouseup', onUp, true);
303
- return () => {
304
- window.removeEventListener('mousemove', onMove, true);
305
- window.removeEventListener('mouseup', onUp, true);
306
- if (rafRef.current)
307
- cancelAnimationFrame(rafRef.current);
308
- stopAutoScroll();
309
- };
310
- }, [colOffset, setActiveCell, wrapperRef]);
311
- return {
312
- selectionRange,
313
- setSelectionRange,
314
- handleCellMouseDown,
315
- handleSelectAllCells,
316
- isDragging,
317
- };
318
- }
@@ -1,162 +0,0 @@
1
- import { useCallback, useRef, useState } from 'react';
2
- import { getCellValue } from '../utils';
3
- import { parseValue } from '../utils/valueParsers';
4
- import { normalizeSelectionRange } from '../types';
5
- export function useClipboard(params) {
6
- const { items, visibleCols, colOffset, selectionRange, activeCell, editable, onCellValueChanged, beginBatch, endBatch, } = params;
7
- const cutRangeRef = useRef(null);
8
- const [cutRange, setCutRange] = useState(null);
9
- const [copyRange, setCopyRange] = useState(null);
10
- /** In-page clipboard fallback when system clipboard is unavailable. */
11
- const internalClipboardRef = useRef(null);
12
- const handleCopy = useCallback(() => {
13
- const range = selectionRange ??
14
- (activeCell != null
15
- ? {
16
- startRow: activeCell.rowIndex,
17
- startCol: activeCell.columnIndex - colOffset,
18
- endRow: activeCell.rowIndex,
19
- endCol: activeCell.columnIndex - colOffset,
20
- }
21
- : null);
22
- if (range == null)
23
- return;
24
- const norm = normalizeSelectionRange(range);
25
- const rows = [];
26
- for (let r = norm.startRow; r <= norm.endRow; r++) {
27
- const cells = [];
28
- for (let c = norm.startCol; c <= norm.endCol; c++) {
29
- if (r >= items.length || c >= visibleCols.length)
30
- break;
31
- const item = items[r];
32
- const col = visibleCols[c];
33
- const raw = getCellValue(item, col);
34
- const val = col.valueFormatter ? col.valueFormatter(raw, item) : raw;
35
- cells.push(val != null && val !== '' ? String(val).replace(/\t/g, ' ').replace(/\n/g, ' ') : '');
36
- }
37
- rows.push(cells.join('\t'));
38
- }
39
- const tsv = rows.join('\r\n');
40
- internalClipboardRef.current = tsv;
41
- setCopyRange(norm);
42
- void navigator.clipboard.writeText(tsv).catch(() => { });
43
- }, [selectionRange, activeCell, colOffset, items, visibleCols]);
44
- const handleCut = useCallback(() => {
45
- if (editable === false)
46
- return;
47
- const range = selectionRange ??
48
- (activeCell != null
49
- ? {
50
- startRow: activeCell.rowIndex,
51
- startCol: activeCell.columnIndex - colOffset,
52
- endRow: activeCell.rowIndex,
53
- endCol: activeCell.columnIndex - colOffset,
54
- }
55
- : null);
56
- if (range == null || onCellValueChanged == null)
57
- return;
58
- const norm = normalizeSelectionRange(range);
59
- cutRangeRef.current = norm;
60
- setCutRange(norm);
61
- setCopyRange(null);
62
- handleCopy();
63
- // handleCopy sets copyRange — override it back since this is a cut
64
- setCopyRange(null);
65
- }, [selectionRange, activeCell, colOffset, handleCopy, editable, onCellValueChanged]);
66
- const handlePaste = useCallback(async () => {
67
- if (editable === false)
68
- return;
69
- if (onCellValueChanged == null)
70
- return;
71
- let text;
72
- try {
73
- text = await navigator.clipboard.readText();
74
- }
75
- catch {
76
- text = '';
77
- }
78
- if (!text.trim() && internalClipboardRef.current != null) {
79
- text = internalClipboardRef.current;
80
- }
81
- if (!text.trim())
82
- return;
83
- const norm = selectionRange ??
84
- (activeCell != null
85
- ? {
86
- startRow: activeCell.rowIndex,
87
- startCol: activeCell.columnIndex - colOffset,
88
- endRow: activeCell.rowIndex,
89
- endCol: activeCell.columnIndex - colOffset,
90
- }
91
- : null);
92
- const anchorRow = norm ? norm.startRow : 0;
93
- const anchorCol = norm ? norm.startCol : 0;
94
- const lines = text.split(/\r?\n/).filter((l) => l.length > 0);
95
- beginBatch?.();
96
- for (let r = 0; r < lines.length; r++) {
97
- const cells = lines[r].split('\t');
98
- for (let c = 0; c < cells.length; c++) {
99
- const targetRow = anchorRow + r;
100
- const targetCol = anchorCol + c;
101
- if (targetRow >= items.length || targetCol >= visibleCols.length)
102
- continue;
103
- const item = items[targetRow];
104
- const col = visibleCols[targetCol];
105
- const colEditable = col.editable === true ||
106
- (typeof col.editable === 'function' && col.editable(item));
107
- if (!colEditable)
108
- continue;
109
- const rawValue = cells[c] ?? '';
110
- const oldValue = getCellValue(item, col);
111
- const result = parseValue(rawValue, oldValue, item, col);
112
- if (!result.valid)
113
- continue;
114
- onCellValueChanged({
115
- item,
116
- columnId: col.columnId,
117
- field: col.columnId,
118
- oldValue,
119
- newValue: result.value,
120
- rowIndex: targetRow,
121
- });
122
- }
123
- }
124
- if (cutRangeRef.current) {
125
- const cut = cutRangeRef.current;
126
- for (let r = cut.startRow; r <= cut.endRow; r++) {
127
- for (let c = cut.startCol; c <= cut.endCol; c++) {
128
- if (r >= items.length || c >= visibleCols.length)
129
- continue;
130
- const item = items[r];
131
- const col = visibleCols[c];
132
- const colEditable = col.editable === true ||
133
- (typeof col.editable === 'function' && col.editable(item));
134
- if (!colEditable)
135
- continue;
136
- const oldValue = getCellValue(item, col);
137
- const result = parseValue('', oldValue, item, col);
138
- if (!result.valid)
139
- continue;
140
- onCellValueChanged({
141
- item,
142
- columnId: col.columnId,
143
- field: col.columnId,
144
- oldValue,
145
- newValue: result.value,
146
- rowIndex: r,
147
- });
148
- }
149
- }
150
- cutRangeRef.current = null;
151
- setCutRange(null);
152
- }
153
- endBatch?.();
154
- setCopyRange(null);
155
- }, [selectionRange, activeCell, colOffset, items, visibleCols, editable, onCellValueChanged, beginBatch, endBatch]);
156
- const clearClipboardRanges = useCallback(() => {
157
- setCopyRange(null);
158
- setCutRange(null);
159
- cutRangeRef.current = null;
160
- }, []);
161
- return { handleCopy, handleCut, handlePaste, cutRangeRef, cutRange, copyRange, clearClipboardRanges };
162
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * Headless column chooser state and handlers for Fluent, Material, and Radix.
3
- * UI packages use this hook and render only trigger + popover (checkboxes, Select All, Clear All).
4
- */
5
- import { useState, useCallback, useEffect } from 'react';
6
- /**
7
- * Returns open/setOpen, handleToggle, handleClose (Escape handled in hook),
8
- * handleCheckboxChange(columnKey)(visible), handleSelectAll, handleClearAll,
9
- * visibleCount, totalCount. UI renders trigger + popover and wires handlers.
10
- */
11
- export function useColumnChooserState(params) {
12
- const { columns, visibleColumns, onVisibilityChange } = params;
13
- const [open, setOpen] = useState(false);
14
- useEffect(() => {
15
- if (!open)
16
- return;
17
- const handleKeyDown = (event) => {
18
- if (event.key === 'Escape') {
19
- event.preventDefault();
20
- setOpen(false);
21
- }
22
- };
23
- document.addEventListener('keydown', handleKeyDown, true);
24
- return () => document.removeEventListener('keydown', handleKeyDown, true);
25
- }, [open]);
26
- const handleToggle = useCallback(() => {
27
- setOpen((prev) => !prev);
28
- }, []);
29
- const handleClose = useCallback(() => {
30
- setOpen(false);
31
- }, []);
32
- const handleCheckboxChange = useCallback((columnKey) => (visible) => {
33
- onVisibilityChange(columnKey, visible);
34
- }, [onVisibilityChange]);
35
- const handleSelectAll = useCallback(() => {
36
- columns.forEach((col) => {
37
- if (!visibleColumns.has(col.columnId)) {
38
- onVisibilityChange(col.columnId, true);
39
- }
40
- });
41
- }, [columns, visibleColumns, onVisibilityChange]);
42
- const handleClearAll = useCallback(() => {
43
- columns.forEach((col) => {
44
- if (!col.required && visibleColumns.has(col.columnId)) {
45
- onVisibilityChange(col.columnId, false);
46
- }
47
- });
48
- }, [columns, visibleColumns, onVisibilityChange]);
49
- const visibleCount = visibleColumns.size;
50
- const totalCount = columns.length;
51
- return {
52
- open,
53
- setOpen,
54
- handleToggle,
55
- handleClose,
56
- handleCheckboxChange,
57
- handleSelectAll,
58
- handleClearAll,
59
- visibleCount,
60
- totalCount,
61
- };
62
- }