@gridkitjs/react 0.0.0-dev-20261109132710 → 0.0.0-dev-20261109134250

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/dist/index.d.ts CHANGED
@@ -144,7 +144,10 @@ interface DataGridApi<Row> extends GridExportApi, GridPrintApi, GridPanelApi {
144
144
  getSelectedRows(): readonly ResolvedRow<Row>[];
145
145
  getColumnSelection(): SelectionState;
146
146
  getCellSelection(): CellSelectionState;
147
- /** The cell currently holding the grid's single tab stop. */
147
+ /**
148
+ * The cell currently holding the grid's single tab stop, numbered as
149
+ * `focusCell` takes it: `rowIndex` is `-1` on the header.
150
+ */
148
151
  getFocusedCell(): {
149
152
  rowIndex: number;
150
153
  columnIndex: number;
@@ -162,6 +165,12 @@ interface DataGridApi<Row> extends GridExportApi, GridPrintApi, GridPanelApi {
162
165
  * grid re-renders once.
163
166
  */
164
167
  restoreState(saved: Partial<GridState<Row>> | Partial<SerializedGridState<Row>>): void;
168
+ /**
169
+ * Moves the grid's tab stop, and the browser's focus, to body row
170
+ * `rowIndex`'s cell at `columnIndex`, or to the header's cell at
171
+ * `columnIndex` when `rowIndex` is negative. Either index is held inside
172
+ * the grid.
173
+ */
165
174
  focusCell(rowIndex: number, columnIndex: number): void;
166
175
  clearSelection(): void;
167
176
  selectAllRows(): void;
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@ import {
10
10
  useState as useState18
11
11
  } from "react";
12
12
  import {
13
+ bodyFocus as bodyFocus3,
14
+ headerFocus as headerFocus3,
13
15
  moveColumnBefore,
14
16
  resolveColumnHeaderText as resolveColumnHeaderText2,
15
17
  resolveMessages,
@@ -27,6 +29,7 @@ function GridColgroup({ columns }) {
27
29
  import { createPortal } from "react-dom";
28
30
  import {
29
31
  buildKeyShortcuts,
32
+ headerFocus,
30
33
  intentOf,
31
34
  isColumnFiltered,
32
35
  KEYBOARD_STEP,
@@ -105,83 +108,6 @@ function isOwnEvent(event) {
105
108
  return event.target instanceof Node && event.currentTarget.contains(event.target);
106
109
  }
107
110
 
108
- // src/DataGrid/useGridNavigation.ts
109
- import {
110
- useEffect,
111
- useLayoutEffect,
112
- useState,
113
- useSyncExternalStore
114
- } from "react";
115
- import { HEADER_ROW } from "@gridkitjs/core";
116
- import {
117
- createGridFocus,
118
- findBodyRow,
119
- measurePageSize
120
- } from "@gridkitjs/dom";
121
- function tabIndexFor(columnIndex, focusedColumnIndex) {
122
- return columnIndex === focusedColumnIndex ? 0 : -1;
123
- }
124
- function useGridNavigation({
125
- gridRef,
126
- headerTableRef,
127
- bodyTableRef,
128
- rowCount,
129
- columnCount,
130
- isSkippableRow,
131
- virtualRange
132
- }) {
133
- const options = {
134
- rowCount,
135
- columnCount,
136
- isSkippableRow,
137
- virtualRange
138
- };
139
- const [gridFocus] = useState(() => createGridFocus(options));
140
- gridFocus.setOptions(options);
141
- const focus = useSyncExternalStore(
142
- gridFocus.subscribe,
143
- gridFocus.getFocus,
144
- gridFocus.getFocus
145
- );
146
- const [rows] = useState(() => ({
147
- getHeaderRow: () => headerTableRef.current?.tHead?.rows[0] ?? null,
148
- getBodyRow: (rowIndex) => findBodyRow(bodyTableRef.current, rowIndex)
149
- }));
150
- useLayoutEffect(
151
- () => gridFocus.attach(rows, gridRef.current),
152
- [gridFocus, rows, gridRef]
153
- );
154
- useEffect(() => {
155
- gridFocus.focusDrawnCell(focus);
156
- });
157
- return {
158
- focus,
159
- tabIndexFor: (rowIndex, columnIndex) => tabIndexFor(
160
- columnIndex,
161
- rowIndex === focus.rowIndex ? focus.columnIndex : null
162
- ),
163
- focusCell: gridFocus.focusCell,
164
- focusCellNow: (rowIndex, columnIndex) => {
165
- gridFocus.focusCell(rowIndex, columnIndex);
166
- gridFocus.focusDrawnCell(gridFocus.getFocus());
167
- },
168
- tabStopCell: () => {
169
- const { rowIndex, columnIndex } = gridFocus.getFocus();
170
- const cells = (rowIndex === HEADER_ROW ? rows.getHeaderRow() : rows.getBodyRow(rowIndex))?.cells;
171
- return cells?.[Math.min(columnIndex, cells.length - 1)] ?? null;
172
- },
173
- onKeyDown: (event) => {
174
- if (gridFocus.handleKey(
175
- event.key,
176
- event,
177
- measurePageSize(bodyTableRef.current)
178
- )) {
179
- event.preventDefault();
180
- }
181
- }
182
- };
183
- }
184
-
185
111
  // src/DataGrid/useGridSelection.ts
186
112
  import { useMemo } from "react";
187
113
  function keyboardSelectIntent(event) {
@@ -281,14 +207,14 @@ function GridHeader({
281
207
  role: "columnheader",
282
208
  "data-gridkit-column": entry.id,
283
209
  "aria-colindex": index + 1,
284
- tabIndex: nav.tabIndexFor(HEADER_ROW, index),
210
+ tabIndex: nav.tabIndexFor(headerFocus(index)),
285
211
  ...ariaAttr(
286
212
  selection.columnMode !== false,
287
213
  "aria-selected",
288
214
  selected
289
215
  ),
290
216
  onFocus: () => {
291
- nav.focusCell(HEADER_ROW, index);
217
+ nav.focusCell(headerFocus(index));
292
218
  },
293
219
  onClick: (event) => {
294
220
  if (drag.justDragged(entry.id)) {
@@ -508,6 +434,7 @@ import {
508
434
  useState as useState2
509
435
  } from "react";
510
436
  import {
437
+ bodyFocus,
511
438
  groupRowId,
512
439
  intentOf as intentOf2
513
440
  } from "@gridkitjs/core";
@@ -517,6 +444,88 @@ import {
517
444
  groupRowFrom
518
445
  } from "@gridkitjs/dom";
519
446
 
447
+ // src/DataGrid/useGridNavigation.ts
448
+ import {
449
+ useEffect,
450
+ useLayoutEffect,
451
+ useState,
452
+ useSyncExternalStore
453
+ } from "react";
454
+ import {
455
+ createGridFocus,
456
+ findBodyRow,
457
+ measurePageSize
458
+ } from "@gridkitjs/dom";
459
+ function tabIndexFor(columnIndex, focusedColumnIndex) {
460
+ return columnIndex === focusedColumnIndex ? 0 : -1;
461
+ }
462
+ function focusedColumnInRow(focus, rowIndex) {
463
+ return focus.area === "body" && focus.rowIndex === rowIndex ? focus.columnIndex : null;
464
+ }
465
+ function sameCell(a, b) {
466
+ if (a.columnIndex !== b.columnIndex) {
467
+ return false;
468
+ }
469
+ return a.area === "header" ? b.area === "header" : focusedColumnInRow(b, a.rowIndex) !== null;
470
+ }
471
+ function useGridNavigation({
472
+ gridRef,
473
+ headerTableRef,
474
+ bodyTableRef,
475
+ rowCount,
476
+ columnCount,
477
+ isSkippableRow,
478
+ virtualRange
479
+ }) {
480
+ const options = {
481
+ rowCount,
482
+ columnCount,
483
+ isSkippableRow,
484
+ virtualRange
485
+ };
486
+ const [gridFocus] = useState(() => createGridFocus(options));
487
+ gridFocus.setOptions(options);
488
+ const focus = useSyncExternalStore(
489
+ gridFocus.subscribe,
490
+ gridFocus.getFocus,
491
+ gridFocus.getFocus
492
+ );
493
+ const [rows] = useState(() => ({
494
+ getHeaderRow: () => headerTableRef.current?.tHead?.rows[0] ?? null,
495
+ getBodyRow: (rowIndex) => findBodyRow(bodyTableRef.current, rowIndex)
496
+ }));
497
+ useLayoutEffect(
498
+ () => gridFocus.attach(rows, gridRef.current),
499
+ [gridFocus, rows, gridRef]
500
+ );
501
+ useEffect(() => {
502
+ gridFocus.focusDrawnCell(focus);
503
+ });
504
+ return {
505
+ focus,
506
+ tabIndexFor: (cell) => sameCell(cell, focus) ? 0 : -1,
507
+ focusCell: gridFocus.focusCell,
508
+ focusCellNow: (next) => {
509
+ gridFocus.focusCell(next);
510
+ gridFocus.focusDrawnCell(gridFocus.getFocus());
511
+ },
512
+ tabStopCell: () => {
513
+ const stop = gridFocus.getFocus();
514
+ const cells = (stop.area === "header" ? rows.getHeaderRow() : rows.getBodyRow(stop.rowIndex))?.cells;
515
+ return cells?.[Math.min(stop.columnIndex, cells.length - 1)] ?? null;
516
+ },
517
+ onKeyDown: (event) => {
518
+ if (gridFocus.handleKey(
519
+ event.key,
520
+ event,
521
+ measurePageSize(bodyTableRef.current)
522
+ )) {
523
+ event.preventDefault();
524
+ }
525
+ }
526
+ };
527
+ }
528
+
520
529
  // src/DataGrid/rowProps.ts
521
530
  var NO_ATTRIBUTES = {};
522
531
  var NO_ROW_PROPS = {
@@ -936,12 +945,14 @@ function GridBody({
936
945
  onFocus: (event) => {
937
946
  const group = groupRowFrom(event.target);
938
947
  if (group !== null) {
939
- nav.focusCell(group.rowIndex, 0);
948
+ nav.focusCell(bodyFocus(group.rowIndex, 0));
940
949
  return;
941
950
  }
942
951
  const found = targetOf(event.target);
943
952
  if (found === null) return;
944
- nav.focusCell(found.position.rowIndex, found.position.columnIndex);
953
+ nav.focusCell(
954
+ bodyFocus(found.position.rowIndex, found.position.columnIndex)
955
+ );
945
956
  },
946
957
  onClick: (event) => {
947
958
  const group = groupRowFrom(event.target);
@@ -1020,7 +1031,7 @@ function GridBody({
1020
1031
  selectedColumnIds: selection.selectedColumnIds,
1021
1032
  selected: selection.selectedRowIds.has(entry.rowId),
1022
1033
  selectedColumnId: selectedCell?.rowId === entry.rowId ? selectedCell.columnId : null,
1023
- focusedColumnIndex: nav.focus.rowIndex === entry.rowIndex ? nav.focus.columnIndex : null,
1034
+ focusedColumnIndex: focusedColumnInRow(nav.focus, entry.rowIndex),
1024
1035
  rowsSelectable: rowMode !== false,
1025
1036
  cellsSelectable: cellMode !== false,
1026
1037
  contextMenu: cellMenu,
@@ -1069,7 +1080,7 @@ function GridBody({
1069
1080
  definitions,
1070
1081
  locale,
1071
1082
  cellMessages,
1072
- focused: nav.focus.rowIndex === entry.rowIndex,
1083
+ focused: focusedColumnInRow(nav.focus, entry.rowIndex) !== null,
1073
1084
  contextMenu: groupMenu
1074
1085
  },
1075
1086
  entry.groupId
@@ -1362,7 +1373,7 @@ function useDragReorder({
1362
1373
  (signal) => startDragReorder(id, grabElement, event, {
1363
1374
  resolveTarget: (clientX, clientY, movedId) => handlers.current.resolveTarget(clientX, clientY, movedId),
1364
1375
  onState(state) {
1365
- if (state.draggedId !== null) {
1376
+ if (state.kind === "dragging") {
1366
1377
  openedDragId.current = state.draggedId;
1367
1378
  }
1368
1379
  setDrag(state);
@@ -1377,10 +1388,11 @@ function useDragReorder({
1377
1388
  openedDragId.current = null;
1378
1389
  }
1379
1390
  }
1391
+ const dragging = drag.kind === "dragging" ? drag : null;
1380
1392
  return {
1381
- draggedId: drag.draggedId,
1382
- dropTarget: drag.dropTarget,
1383
- ghostTransform: drag.ghostTransform,
1393
+ draggedId: dragging?.draggedId ?? null,
1394
+ dropTarget: dragging?.dropTarget ?? null,
1395
+ ghostTransform: dragging?.ghostTransform ?? null,
1384
1396
  startDrag,
1385
1397
  justDragged
1386
1398
  };
@@ -1697,13 +1709,13 @@ function useGridCopy({
1697
1709
  return command !== void 0 && runCommand(command, commands.contextFor(target));
1698
1710
  }
1699
1711
  function focusedCell() {
1700
- const { rowIndex, columnIndex } = nav.focus;
1701
- if (rowIndex === HEADER_ROW) {
1712
+ const { focus } = nav;
1713
+ if (focus.area === "header") {
1702
1714
  return null;
1703
1715
  }
1704
1716
  const { pageRows, columns } = controller.getView();
1705
- const entry = pageRows[rowIndex];
1706
- const column = columns[columnIndex];
1717
+ const entry = pageRows[focus.rowIndex];
1718
+ const column = columns[focus.columnIndex];
1707
1719
  return entry === void 0 || "kind" in entry || column === void 0 ? null : { area: "cell", rowId: entry.rowId, columnId: column.id };
1708
1720
  }
1709
1721
  return (event) => {
@@ -2296,7 +2308,8 @@ import {
2296
2308
 
2297
2309
  // src/DataGrid/contextMenu/contextMenuPlaces.ts
2298
2310
  import {
2299
- HEADER_ROW as HEADER_ROW2
2311
+ bodyFocus as bodyFocus2,
2312
+ headerFocus as headerFocus2
2300
2313
  } from "@gridkitjs/core";
2301
2314
  import {
2302
2315
  bodyCellFrom as bodyCellFrom2,
@@ -2306,11 +2319,11 @@ import {
2306
2319
  } from "@gridkitjs/dom";
2307
2320
  function headerCell(parts, columnIndex) {
2308
2321
  const element = parts.headerTable?.tHead?.rows[0]?.cells[columnIndex];
2309
- return element === void 0 ? null : { element, rowIndex: HEADER_ROW2, columnIndex };
2322
+ return element === void 0 ? null : { element, focus: headerFocus2(columnIndex) };
2310
2323
  }
2311
2324
  function bodyCell(parts, rowIndex, columnIndex) {
2312
2325
  const element = findBodyRow2(parts.bodyTable, rowIndex)?.cells[columnIndex];
2313
- return element === void 0 ? null : { element, rowIndex, columnIndex };
2326
+ return element === void 0 ? null : { element, focus: bodyFocus2(rowIndex, columnIndex) };
2314
2327
  }
2315
2328
  function viewOf(element, parts) {
2316
2329
  const root = parts.root?.getBoundingClientRect();
@@ -2505,7 +2518,7 @@ function useContextMenu({
2505
2518
  }
2506
2519
  if (cell !== null && document.activeElement !== cell.element) {
2507
2520
  cell.element.focus({ preventScroll: true });
2508
- nav.focusCellNow(cell.rowIndex, cell.columnIndex);
2521
+ nav.focusCellNow(cell.focus);
2509
2522
  }
2510
2523
  openings.current += 1;
2511
2524
  overlay.open({
@@ -4521,6 +4534,19 @@ function selectedRowsReader(controller) {
4521
4534
  return last.selected;
4522
4535
  };
4523
4536
  }
4537
+ function focusAt(rowIndex, columnIndex) {
4538
+ return rowIndex < 0 ? headerFocus3(columnIndex) : bodyFocus3(rowIndex, columnIndex);
4539
+ }
4540
+ function focusedCellReader() {
4541
+ let last;
4542
+ return (focus) => {
4543
+ if (last?.focus !== focus) {
4544
+ const rowIndex = focus.area === "header" ? -1 : focus.rowIndex;
4545
+ last = { focus, cell: { rowIndex, columnIndex: focus.columnIndex } };
4546
+ }
4547
+ return last.cell;
4548
+ };
4549
+ }
4524
4550
  function DataGrid(props) {
4525
4551
  const {
4526
4552
  borders,
@@ -4580,6 +4606,7 @@ function DataGrid(props) {
4580
4606
  });
4581
4607
  const [readPagination] = useState18(() => paginationReader(controller));
4582
4608
  const [readSelectedRows] = useState18(() => selectedRowsReader(controller));
4609
+ const [readFocusedCell] = useState18(focusedCellReader);
4583
4610
  const virtualizer = useRowVirtualizer({
4584
4611
  enabled: virtualized,
4585
4612
  rows: view.pageRows,
@@ -4699,7 +4726,7 @@ function DataGrid(props) {
4699
4726
  announce(
4700
4727
  messages.columnMoved(columnName(movedId), position + 1, shown.length)
4701
4728
  );
4702
- nav.focusCell(HEADER_ROW, position);
4729
+ nav.focusCell(headerFocus3(position));
4703
4730
  }
4704
4731
  const drag = useColumnDrag({
4705
4732
  order: displayedIds,
@@ -4771,10 +4798,12 @@ function DataGrid(props) {
4771
4798
  getSelectedRows: readSelectedRows,
4772
4799
  getColumnSelection: () => controller.getState().columnSelection,
4773
4800
  getCellSelection: () => controller.getState().cellSelection,
4774
- getFocusedCell: () => nav.focus,
4801
+ getFocusedCell: () => readFocusedCell(nav.focus),
4775
4802
  getState: controller.getState,
4776
4803
  restoreState: controller.restoreState,
4777
- focusCell: nav.focusCell,
4804
+ focusCell: (rowIndex, columnIndex) => {
4805
+ nav.focusCell(focusAt(rowIndex, columnIndex));
4806
+ },
4778
4807
  clearSelection: controller.clearSelection,
4779
4808
  selectAllRows: controller.selectAllRows,
4780
4809
  selectRows: controller.selectRows,