@pablovsouza/react-datagrid 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +267 -2
- package/dist/DataGrid.d.ts +11 -0
- package/dist/DataGrid.d.ts.map +1 -0
- package/dist/DataGrid.js +782 -0
- package/dist/DefaultEditor.d.ts +13 -0
- package/dist/DefaultEditor.d.ts.map +1 -0
- package/dist/DefaultEditor.js +59 -0
- package/dist/clipboard.d.ts +5 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +51 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/keyboard.d.ts +30 -0
- package/dist/keyboard.d.ts.map +1 -0
- package/dist/keyboard.js +53 -0
- package/dist/selection.d.ts +23 -0
- package/dist/selection.d.ts.map +1 -0
- package/dist/selection.js +43 -0
- package/dist/styles.css +256 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +73 -4
package/dist/DataGrid.js
ADDED
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useCallback, useEffect, useId, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { columnOrderingFeature, columnPinningFeature, columnSizingFeature, columnVisibilityFeature, createSortedRowModel, rowSortingFeature, tableFeatures, useTable } from '@tanstack/react-table';
|
|
4
|
+
import { observeElementRect, useVirtualizer } from '@tanstack/react-virtual';
|
|
5
|
+
import { DefaultEditor } from './DefaultEditor';
|
|
6
|
+
import { parseTsv, toTsv } from './clipboard';
|
|
7
|
+
import { isTypingKey, navigate, tabTarget } from './keyboard';
|
|
8
|
+
import { boundsOf, cellSelected, clamp, clampRanges, inBounds, normalizeRanges, rowSelected, sameCell, single } from './selection';
|
|
9
|
+
/** TanStack Table: column order, widths, visibility, pinning and sorting. */
|
|
10
|
+
const features = tableFeatures({
|
|
11
|
+
columnOrderingFeature,
|
|
12
|
+
columnPinningFeature,
|
|
13
|
+
columnSizingFeature,
|
|
14
|
+
columnVisibilityFeature,
|
|
15
|
+
rowSortingFeature,
|
|
16
|
+
sortedRowModel: createSortedRowModel()
|
|
17
|
+
});
|
|
18
|
+
const DEFAULT_WIDTH = 150;
|
|
19
|
+
const DEFAULT_MIN_WIDTH = 40;
|
|
20
|
+
const LONG_PRESS_MS = 550;
|
|
21
|
+
/** A value that is controlled when the prop is not `undefined`, else kept inside. */
|
|
22
|
+
function useControlled(controlled, initial, onChange) {
|
|
23
|
+
const [inner, setInner] = useState(initial);
|
|
24
|
+
const isControlled = controlled !== undefined;
|
|
25
|
+
const set = useCallback((value) => {
|
|
26
|
+
if (!isControlled)
|
|
27
|
+
setInner(() => value);
|
|
28
|
+
onChange?.(value);
|
|
29
|
+
}, [isControlled, onChange]);
|
|
30
|
+
return [isControlled ? controlled : inner, set];
|
|
31
|
+
}
|
|
32
|
+
export const defaultCompare = (a, b) => {
|
|
33
|
+
if (a == null || a === '')
|
|
34
|
+
return b == null || b === '' ? 0 : -1;
|
|
35
|
+
if (b == null || b === '')
|
|
36
|
+
return 1;
|
|
37
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
38
|
+
return a - b;
|
|
39
|
+
if (a instanceof Date && b instanceof Date)
|
|
40
|
+
return a.getTime() - b.getTime();
|
|
41
|
+
return String(a).localeCompare(String(b), undefined, { numeric: true, sensitivity: 'base' });
|
|
42
|
+
};
|
|
43
|
+
const valueOf = (column, row) => column.value ? column.value(row) : row[column.id];
|
|
44
|
+
const textOf = (column, row, value = valueOf(column, row)) => column.format ? column.format(value, row) : value == null ? '' : String(value);
|
|
45
|
+
const canEditColumn = (column, row) => !!column &&
|
|
46
|
+
row !== undefined &&
|
|
47
|
+
(typeof column.editable === 'function' ? column.editable(row) : !!column.editable);
|
|
48
|
+
const initialColumnState = (columns, partial) => ({
|
|
49
|
+
order: partial?.order ?? columns.map((column) => column.id),
|
|
50
|
+
widths: partial?.widths ?? {},
|
|
51
|
+
hidden: partial?.hidden ?? columns.filter((column) => column.hidden).map((column) => column.id),
|
|
52
|
+
frozenStart: partial?.frozenStart ??
|
|
53
|
+
columns.filter((column) => column.frozen === 'start').map((column) => column.id),
|
|
54
|
+
frozenEnd: partial?.frozenEnd ??
|
|
55
|
+
columns.filter((column) => column.frozen === 'end').map((column) => column.id)
|
|
56
|
+
});
|
|
57
|
+
function DataGridInner(props, ref) {
|
|
58
|
+
const { rows, columns, getRowId, selectionMode = 'row', multiSelect = true, rowHeight = 32, overscan = 8, editable = false, manualSorting = false, tabBehavior = selectionMode === 'cell' ? 'cell' : 'leave' } = props;
|
|
59
|
+
const headerHeight = props.headerHeight ?? rowHeight;
|
|
60
|
+
const gridId = useId().replace(/:/g, '');
|
|
61
|
+
const rootRef = useRef(null);
|
|
62
|
+
const scrollRef = useRef(null);
|
|
63
|
+
// ── State (each one controlled or not) ──
|
|
64
|
+
const [columnState, setColumnState] = useControlled(props.columnState, () => initialColumnState(columns, props.defaultColumnState), props.onColumnStateChange);
|
|
65
|
+
const [sorting, setSorting] = useControlled(props.sorting, () => props.defaultSorting ?? [], props.onSortingChange);
|
|
66
|
+
// `undefined` inside: the first cell once there are rows.
|
|
67
|
+
const [activeInner, setActiveInner] = useState(props.defaultActiveCell);
|
|
68
|
+
const [rangesInner, setRangesInner] = useState(() => props.defaultSelection ?? []);
|
|
69
|
+
const [editing, setEditing] = useState(null);
|
|
70
|
+
// ── TanStack Table: sorted rows and the visible columns in order ──
|
|
71
|
+
const columnDefs = useMemo(() => columns.map((column) => ({
|
|
72
|
+
id: column.id,
|
|
73
|
+
accessorFn: (row) => valueOf(column, row),
|
|
74
|
+
header: column.id,
|
|
75
|
+
size: column.width ?? DEFAULT_WIDTH,
|
|
76
|
+
minSize: column.minWidth ?? DEFAULT_MIN_WIDTH,
|
|
77
|
+
maxSize: column.maxWidth ?? Number.MAX_SAFE_INTEGER,
|
|
78
|
+
enableSorting: column.sortable ?? true,
|
|
79
|
+
enableHiding: column.hideable ?? true,
|
|
80
|
+
sortFn: (a, b, id) => (column.compare ?? defaultCompare)(a.getValue(id), b.getValue(id))
|
|
81
|
+
})), [columns]);
|
|
82
|
+
const tableSorting = useMemo(() => sorting.map((item) => ({ id: item.columnId, desc: item.direction === 'desc' })), [sorting]);
|
|
83
|
+
const tableState = useMemo(() => ({
|
|
84
|
+
sorting: tableSorting,
|
|
85
|
+
columnOrder: columnState.order,
|
|
86
|
+
columnSizing: columnState.widths,
|
|
87
|
+
columnVisibility: Object.fromEntries(columnState.hidden.map((id) => [id, false])),
|
|
88
|
+
columnPinning: { start: columnState.frozenStart, end: columnState.frozenEnd }
|
|
89
|
+
}), [tableSorting, columnState]);
|
|
90
|
+
const table = useTable({
|
|
91
|
+
features,
|
|
92
|
+
columns: columnDefs,
|
|
93
|
+
data: rows,
|
|
94
|
+
getRowId: (row, index) => getRowId(row, index),
|
|
95
|
+
state: tableState,
|
|
96
|
+
manualSorting
|
|
97
|
+
});
|
|
98
|
+
const displayed = table.getRowModel().rows;
|
|
99
|
+
const displayedRows = useMemo(() => displayed.map((row) => row.original), [displayed]);
|
|
100
|
+
const byId = useMemo(() => new Map(columns.map((column) => [column.id, column])), [columns]);
|
|
101
|
+
const visibleColumns = [
|
|
102
|
+
...table.getStartVisibleLeafColumns(),
|
|
103
|
+
...table.getCenterVisibleLeafColumns(),
|
|
104
|
+
...table.getEndVisibleLeafColumns()
|
|
105
|
+
].flatMap((column) => {
|
|
106
|
+
const def = byId.get(column.id);
|
|
107
|
+
if (!def)
|
|
108
|
+
return [];
|
|
109
|
+
const pinned = column.getIsPinned();
|
|
110
|
+
return [
|
|
111
|
+
{
|
|
112
|
+
def,
|
|
113
|
+
width: column.getSize(),
|
|
114
|
+
pinned,
|
|
115
|
+
offset: pinned === 'start'
|
|
116
|
+
? column.getStart('start')
|
|
117
|
+
: pinned === 'end'
|
|
118
|
+
? column.getAfter('end')
|
|
119
|
+
: 0
|
|
120
|
+
}
|
|
121
|
+
];
|
|
122
|
+
});
|
|
123
|
+
const rowCount = displayedRows.length;
|
|
124
|
+
const colCount = visibleColumns.length;
|
|
125
|
+
// ── Current cell and selection, kept inside the grid ──
|
|
126
|
+
const activeControlled = props.activeRowIndex !== undefined
|
|
127
|
+
? props.activeRowIndex < 0
|
|
128
|
+
? null
|
|
129
|
+
: { row: props.activeRowIndex, col: activeInner?.col ?? 0 }
|
|
130
|
+
: props.activeCell !== undefined
|
|
131
|
+
? props.activeCell
|
|
132
|
+
: (activeInner ?? undefined);
|
|
133
|
+
const rawActive = activeControlled === undefined
|
|
134
|
+
? rowCount > 0 && colCount > 0
|
|
135
|
+
? { row: 0, col: 0 }
|
|
136
|
+
: null
|
|
137
|
+
: activeControlled;
|
|
138
|
+
const active = rawActive && rowCount > 0 && colCount > 0
|
|
139
|
+
? { row: clamp(rawActive.row, 0, rowCount - 1), col: clamp(rawActive.col, 0, colCount - 1) }
|
|
140
|
+
: null;
|
|
141
|
+
const rangesSource = props.selection ?? rangesInner;
|
|
142
|
+
// The selection always holds the current cell (a host that moves the current cell resets it).
|
|
143
|
+
const ranges = useMemo(() => {
|
|
144
|
+
const fitted = clampRanges(rangesSource, rowCount, colCount);
|
|
145
|
+
const last = fitted.at(-1);
|
|
146
|
+
const valid = active && last && inBounds(boundsOf(last), active.row, active.col);
|
|
147
|
+
const base = valid ? fitted : active ? [single(active)] : [];
|
|
148
|
+
return normalizeRanges(base, selectionMode, colCount);
|
|
149
|
+
}, [rangesSource, rowCount, colCount, active?.row, active?.col, selectionMode]);
|
|
150
|
+
const latest = useRef({
|
|
151
|
+
active,
|
|
152
|
+
ranges,
|
|
153
|
+
rowCount,
|
|
154
|
+
colCount,
|
|
155
|
+
displayedRows,
|
|
156
|
+
visibleColumns,
|
|
157
|
+
editing
|
|
158
|
+
});
|
|
159
|
+
latest.current = { active, ranges, rowCount, colCount, displayedRows, visibleColumns, editing };
|
|
160
|
+
const setActive = useCallback((cell) => {
|
|
161
|
+
const previous = latest.current.active;
|
|
162
|
+
setActiveInner(cell);
|
|
163
|
+
const row = cell ? latest.current.displayedRows[cell.row] : undefined;
|
|
164
|
+
props.onActiveCellChange?.(cell, row);
|
|
165
|
+
if (cell && cell.row !== previous?.row)
|
|
166
|
+
props.onActiveRowChange?.(cell.row, row);
|
|
167
|
+
}, [props.onActiveCellChange, props.onActiveRowChange]);
|
|
168
|
+
const setRanges = useCallback((next) => {
|
|
169
|
+
setRangesInner(next);
|
|
170
|
+
props.onSelectionChange?.(next);
|
|
171
|
+
}, [props.onSelectionChange]);
|
|
172
|
+
// ── Virtual rows ──
|
|
173
|
+
const virtualizer = useVirtualizer({
|
|
174
|
+
count: rowCount,
|
|
175
|
+
getScrollElement: () => scrollRef.current,
|
|
176
|
+
estimateSize: () => rowHeight,
|
|
177
|
+
overscan,
|
|
178
|
+
scrollMargin: headerHeight,
|
|
179
|
+
scrollPaddingStart: headerHeight,
|
|
180
|
+
// A scroller with no size yet (not laid out, a hidden window, a test DOM without layout) still
|
|
181
|
+
// renders the rows a screen would show, instead of none.
|
|
182
|
+
observeElementRect: (instance, callback) => observeElementRect(instance, (rect) => callback(rect.height > 0 ? rect : { width: rect.width, height: window.innerHeight || 600 }))
|
|
183
|
+
});
|
|
184
|
+
const scrollIntoView = useCallback((cell) => {
|
|
185
|
+
virtualizer.scrollToIndex(cell.row, { align: 'auto' });
|
|
186
|
+
const scroller = scrollRef.current;
|
|
187
|
+
const column = latest.current.visibleColumns[cell.col];
|
|
188
|
+
if (!scroller || !column || column.pinned)
|
|
189
|
+
return;
|
|
190
|
+
let left = 0;
|
|
191
|
+
let startPinned = 0;
|
|
192
|
+
let endPinned = 0;
|
|
193
|
+
for (const [index, item] of latest.current.visibleColumns.entries()) {
|
|
194
|
+
if (index < cell.col)
|
|
195
|
+
left += item.width;
|
|
196
|
+
if (item.pinned === 'start')
|
|
197
|
+
startPinned += item.width;
|
|
198
|
+
if (item.pinned === 'end')
|
|
199
|
+
endPinned += item.width;
|
|
200
|
+
}
|
|
201
|
+
const viewStart = scroller.scrollLeft + startPinned;
|
|
202
|
+
const viewEnd = scroller.scrollLeft + scroller.clientWidth - endPinned;
|
|
203
|
+
if (left < viewStart)
|
|
204
|
+
scroller.scrollLeft = left - startPinned;
|
|
205
|
+
else if (left + column.width > viewEnd)
|
|
206
|
+
scroller.scrollLeft = left + column.width - scroller.clientWidth + endPinned;
|
|
207
|
+
}, [virtualizer]);
|
|
208
|
+
/** Moves the current cell: alone, extending the last range (Shift) or adding a range (Ctrl). */
|
|
209
|
+
const moveTo = useCallback((cell, how = 'replace') => {
|
|
210
|
+
const { ranges: current } = latest.current;
|
|
211
|
+
if (how === 'extend' && multiSelect && current.length > 0) {
|
|
212
|
+
const last = current.at(-1);
|
|
213
|
+
setRanges([...current.slice(0, -1), { anchor: last.anchor, focus: cell }]);
|
|
214
|
+
}
|
|
215
|
+
else if (how === 'add' && multiSelect) {
|
|
216
|
+
setRanges([...current, single(cell)]);
|
|
217
|
+
}
|
|
218
|
+
else
|
|
219
|
+
setRanges([single(cell)]);
|
|
220
|
+
if (!sameCell(latest.current.active, cell))
|
|
221
|
+
setActive(cell);
|
|
222
|
+
scrollIntoView(cell);
|
|
223
|
+
}, [multiSelect, setActive, setRanges, scrollIntoView]);
|
|
224
|
+
const selectAll = useCallback(() => {
|
|
225
|
+
const { rowCount: count, colCount: cols, active: current } = latest.current;
|
|
226
|
+
if (!multiSelect || count === 0 || cols === 0)
|
|
227
|
+
return;
|
|
228
|
+
const all = { anchor: { row: 0, col: 0 }, focus: { row: count - 1, col: cols - 1 } };
|
|
229
|
+
setRanges([all]);
|
|
230
|
+
if (!current)
|
|
231
|
+
setActive({ row: 0, col: 0 });
|
|
232
|
+
}, [multiSelect, setActive, setRanges]);
|
|
233
|
+
// ── Clipboard ──
|
|
234
|
+
const getSelectionText = useCallback(() => {
|
|
235
|
+
const { ranges: current, displayedRows: data, visibleColumns: cols } = latest.current;
|
|
236
|
+
const blocks = [...current]
|
|
237
|
+
.map(boundsOf)
|
|
238
|
+
.sort((a, b) => a.top - b.top || a.left - b.left)
|
|
239
|
+
.map((bounds) => {
|
|
240
|
+
const matrix = [];
|
|
241
|
+
for (let row = bounds.top; row <= bounds.bottom; row++) {
|
|
242
|
+
const item = data[row];
|
|
243
|
+
if (item === undefined)
|
|
244
|
+
continue;
|
|
245
|
+
const line = [];
|
|
246
|
+
for (let col = bounds.left; col <= bounds.right; col++) {
|
|
247
|
+
const column = cols[col];
|
|
248
|
+
if (column)
|
|
249
|
+
line.push(textOf(column.def, item));
|
|
250
|
+
}
|
|
251
|
+
matrix.push(line);
|
|
252
|
+
}
|
|
253
|
+
return toTsv(matrix);
|
|
254
|
+
});
|
|
255
|
+
return blocks.join('\n');
|
|
256
|
+
}, []);
|
|
257
|
+
const commitChanges = useCallback((changes) => {
|
|
258
|
+
if (changes.length === 0)
|
|
259
|
+
return;
|
|
260
|
+
if (props.onPaste)
|
|
261
|
+
props.onPaste(changes);
|
|
262
|
+
else
|
|
263
|
+
for (const change of changes)
|
|
264
|
+
props.onCellEdit?.(change);
|
|
265
|
+
}, [props.onPaste, props.onCellEdit]);
|
|
266
|
+
const paste = useCallback((text) => {
|
|
267
|
+
const { ranges: current, active: at, displayedRows: data, visibleColumns: cols } = latest.current;
|
|
268
|
+
if (!editable || !at)
|
|
269
|
+
return;
|
|
270
|
+
const matrix = parseTsv(text);
|
|
271
|
+
if (matrix.length === 0)
|
|
272
|
+
return;
|
|
273
|
+
const last = current.at(-1);
|
|
274
|
+
const bounds = last
|
|
275
|
+
? boundsOf(last)
|
|
276
|
+
: { top: at.row, bottom: at.row, left: at.col, right: at.col };
|
|
277
|
+
// One value into a larger selection fills it (as spreadsheets do); else the block from the top-left cell.
|
|
278
|
+
const fill = matrix.length === 1 && matrix[0].length === 1;
|
|
279
|
+
const height = fill ? bounds.bottom - bounds.top + 1 : matrix.length;
|
|
280
|
+
const width = fill
|
|
281
|
+
? bounds.right - bounds.left + 1
|
|
282
|
+
: Math.max(...matrix.map((line) => line.length));
|
|
283
|
+
const changes = [];
|
|
284
|
+
for (let r = 0; r < height; r++) {
|
|
285
|
+
const rowIndex = bounds.top + r;
|
|
286
|
+
const row = data[rowIndex];
|
|
287
|
+
if (row === undefined)
|
|
288
|
+
break;
|
|
289
|
+
for (let c = 0; c < width; c++) {
|
|
290
|
+
const column = cols[bounds.left + c]?.def;
|
|
291
|
+
const source = fill ? matrix[0][0] : matrix[r]?.[c];
|
|
292
|
+
if (!column || source === undefined || !canEditColumn(column, row))
|
|
293
|
+
continue;
|
|
294
|
+
const value = column.parse ? column.parse(source, row) : source;
|
|
295
|
+
if (value === undefined)
|
|
296
|
+
continue;
|
|
297
|
+
changes.push({
|
|
298
|
+
row,
|
|
299
|
+
rowIndex,
|
|
300
|
+
rowId: getRowId(row, rowIndex),
|
|
301
|
+
columnId: column.id,
|
|
302
|
+
value,
|
|
303
|
+
previousValue: valueOf(column, row)
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
commitChanges(changes);
|
|
308
|
+
const bottom = Math.min(bounds.top + height - 1, data.length - 1);
|
|
309
|
+
const right = Math.min(bounds.left + width - 1, cols.length - 1);
|
|
310
|
+
setRanges([
|
|
311
|
+
{ anchor: { row: bounds.top, col: bounds.left }, focus: { row: bottom, col: right } }
|
|
312
|
+
]);
|
|
313
|
+
setActive({ row: bounds.top, col: bounds.left });
|
|
314
|
+
}, [editable, commitChanges, getRowId, setActive, setRanges]);
|
|
315
|
+
// ── Editing ──
|
|
316
|
+
const startEdit = useCallback((initialText, cell) => {
|
|
317
|
+
const { displayedRows: data, visibleColumns: cols } = latest.current;
|
|
318
|
+
const at = cell ?? latest.current.active;
|
|
319
|
+
if (!editable || !at)
|
|
320
|
+
return false;
|
|
321
|
+
if (!canEditColumn(cols[at.col]?.def, data[at.row]))
|
|
322
|
+
return false;
|
|
323
|
+
scrollIntoView(at);
|
|
324
|
+
setEditing({ row: at.row, col: at.col, initialText });
|
|
325
|
+
return true;
|
|
326
|
+
}, [editable, scrollIntoView]);
|
|
327
|
+
const stepFrom = (cell, move) => {
|
|
328
|
+
const { rowCount: count, colCount: cols } = latest.current;
|
|
329
|
+
if (move === 'down')
|
|
330
|
+
return cell.row < count - 1 ? { row: cell.row + 1, col: cell.col } : null;
|
|
331
|
+
if (move === 'up')
|
|
332
|
+
return cell.row > 0 ? { row: cell.row - 1, col: cell.col } : null;
|
|
333
|
+
if (move === 'right' || move === 'left')
|
|
334
|
+
return tabTarget(cell, move === 'left', count, cols);
|
|
335
|
+
return null;
|
|
336
|
+
};
|
|
337
|
+
const endEdit = useCallback((value, move = 'none', cancelled = false) => {
|
|
338
|
+
const current = latest.current.editing;
|
|
339
|
+
if (!current)
|
|
340
|
+
return;
|
|
341
|
+
const row = latest.current.displayedRows[current.row];
|
|
342
|
+
const column = latest.current.visibleColumns[current.col]?.def;
|
|
343
|
+
if (!cancelled && row !== undefined && column) {
|
|
344
|
+
const previousValue = valueOf(column, row);
|
|
345
|
+
if (!Object.is(previousValue, value)) {
|
|
346
|
+
const result = props.onCellEdit?.({
|
|
347
|
+
row,
|
|
348
|
+
rowIndex: current.row,
|
|
349
|
+
rowId: getRowId(row, current.row),
|
|
350
|
+
columnId: column.id,
|
|
351
|
+
value,
|
|
352
|
+
previousValue
|
|
353
|
+
});
|
|
354
|
+
if (result === false)
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
setEditing(null);
|
|
359
|
+
rootRef.current?.focus({ preventScroll: true });
|
|
360
|
+
const next = cancelled ? null : stepFrom(current, move);
|
|
361
|
+
if (next)
|
|
362
|
+
moveTo(next);
|
|
363
|
+
}, [props.onCellEdit, getRowId, moveTo]);
|
|
364
|
+
// ── Keyboard ──
|
|
365
|
+
const pageSize = () => {
|
|
366
|
+
const height = scrollRef.current?.clientHeight ?? 0;
|
|
367
|
+
return Math.max(1, Math.floor((height - headerHeight) / rowHeight) || 10);
|
|
368
|
+
};
|
|
369
|
+
const onKeyDown = (event) => {
|
|
370
|
+
props.onKeyDown?.(event, {
|
|
371
|
+
activeCell: active,
|
|
372
|
+
activeRow: active ? displayedRows[active.row] : undefined,
|
|
373
|
+
editing: !!editing
|
|
374
|
+
});
|
|
375
|
+
if (event.defaultPrevented || editing)
|
|
376
|
+
return;
|
|
377
|
+
if (event.target !== rootRef.current)
|
|
378
|
+
return;
|
|
379
|
+
const ctrl = event.ctrlKey || event.metaKey;
|
|
380
|
+
const handled = () => {
|
|
381
|
+
event.preventDefault();
|
|
382
|
+
event.stopPropagation();
|
|
383
|
+
};
|
|
384
|
+
if (ctrl && (event.key === 'a' || event.key === 'A')) {
|
|
385
|
+
if (!multiSelect)
|
|
386
|
+
return;
|
|
387
|
+
selectAll();
|
|
388
|
+
return handled();
|
|
389
|
+
}
|
|
390
|
+
if (ctrl && (event.key === 'c' || event.key === 'C')) {
|
|
391
|
+
// The native copy event (onCopy) fills the clipboard; this covers webviews without one.
|
|
392
|
+
const text = getSelectionText();
|
|
393
|
+
navigator.clipboard?.writeText(text).catch(() => undefined);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (!active)
|
|
397
|
+
return;
|
|
398
|
+
const target = navigate({
|
|
399
|
+
key: event.key,
|
|
400
|
+
ctrl,
|
|
401
|
+
cell: active,
|
|
402
|
+
rowCount,
|
|
403
|
+
colCount,
|
|
404
|
+
pageSize: pageSize(),
|
|
405
|
+
mode: selectionMode
|
|
406
|
+
});
|
|
407
|
+
if (target) {
|
|
408
|
+
moveTo(target, event.shiftKey ? 'extend' : 'replace');
|
|
409
|
+
return handled();
|
|
410
|
+
}
|
|
411
|
+
switch (event.key) {
|
|
412
|
+
case 'Tab': {
|
|
413
|
+
if (tabBehavior !== 'cell' || ctrl || event.altKey)
|
|
414
|
+
return;
|
|
415
|
+
const next = tabTarget(active, event.shiftKey, rowCount, colCount);
|
|
416
|
+
if (!next)
|
|
417
|
+
return;
|
|
418
|
+
moveTo(next);
|
|
419
|
+
return handled();
|
|
420
|
+
}
|
|
421
|
+
case 'Enter': {
|
|
422
|
+
if (ctrl || event.altKey)
|
|
423
|
+
return;
|
|
424
|
+
const row = displayedRows[active.row];
|
|
425
|
+
if (props.onRowActivate && row !== undefined && !event.shiftKey) {
|
|
426
|
+
props.onRowActivate(row, active.row);
|
|
427
|
+
return handled();
|
|
428
|
+
}
|
|
429
|
+
if (!editable)
|
|
430
|
+
return;
|
|
431
|
+
const next = stepFrom(active, event.shiftKey ? 'up' : 'down');
|
|
432
|
+
if (next)
|
|
433
|
+
moveTo(next);
|
|
434
|
+
return handled();
|
|
435
|
+
}
|
|
436
|
+
case 'F2':
|
|
437
|
+
if (startEdit())
|
|
438
|
+
handled();
|
|
439
|
+
return;
|
|
440
|
+
case 'Escape':
|
|
441
|
+
if (ranges.length > 1 ||
|
|
442
|
+
(ranges[0] && !sameRangeAsCell(ranges[0], active, selectionMode))) {
|
|
443
|
+
moveTo(active);
|
|
444
|
+
handled();
|
|
445
|
+
}
|
|
446
|
+
return;
|
|
447
|
+
default:
|
|
448
|
+
if (isTypingKey(event) && startEdit(event.key))
|
|
449
|
+
handled();
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
// ── Mouse and touch ──
|
|
453
|
+
const dragging = useRef(false);
|
|
454
|
+
const longPress = useRef(null);
|
|
455
|
+
useEffect(() => {
|
|
456
|
+
const stop = () => {
|
|
457
|
+
dragging.current = false;
|
|
458
|
+
};
|
|
459
|
+
window.addEventListener('mouseup', stop);
|
|
460
|
+
return () => {
|
|
461
|
+
window.removeEventListener('mouseup', stop);
|
|
462
|
+
if (longPress.current)
|
|
463
|
+
clearTimeout(longPress.current);
|
|
464
|
+
};
|
|
465
|
+
}, []);
|
|
466
|
+
const onCellMouseDown = (event, cell) => {
|
|
467
|
+
if (event.button !== 0)
|
|
468
|
+
return;
|
|
469
|
+
if (editing && sameCell(editing, cell))
|
|
470
|
+
return;
|
|
471
|
+
if (!editing)
|
|
472
|
+
rootRef.current?.focus({ preventScroll: true });
|
|
473
|
+
const how = event.shiftKey ? 'extend' : event.ctrlKey || event.metaKey ? 'add' : 'replace';
|
|
474
|
+
moveTo(cell, how);
|
|
475
|
+
dragging.current = multiSelect && how !== 'add';
|
|
476
|
+
// No text selection while selecting cells.
|
|
477
|
+
event.preventDefault();
|
|
478
|
+
};
|
|
479
|
+
const onCellMouseEnter = (cell) => {
|
|
480
|
+
if (dragging.current)
|
|
481
|
+
moveTo(cell, 'extend');
|
|
482
|
+
};
|
|
483
|
+
const onCellDoubleClick = (cell) => {
|
|
484
|
+
const row = displayedRows[cell.row];
|
|
485
|
+
if (row === undefined)
|
|
486
|
+
return;
|
|
487
|
+
if (editable && canEditColumn(visibleColumns[cell.col]?.def, row)) {
|
|
488
|
+
if (!sameCell(active, cell))
|
|
489
|
+
moveTo(cell);
|
|
490
|
+
startEdit(undefined, cell);
|
|
491
|
+
}
|
|
492
|
+
else
|
|
493
|
+
props.onRowActivate?.(row, cell.row);
|
|
494
|
+
};
|
|
495
|
+
const onCellContextMenu = (event, cell) => {
|
|
496
|
+
const row = displayedRows[cell.row];
|
|
497
|
+
if (!props.onCellContextMenu || row === undefined)
|
|
498
|
+
return;
|
|
499
|
+
event.preventDefault();
|
|
500
|
+
if (!cellSelected(ranges, cell.row, cell.col))
|
|
501
|
+
moveTo(cell);
|
|
502
|
+
props.onCellContextMenu(cell, row, { clientX: event.clientX, clientY: event.clientY });
|
|
503
|
+
};
|
|
504
|
+
const onCellTouchStart = (event, cell) => {
|
|
505
|
+
const touch = event.touches[0];
|
|
506
|
+
if (!props.onCellContextMenu || !touch || event.touches.length > 1)
|
|
507
|
+
return;
|
|
508
|
+
const point = { clientX: touch.clientX, clientY: touch.clientY };
|
|
509
|
+
if (longPress.current)
|
|
510
|
+
clearTimeout(longPress.current);
|
|
511
|
+
longPress.current = setTimeout(() => {
|
|
512
|
+
longPress.current = null;
|
|
513
|
+
const row = latest.current.displayedRows[cell.row];
|
|
514
|
+
if (row === undefined)
|
|
515
|
+
return;
|
|
516
|
+
moveTo(cell);
|
|
517
|
+
props.onCellContextMenu?.(cell, row, point);
|
|
518
|
+
}, LONG_PRESS_MS);
|
|
519
|
+
};
|
|
520
|
+
const cancelLongPress = () => {
|
|
521
|
+
if (longPress.current)
|
|
522
|
+
clearTimeout(longPress.current);
|
|
523
|
+
longPress.current = null;
|
|
524
|
+
};
|
|
525
|
+
// ── Columns: sort, resize, reorder ──
|
|
526
|
+
const toggleSort = (columnId, multi) => {
|
|
527
|
+
const current = sorting.find((item) => item.columnId === columnId);
|
|
528
|
+
const nextItem = !current
|
|
529
|
+
? { columnId, direction: 'asc' }
|
|
530
|
+
: current.direction === 'asc'
|
|
531
|
+
? { columnId, direction: 'desc' }
|
|
532
|
+
: null;
|
|
533
|
+
const others = multi ? sorting.filter((item) => item.columnId !== columnId) : [];
|
|
534
|
+
setSorting(nextItem ? [...others, nextItem] : others);
|
|
535
|
+
};
|
|
536
|
+
const columnStateRef = useRef(columnState);
|
|
537
|
+
columnStateRef.current = columnState;
|
|
538
|
+
const updateColumns = useCallback((change) => setColumnState(change(columnStateRef.current)), [setColumnState]);
|
|
539
|
+
const moveColumn = useCallback((columnId, toIndex) => updateColumns((state) => {
|
|
540
|
+
const order = [
|
|
541
|
+
...state.order.filter((id) => byId.has(id)),
|
|
542
|
+
...columns.map((column) => column.id).filter((id) => !state.order.includes(id))
|
|
543
|
+
].filter((id) => id !== columnId);
|
|
544
|
+
order.splice(clamp(toIndex, 0, order.length), 0, columnId);
|
|
545
|
+
return { ...state, order };
|
|
546
|
+
}), [updateColumns, byId, columns]);
|
|
547
|
+
const resizing = useRef(null);
|
|
548
|
+
const onResizeStart = (event, column) => {
|
|
549
|
+
event.preventDefault();
|
|
550
|
+
event.stopPropagation();
|
|
551
|
+
resizing.current = { id: column.def.id, startX: event.clientX, startWidth: column.width };
|
|
552
|
+
const move = (moveEvent) => {
|
|
553
|
+
const current = resizing.current;
|
|
554
|
+
if (!current)
|
|
555
|
+
return;
|
|
556
|
+
const def = byId.get(current.id);
|
|
557
|
+
const width = clamp(Math.round(current.startWidth + moveEvent.clientX - current.startX), def?.minWidth ?? DEFAULT_MIN_WIDTH, def?.maxWidth ?? Number.MAX_SAFE_INTEGER);
|
|
558
|
+
updateColumns((state) => ({ ...state, widths: { ...state.widths, [current.id]: width } }));
|
|
559
|
+
};
|
|
560
|
+
const up = () => {
|
|
561
|
+
resizing.current = null;
|
|
562
|
+
window.removeEventListener('pointermove', move);
|
|
563
|
+
window.removeEventListener('pointerup', up);
|
|
564
|
+
};
|
|
565
|
+
window.addEventListener('pointermove', move);
|
|
566
|
+
window.addEventListener('pointerup', up);
|
|
567
|
+
};
|
|
568
|
+
const headerDrag = useRef(null);
|
|
569
|
+
const onHeaderPointerDown = (event, column) => {
|
|
570
|
+
if (event.button !== 0)
|
|
571
|
+
return;
|
|
572
|
+
headerDrag.current = { id: column.def.id, x: event.clientX, moved: false, over: null };
|
|
573
|
+
const move = (moveEvent) => {
|
|
574
|
+
const drag = headerDrag.current;
|
|
575
|
+
if (drag && Math.abs(moveEvent.clientX - drag.x) > 4)
|
|
576
|
+
drag.moved = true;
|
|
577
|
+
};
|
|
578
|
+
const up = (upEvent) => {
|
|
579
|
+
window.removeEventListener('pointermove', move);
|
|
580
|
+
window.removeEventListener('pointerup', up);
|
|
581
|
+
const drag = headerDrag.current;
|
|
582
|
+
headerDrag.current = null;
|
|
583
|
+
if (!drag)
|
|
584
|
+
return;
|
|
585
|
+
if (drag.moved && drag.over && drag.over !== drag.id) {
|
|
586
|
+
const from = byId.get(drag.id);
|
|
587
|
+
if (from?.reorderable === false)
|
|
588
|
+
return;
|
|
589
|
+
const target = latest.current.visibleColumns.find((item) => item.def.id === drag.over);
|
|
590
|
+
if (!target || target.def.reorderable === false)
|
|
591
|
+
return;
|
|
592
|
+
// Dropped on a column: takes its place (hidden columns keep theirs).
|
|
593
|
+
const saved = columnStateRef.current.order;
|
|
594
|
+
const fullOrder = [
|
|
595
|
+
...saved.filter((id) => byId.has(id)),
|
|
596
|
+
...columns.map((item) => item.id).filter((id) => !saved.includes(id))
|
|
597
|
+
];
|
|
598
|
+
moveColumn(drag.id, fullOrder.indexOf(drag.over));
|
|
599
|
+
}
|
|
600
|
+
else if (!drag.moved) {
|
|
601
|
+
const def = byId.get(drag.id);
|
|
602
|
+
if (def?.sortable !== false)
|
|
603
|
+
toggleSort(drag.id, upEvent.shiftKey);
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
window.addEventListener('pointermove', move);
|
|
607
|
+
window.addEventListener('pointerup', up);
|
|
608
|
+
};
|
|
609
|
+
// ── Imperative handle ──
|
|
610
|
+
useImperativeHandle(ref, () => ({
|
|
611
|
+
focus: () => rootRef.current?.focus(),
|
|
612
|
+
scrollToRow: (rowIndex) => virtualizer.scrollToIndex(clamp(rowIndex, 0, Math.max(0, latest.current.rowCount - 1))),
|
|
613
|
+
getActiveCell: () => latest.current.active,
|
|
614
|
+
setActiveCell: (cell) => (cell ? moveTo(cell) : setActive(null)),
|
|
615
|
+
selectAll,
|
|
616
|
+
getSelectionText,
|
|
617
|
+
paste,
|
|
618
|
+
startEdit: (initialText) => void startEdit(initialText),
|
|
619
|
+
getColumnState: () => columnStateRef.current,
|
|
620
|
+
getDisplayedRows: () => latest.current.displayedRows,
|
|
621
|
+
moveColumn,
|
|
622
|
+
setColumnWidth: (columnId, width) => updateColumns((state) => {
|
|
623
|
+
const widths = { ...state.widths };
|
|
624
|
+
if (width === null)
|
|
625
|
+
delete widths[columnId];
|
|
626
|
+
else
|
|
627
|
+
widths[columnId] = width;
|
|
628
|
+
return { ...state, widths };
|
|
629
|
+
}),
|
|
630
|
+
setColumnHidden: (columnId, hidden) => updateColumns((state) => ({
|
|
631
|
+
...state,
|
|
632
|
+
hidden: hidden
|
|
633
|
+
? [...state.hidden.filter((id) => id !== columnId), columnId]
|
|
634
|
+
: state.hidden.filter((id) => id !== columnId)
|
|
635
|
+
})),
|
|
636
|
+
setColumnFrozen: (columnId, frozen) => updateColumns((state) => ({
|
|
637
|
+
...state,
|
|
638
|
+
frozenStart: frozen === 'start'
|
|
639
|
+
? [...state.frozenStart.filter((id) => id !== columnId), columnId]
|
|
640
|
+
: state.frozenStart.filter((id) => id !== columnId),
|
|
641
|
+
frozenEnd: frozen === 'end'
|
|
642
|
+
? [...state.frozenEnd.filter((id) => id !== columnId), columnId]
|
|
643
|
+
: state.frozenEnd.filter((id) => id !== columnId)
|
|
644
|
+
}))
|
|
645
|
+
}), [
|
|
646
|
+
virtualizer,
|
|
647
|
+
moveTo,
|
|
648
|
+
setActive,
|
|
649
|
+
selectAll,
|
|
650
|
+
getSelectionText,
|
|
651
|
+
paste,
|
|
652
|
+
startEdit,
|
|
653
|
+
moveColumn,
|
|
654
|
+
updateColumns
|
|
655
|
+
]);
|
|
656
|
+
useEffect(() => {
|
|
657
|
+
// On mount only.
|
|
658
|
+
if (props.autoFocus)
|
|
659
|
+
rootRef.current?.focus();
|
|
660
|
+
}, []);
|
|
661
|
+
// ── Render ──
|
|
662
|
+
const totalWidth = visibleColumns.reduce((sum, column) => sum + column.width, 0);
|
|
663
|
+
const cellId = (cell) => `${gridId}-r${cell.row}-c${cell.col}`;
|
|
664
|
+
const columnStyle = (column) => ({
|
|
665
|
+
width: column.width,
|
|
666
|
+
minWidth: column.width,
|
|
667
|
+
flexGrow: column.def.grow ?? 0,
|
|
668
|
+
...(column.pinned === 'start'
|
|
669
|
+
? { position: 'sticky', left: column.offset, zIndex: 1 }
|
|
670
|
+
: column.pinned === 'end'
|
|
671
|
+
? { position: 'sticky', right: column.offset, zIndex: 1 }
|
|
672
|
+
: {})
|
|
673
|
+
});
|
|
674
|
+
const alignClass = (column) => `rdg-align-${column.align ?? 'start'}`;
|
|
675
|
+
const sortOf = (columnId) => sorting.find((item) => item.columnId === columnId);
|
|
676
|
+
const header = (_jsx("div", { role: "rowgroup", className: "rdg-header", style: { height: headerHeight, minWidth: totalWidth }, children: _jsx("div", { role: "row", "aria-rowindex": 1, className: "rdg-header-row", style: { height: headerHeight }, children: visibleColumns.map((column, index) => {
|
|
677
|
+
const sort = sortOf(column.def.id);
|
|
678
|
+
return (_jsxs("div", { role: "columnheader", "aria-colindex": index + 1, "aria-sort": sort ? (sort.direction === 'asc' ? 'ascending' : 'descending') : undefined, title: column.def.title, "data-column-id": column.def.id, "data-pinned": column.pinned || undefined, className: [
|
|
679
|
+
'rdg-header-cell',
|
|
680
|
+
alignClass(column.def),
|
|
681
|
+
column.def.sortable !== false && 'rdg-sortable',
|
|
682
|
+
active?.col === index && 'rdg-header-cell-active',
|
|
683
|
+
column.def.headerClassName
|
|
684
|
+
]
|
|
685
|
+
.filter(Boolean)
|
|
686
|
+
.join(' '), style: columnStyle(column), onPointerDown: (event) => onHeaderPointerDown(event, column), onPointerEnter: () => {
|
|
687
|
+
if (headerDrag.current)
|
|
688
|
+
headerDrag.current.over = column.def.id;
|
|
689
|
+
}, children: [_jsx("span", { className: "rdg-header-label", children: column.def.header }), sort && (_jsxs("span", { className: "rdg-sort-indicator", "aria-hidden": "true", children: [sort.direction === 'asc' ? '▲' : '▼', sorting.length > 1 ? sorting.indexOf(sort) + 1 : ''] })), column.def.resizable !== false && (_jsx("span", { className: "rdg-resize-handle", "aria-hidden": "true", onPointerDown: (event) => onResizeStart(event, column), onDoubleClick: () => updateColumns((state) => {
|
|
690
|
+
const widths = { ...state.widths };
|
|
691
|
+
delete widths[column.def.id];
|
|
692
|
+
return { ...state, widths };
|
|
693
|
+
}) }))] }, column.def.id));
|
|
694
|
+
}) }) }));
|
|
695
|
+
const renderCell = (row, rowIndex, column, colIndex) => {
|
|
696
|
+
const value = valueOf(column.def, row);
|
|
697
|
+
const text = textOf(column.def, row, value);
|
|
698
|
+
const position = { row: rowIndex, col: colIndex };
|
|
699
|
+
const isActive = sameCell(active, position);
|
|
700
|
+
const selected = selectionMode === 'row'
|
|
701
|
+
? rowSelected(ranges, rowIndex)
|
|
702
|
+
: cellSelected(ranges, rowIndex, colIndex);
|
|
703
|
+
const isEditing = editing?.row === rowIndex && editing.col === colIndex;
|
|
704
|
+
const extra = typeof column.def.cellClassName === 'function'
|
|
705
|
+
? column.def.cellClassName(row)
|
|
706
|
+
: column.def.cellClassName;
|
|
707
|
+
let content;
|
|
708
|
+
if (isEditing) {
|
|
709
|
+
const editorProps = {
|
|
710
|
+
row,
|
|
711
|
+
rowIndex,
|
|
712
|
+
column: column.def,
|
|
713
|
+
value,
|
|
714
|
+
initialText: editing.initialText,
|
|
715
|
+
commit: (next, move) => endEdit(next, move),
|
|
716
|
+
cancel: () => endEdit(undefined, 'none', true)
|
|
717
|
+
};
|
|
718
|
+
content = column.def.editor ? (_jsx("div", { className: "rdg-editor-slot", onKeyDown: (event) => {
|
|
719
|
+
if (!event.defaultPrevented && event.key === 'Escape') {
|
|
720
|
+
event.preventDefault();
|
|
721
|
+
editorProps.cancel();
|
|
722
|
+
}
|
|
723
|
+
event.stopPropagation();
|
|
724
|
+
}, children: column.def.editor(editorProps) })) : (_jsx(DefaultEditor, { ...editorProps, text: text, align: column.def.align ?? 'start', parse: (input) => (column.def.parse ? column.def.parse(input, row) : input) }));
|
|
725
|
+
}
|
|
726
|
+
else if (column.def.cell) {
|
|
727
|
+
content = column.def.cell({
|
|
728
|
+
row,
|
|
729
|
+
rowIndex,
|
|
730
|
+
rowId: getRowId(row, rowIndex),
|
|
731
|
+
column: column.def,
|
|
732
|
+
value,
|
|
733
|
+
text,
|
|
734
|
+
selected,
|
|
735
|
+
active: isActive
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
else
|
|
739
|
+
content = _jsx("span", { className: "rdg-cell-text", children: text });
|
|
740
|
+
return (_jsx("div", { id: cellId(position), role: "gridcell", "aria-colindex": colIndex + 1, "aria-selected": selectionMode === 'cell' ? selected : undefined, "aria-readonly": editable ? !canEditColumn(column.def, row) : undefined, title: typeof content === 'string' ? content : undefined, "data-active": isActive || undefined, "data-editing": isEditing || undefined, "data-pinned": column.pinned || undefined, className: ['rdg-cell', alignClass(column.def), extra].filter(Boolean).join(' '), style: columnStyle(column), onMouseDown: (event) => onCellMouseDown(event, position), onMouseEnter: () => onCellMouseEnter(position), onDoubleClick: () => onCellDoubleClick(position), onContextMenu: (event) => onCellContextMenu(event, position), onTouchStart: (event) => onCellTouchStart(event, position), onTouchMove: cancelLongPress, onTouchEnd: cancelLongPress, children: content }, column.def.id));
|
|
741
|
+
};
|
|
742
|
+
const items = virtualizer.getVirtualItems();
|
|
743
|
+
const body = rowCount === 0 ? (_jsx("div", { className: "rdg-empty", role: "presentation", children: props.empty })) : (_jsx("div", { role: "rowgroup", className: "rdg-body", style: { height: virtualizer.getTotalSize(), minWidth: totalWidth }, children: items.map((item) => {
|
|
744
|
+
const row = displayedRows[item.index];
|
|
745
|
+
if (row === undefined)
|
|
746
|
+
return null;
|
|
747
|
+
const rowIndex = item.index;
|
|
748
|
+
const selected = rowSelected(ranges, rowIndex);
|
|
749
|
+
return (_jsx("div", { role: "row", "aria-rowindex": rowIndex + 2, "aria-selected": selectionMode === 'row' ? selected : undefined, "data-active": active?.row === rowIndex || undefined, className: [
|
|
750
|
+
'rdg-row',
|
|
751
|
+
rowIndex % 2 === 1 && 'rdg-row-odd',
|
|
752
|
+
props.rowClassName?.(row, rowIndex)
|
|
753
|
+
]
|
|
754
|
+
.filter(Boolean)
|
|
755
|
+
.join(' '), style: { height: rowHeight, transform: `translateY(${item.start - headerHeight}px)` }, children: visibleColumns.map((column, colIndex) => renderCell(row, rowIndex, column, colIndex)) }, displayed[rowIndex]?.id ?? rowIndex));
|
|
756
|
+
}) }));
|
|
757
|
+
return (_jsx("div", { ref: rootRef, id: gridId, role: "grid", tabIndex: 0, "aria-label": props['aria-label'], "aria-labelledby": props['aria-labelledby'], "aria-rowcount": rowCount + 1, "aria-colcount": colCount, "aria-multiselectable": multiSelect || undefined, "aria-readonly": !editable || undefined, "aria-activedescendant": active && !editing ? cellId(active) : undefined, "data-selection-mode": selectionMode, className: ['rdg', props.className].filter(Boolean).join(' '), style: props.style, onKeyDown: onKeyDown, onCopy: (event) => {
|
|
758
|
+
if (editing || event.target !== rootRef.current)
|
|
759
|
+
return;
|
|
760
|
+
event.clipboardData.setData('text/plain', getSelectionText());
|
|
761
|
+
event.preventDefault();
|
|
762
|
+
}, onPaste: (event) => {
|
|
763
|
+
if (editing || event.target !== rootRef.current || !editable)
|
|
764
|
+
return;
|
|
765
|
+
event.preventDefault();
|
|
766
|
+
paste(event.clipboardData.getData('text/plain'));
|
|
767
|
+
}, children: _jsxs("div", { ref: scrollRef, className: "rdg-scroller", children: [header, body] }) }));
|
|
768
|
+
}
|
|
769
|
+
const sameRangeAsCell = (range, cell, mode) => {
|
|
770
|
+
const bounds = boundsOf(range);
|
|
771
|
+
return mode === 'row'
|
|
772
|
+
? bounds.top === cell.row && bounds.bottom === cell.row
|
|
773
|
+
: bounds.top === cell.row &&
|
|
774
|
+
bounds.bottom === cell.row &&
|
|
775
|
+
bounds.left === cell.col &&
|
|
776
|
+
bounds.right === cell.col;
|
|
777
|
+
};
|
|
778
|
+
/**
|
|
779
|
+
* Keyboard-first data grid: virtualized rows, row or cell selection with ranges, clipboard, in-place
|
|
780
|
+
* editing, sortable / resizable / reorderable / hideable / frozen columns. See README.
|
|
781
|
+
*/
|
|
782
|
+
export const DataGrid = forwardRef(DataGridInner);
|