Skip to content

Remove unnecessary | undefined from types - #4219

Merged
nstepien merged 2 commits into
mainfrom
narrow-keydown-row-type
Oct 21, 2026
Merged

nstepien merged 2 commits into
mainfrom
narrow-keydown-row-type

Conversation

@nstepien

@nstepien nstepien commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Narrows ActiveCellKeyDownArgs.row from TRow | undefined to TRow. The grid only calls onCellKeyDown when the active position is in the viewport (activePositionIsInViewport), so rows[rowIdx] is always defined. The type was TRow until #3979 widened it.

The other fields that looked like candidates still need | undefined:

  • ActiveCellKeyDownArgs.column and PositionChangeArgs.column: undefined when a TreeDataGrid row is active (idx: -1). TreeDataGrid's own handleKeyDown depends on this with column?.idx ?? -1.
  • PositionChangeArgs.row: undefined on header and summary rows. events.test.tsx asserts row: undefined for the header row.

These fields type-check without | undefined only because noUncheckedIndexedAccess is off, so rows[i] and columns[i] have type R. I added a doc comment to ActiveCellKeyDownArgs.column, matching PositionChangeArgs, so it's clear why undefined is there.

Internal cleanups:

  • scrollIntoView(element): Maybe<Element> → Element | null. No caller passes undefined.
  • Cell's handleMouseEvent: the eventHandler parameter is no longer optional. Every caller passes it, and CellMouseEventHandler already includes undefined.

Conditions that can never be true:

  • DataGrid: removed bottomSummaryRows != null from the bottom summary shadow. bottomSummaryRowsCount is bottomSummaryRows?.length ?? 0, so bottomSummaryRowsCount > 0 already implies it.
  • CellNavigation demo: removed the rowIdx === -1 and rows.length === 0 branches. onCellKeyDown never fires on the header row or when there are no rows, so these branches never ran.

I checked every other | undefined, Maybe<>, and optional parameter in src/, website/, and test/. Each one can actually be undefined or null, or is a user-facing input prop. typescript/no-unnecessary-condition (type-aware) already catches conditions that are unnecessary by type, so I only looked for ones that are unnecessary at runtime.

🤖 Generated with Claude Code

`onCellKeyDown` is only called when the active position is in the viewport,
so `row` is always defined in ACTIVE mode.

`column` stays possibly undefined because TreeDataGrid can activate a whole row.
`PositionChangeArgs` keeps both: `row` is undefined on header and summary rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Oct 21, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.36%. Comparing base (a41d3b4) to head (44ccfea).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4219   +/-   ##
=======================================
  Coverage   98.36%   98.36%           
=======================================
  Files          45       45           
  Lines        1955     1955           
  Branches      727      727           
=======================================
  Hits         1923     1923           
  Misses         32       32           
Files with missing lines Coverage Δ
src/Cell.tsx 100.00% <ø> (ø)
src/DataGrid.tsx 98.88% <100.00%> (ø)
src/utils/domUtils.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

`bottomSummaryRowsCount > 0` already implies `bottomSummaryRows != null`.

`onCellKeyDown` is never called on the header row or when there are no rows,
so the `rowIdx === -1` and `rows.length === 0` branches in the CellNavigation demo were dead code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nstepien
nstepien marked this pull request as ready for review October 21, 2026 23:55
@nstepien
nstepien merged commit 96487c5 into main Oct 21, 2026
6 checks passed
@nstepien
nstepien deleted the narrow-keydown-row-type branch October 21, 2026 23:55
nstepien added a commit that referenced this pull request Oct 22, 2026
)

Found while working on #4219.

