Skip to content

Add internal useActivePosition hook#3990

Open
nstepien wants to merge 4 commits intomainfrom
useActivePosition
Open

Add internal useActivePosition hook#3990
nstepien wants to merge 4 commits intomainfrom
useActivePosition

Conversation

@nstepien
Copy link
Collaborator

@nstepien nstepien commented Mar 5, 2026

No description provided.

@nstepien nstepien self-assigned this Mar 5, 2026
const initialActivePosition: ActivePosition = {
idx: -1,
// use -Infinity to avoid issues when adding header rows or top summary rows
rowIdx: Number.NEGATIVE_INFINITY,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug and added a test.

}

return {
activePosition: resolvedActivePosition,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will now render DataGrid with the correct state one render early in some cases.

We could also just not do this to keep the implementation simpler.


import type { CalculatedColumn, Position, StateSetter } from '../types';

interface ActivePosition extends Position {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this from ActiveCellState to ActivePosition, likewise for EditPosition below

if (!activePositionIsCellInViewport) return;
const { idx, rowIdx } = activePosition;
onCellCopy?.({ row: rows[rowIdx], column: columns[idx] }, event);
onCellCopy?.({ row: getActiveRow(), column: getActiveColumn() }, event);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about getActiveRow()/getActiveColumn()? Worth it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Should we return variables instead activeRow, activeColumn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to check if they're undefined in many places

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also add queryActive*() 🤔


function getDragHandle() {
if (onFill == null || activePosition.mode === 'EDIT' || !activePositionIsCellInViewport) {
if (onFill == null || activePosition.mode !== 'ACTIVE' || !activePositionIsCellInViewport) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the mode check in case we ever add a third mode

await validateCellPosition(2, 0);
});

test('adding a top summary row when no rows or cells are active should not focus the summary row', async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where to put this test, it's not only strictly about keyboard navigation, and it may not be limited to TreeDataGrid if we ever add row focus support in DataGrid.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TreeDataGrid tests also have some keyboard tests so we can move it there as well. I am okay with either
https://github.com/Comcast/react-data-grid/blob/main/test/browser/TreeDataGrid.test.tsx#L311

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the test

@nstepien nstepien marked this pull request as ready for review March 5, 2026 22:26
@nstepien nstepien requested a review from amanmahajan7 as a code owner March 5, 2026 22:26
@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2026

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

Codecov Report

❌ Patch coverage is 97.05882% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.41%. Comparing base (e533510) to head (159304e).

Files with missing lines Patch % Lines
src/hooks/useActivePosition.ts 95.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3990      +/-   ##
==========================================
- Coverage   97.51%   97.41%   -0.11%     
==========================================
  Files          36       37       +1     
  Lines        1449     1470      +21     
  Branches      463      471       +8     
==========================================
+ Hits         1413     1432      +19     
- Misses         36       38       +2     
Files with missing lines Coverage Δ
src/DataGrid.tsx 98.91% <100.00%> (-0.05%) ⬇️
src/hooks/useActivePosition.ts 95.00% <95.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.

export function useActivePosition<R, SR>({
columns,
rows,
validatePosition,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for this hook to return validatePosition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (!activePositionIsCellInViewport) return;
const { idx, rowIdx } = activePosition;
onCellCopy?.({ row: rows[rowIdx], column: columns[idx] }, event);
onCellCopy?.({ row: getActiveRow(), column: getActiveColumn() }, event);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Should we return variables instead activeRow, activeColumn?

await validateCellPosition(2, 0);
});

test('adding a top summary row when no rows or cells are active should not focus the summary row', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TreeDataGrid tests also have some keyboard tests so we can move it there as well. I am okay with either
https://github.com/Comcast/react-data-grid/blob/main/test/browser/TreeDataGrid.test.tsx#L311

@nstepien nstepien requested a review from amanmahajan7 March 6, 2026 16:48
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.

3 participants