Skip to content

Commit

Permalink
[DataGrid] Fix error when keyboard navigating an empty grid (#10081)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk authored Aug 31, 2023
1 parent 55afbc8 commit e743bf5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const useGridKeyboardNavigation = (

const getRowIdFromIndex = React.useCallback(
(rowIndex: number) => {
return currentPageRows?.[rowIndex].id;
return currentPageRows[rowIndex]?.id;
},
[currentPageRows],
);
Expand All @@ -197,7 +197,7 @@ export const useGridKeyboardNavigation = (

const viewportPageSize = apiRef.current.getViewportPageSize();
const colIndexBefore = params.field ? apiRef.current.getColumnIndex(params.field) : 0;
const firstRowIndexInPage = 0;
const firstRowIndexInPage = currentPageRows.length > 0 ? 0 : null;
const lastRowIndexInPage = currentPageRows.length - 1;
const firstColIndex = 0;
const lastColIndex = gridVisibleColumnDefinitionsSelector(apiRef).length - 1;
Expand Down
15 changes: 15 additions & 0 deletions packages/grid/x-data-grid/src/tests/keyboard.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -715,4 +715,19 @@ describe('<DataGrid /> - Keyboard', () => {
fireEvent.keyDown(firstCell, { key: 'ArrowDown' });
expect(virtualScroller.scrollLeft).to.equal(0);
});

it('should not throw when moving into an empty grid', async () => {
const columns = [{ field: 'id', width: 400 }, { field: 'name' }];
const rows = [] as any[];

render(
<div style={{ width: 300, height: 300 }}>
<DataGrid rows={rows} columns={columns} />
</div>,
);

const cell = getColumnHeaderCell(0);
act(() => cell.focus());
fireEvent.keyDown(cell, { key: 'ArrowDown' });
});
});

0 comments on commit e743bf5

Please sign in to comment.