In a grid with column groups and bottom summary rows, moving the active
cell into a column group header row called the user's `colSpan` with `{
type: 'SUMMARY', row: undefined }`.

`getCellColSpan` checks for the main header row, then top summary rows,
then data rows. Any other `rowIdx` fell through to the bottom summary
rows branch, and that included column group header rows above the main
header row. So `bottomSummaryRows[rowIdx - rows.length]` read a negative
index. A `colSpan` that reads `args.row` for summary rows would throw,
and one that returns a span for summary rows would move the active cell
to the wrong column.

The bottom summary rows branch now also requires `rowIdx >=
rows.length`. Column group header rows fall through to `return
undefined`, which is what we want because column group header cells
don't use `colSpan`.

The new test failed in Chromium, Firefox, and WebKit before the fix.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
nstepien added a commit that referenced this pull request Oct 29, 2026
…h group rows, and export `isGroupRow` (#4265)

`TreeDataGrid` passes its group rows to `DataGrid` as `R`, so when a
group row, or a cell of one, is active, `onCellKeyDown` and
`onActivePositionChange` receive a `GroupRow` typed as `R`: a property
access like `args.row.name.toUpperCase()` type-checks and throws at
runtime. Their `row` is now typed `R | GroupRow<R>`, and `isGroupRow` is
exported so handlers can narrow it. Like #4219, this makes the types say
what happens.

Builds on #4264.

### Breaking change

```diff
- function onCellKeyDown(args: CellKeyDownArgs<Row>, event: CellKeyboardEvent) {
-   console.log(args.row.name);
+ function onCellKeyDown(args: TreeCellKeyDownArgs<Row>, event: CellKeyboardEvent) {
+   if (isGroupRow(args.row)) return;
+   console.log(args.row.name);
  }
```

Affects `TreeDataGrid`'s `onCellKeyDown` and `onActivePositionChange`
handlers: their `row` is typed `R | GroupRow<R>`, which is what the grid
passed all along when a group row was active. `isGroupRow` is exported
to narrow it. `DataGrid` handlers are unchanged.

### Changes

- `CellKeyDownArgs` and `PositionChangeArgs` get a third type parameter,
`TActiveRow`, which defaults to `TRow`, so `DataGrid` handler types are
unchanged. It only applies to the `ACTIVE` variant of `CellKeyDownArgs`:
`EditCell` is only rendered through `Row`, never through `GroupedRow`,
so `EDIT` args never carry a group row.
- New exported aliases, `TreeCellKeyDownArgs<R, SR>` and
`TreePositionChangeArgs<R, SR>`, keep handlers short. The third type
parameter stays public and is documented next to them.
- `TreeDataGridProps` omits the two inherited props and redeclares them
with the aliases; `TreeDataGrid`'s own `handleKeyDown` uses
`TreeCellKeyDownArgs`. Passing them on to `DataGrid` type-checks because
function parameters are contravariant.
- `isGroupRow` is exported from `TreeDataGrid.tsx`, where it stays with
its `WeakSet`: `DataGrid` doesn't use it, so exporting it from there
creates no import cycle. If the group-row edit-mode bug below gets fixed
in `DataGrid`, that PR can move both to a shared module.
- The `rows as R[]` cast stays, and its `TODO` comment now explains
which callbacks receive group rows.
- README: `isGroupRow` under "Other", the aliases and the third type
parameter under "Types", `onCellKeyDown` and `onActivePositionChange`
under `TreeDataGridProps`, and a "Group rows in callbacks" paragraph
with an example under `<TreeDataGrid />`. The `CellKeyDownArgs` and
`PositionChangeArgs` headings gain `TActiveRow`, so their anchors
changed and the links to them are updated.
- Re-verified that the other callbacks never receive group rows: the
cell mouse events (group cells don't fire them), `rowClass` (not applied
to group rows), `onCellCopy` and `onCellPaste` (filtered),
`renderers.renderRow` and `renderCell` (wrapped), `rowHeight`
(`RowHeightArgs`), and `onRowsChange` (indexes mapped back).

### Follow-ups

- Typing on a cell of a group row in a column with `renderEditCell` sets
`mode: 'EDIT'` with the group row; no editor renders, and the arrow keys
stop working until a click. That fix gets its own PR.

### Verification

`vp run ci` passes on the top of the stack (#4272). Added a browser test
that activates a group row, then a cell of it, then a cell of a regular
row, and asserts which rows `onCellKeyDown` and `onActivePositionChange`
received, using `isGroupRow`. A consumer type-check of the built
`lib/index.d.ts`, with and without `exactOptionalPropertyTypes`, uses
`TreeCellKeyDownArgs`, `TreePositionChangeArgs`, `isGroupRow`,
`GroupRow`, `expandedGroupIds`, and `renderGroupCell`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants