@gridkitjs/react 0.0.0-dev-20261109133348 → 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
@@ -1698,13 +1709,13 @@ function useGridCopy({
1698
1709
  return command !== void 0 && runCommand(command, commands.contextFor(target));
1699
1710
  }
1700
1711
  function focusedCell() {
1701
- const { rowIndex, columnIndex } = nav.focus;
1702
- if (rowIndex === HEADER_ROW) {
1712
+ const { focus } = nav;
1713
+ if (focus.area === "header") {
1703
1714
  return null;
1704
1715
  }
1705
1716
  const { pageRows, columns } = controller.getView();
1706
- const entry = pageRows[rowIndex];
1707
- const column = columns[columnIndex];
1717
+ const entry = pageRows[focus.rowIndex];
1718
+ const column = columns[focus.columnIndex];
1708
1719
  return entry === void 0 || "kind" in entry || column === void 0 ? null : { area: "cell", rowId: entry.rowId, columnId: column.id };
1709
1720
  }
1710
1721
  return (event) => {
@@ -2297,7 +2308,8 @@ import {
2297
2308
 
2298
2309
  // src/DataGrid/contextMenu/contextMenuPlaces.ts
2299
2310
  import {
2300
- HEADER_ROW as HEADER_ROW2
2311
+ bodyFocus as bodyFocus2,
2312
+ headerFocus as headerFocus2
2301
2313
  } from "@gridkitjs/core";
2302
2314
  import {
2303
2315
  bodyCellFrom as bodyCellFrom2,
@@ -2307,11 +2319,11 @@ import {
2307
2319
  } from "@gridkitjs/dom";
2308
2320
  function headerCell(parts, columnIndex) {
2309
2321
  const element = parts.headerTable?.tHead?.rows[0]?.cells[columnIndex];
2310
- return element === void 0 ? null : { element, rowIndex: HEADER_ROW2, columnIndex };
2322
+ return element === void 0 ? null : { element, focus: headerFocus2(columnIndex) };
2311
2323
  }
2312
2324
  function bodyCell(parts, rowIndex, columnIndex) {
2313
2325
  const element = findBodyRow2(parts.bodyTable, rowIndex)?.cells[columnIndex];
2314
- return element === void 0 ? null : { element, rowIndex, columnIndex };
2326
+ return element === void 0 ? null : { element, focus: bodyFocus2(rowIndex, columnIndex) };
2315
2327
  }
2316
2328
  function viewOf(element, parts) {
2317
2329
  const root = parts.root?.getBoundingClientRect();
@@ -2506,7 +2518,7 @@ function useContextMenu({
2506
2518
  }
2507
2519
  if (cell !== null && document.activeElement !== cell.element) {
2508
2520
  cell.element.focus({ preventScroll: true });
2509
- nav.focusCellNow(cell.rowIndex, cell.columnIndex);
2521
+ nav.focusCellNow(cell.focus);
2510
2522
  }
2511
2523
  openings.current += 1;
2512
2524
  overlay.open({
@@ -4522,6 +4534,19 @@ function selectedRowsReader(controller) {
4522
4534
  return last.selected;
4523
4535
  };
4524
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
+ }
4525
4550
  function DataGrid(props) {
4526
4551
  const {
4527
4552
  borders,
@@ -4581,6 +4606,7 @@ function DataGrid(props) {
4581
4606
  });
4582
4607
  const [readPagination] = useState18(() => paginationReader(controller));
4583
4608
  const [readSelectedRows] = useState18(() => selectedRowsReader(controller));
4609
+ const [readFocusedCell] = useState18(focusedCellReader);
4584
4610
  const virtualizer = useRowVirtualizer({
4585
4611
  enabled: virtualized,
4586
4612
  rows: view.pageRows,
@@ -4700,7 +4726,7 @@ function DataGrid(props) {
4700
4726
  announce(
4701
4727
  messages.columnMoved(columnName(movedId), position + 1, shown.length)
4702
4728
  );
4703
- nav.focusCell(HEADER_ROW, position);
4729
+ nav.focusCell(headerFocus3(position));
4704
4730
  }
4705
4731
  const drag = useColumnDrag({
4706
4732
  order: displayedIds,
@@ -4772,10 +4798,12 @@ function DataGrid(props) {
4772
4798
  getSelectedRows: readSelectedRows,
4773
4799
  getColumnSelection: () => controller.getState().columnSelection,
4774
4800
  getCellSelection: () => controller.getState().cellSelection,
4775
- getFocusedCell: () => nav.focus,
4801
+ getFocusedCell: () => readFocusedCell(nav.focus),
4776
4802
  getState: controller.getState,
4777
4803
  restoreState: controller.restoreState,
4778
- focusCell: nav.focusCell,
4804
+ focusCell: (rowIndex, columnIndex) => {
4805
+ nav.focusCell(focusAt(rowIndex, columnIndex));
4806
+ },
4779
4807
  clearSelection: controller.clearSelection,
4780
4808
  selectAllRows: controller.selectAllRows,
4781
4809
  selectRows: controller.selectRows,