Skip to content

Commit

Permalink
Fix onSelectedCellChange arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmahajan7 committed Nov 22, 2024
1 parent a606cdd commit c81292a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,10 @@ function DataGrid<R, SR, K extends Key>(
if (!isCellWithinSelectionBounds(position)) return;
commitEditorChanges();

const row = rows[position.rowIdx];
const samePosition = isSamePosition(selectedPosition, position);

if (enableEditor && isCellEditable(position)) {
const row = rows[position.rowIdx];
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
} else if (samePosition) {
// Avoid re-renders if the selected cell state is the same
Expand All @@ -771,7 +771,7 @@ function DataGrid<R, SR, K extends Key>(
if (onSelectedCellChange && !samePosition) {
onSelectedCellChange({
rowIdx: position.rowIdx,
row,
row: isRowIdxWithinViewportBounds(position.rowIdx) ? rows[position.rowIdx] : undefined,
column: columns[position.idx]
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export type CellKeyDownArgs<TRow, TSummaryRow = unknown> =

export interface CellSelectArgs<TRow, TSummaryRow = unknown> {
rowIdx: number;
row: TRow;
row: TRow | undefined;
column: CalculatedColumn<TRow, TSummaryRow>;
}

Expand Down
8 changes: 8 additions & 0 deletions test/browser/events.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ describe('Events', () => {
rowIdx: 0
});
expect(onSelectedCellChange).toHaveBeenCalledTimes(5);

// go to the header row
await userEvent.keyboard('{ArrowUp}');
expect(onSelectedCellChange).toHaveBeenCalledWith({
column: expect.objectContaining(columns[1]),
row: undefined,
rowIdx: -1
});
});
});

Expand Down

0 comments on commit c81292a

Please sign in to comment.