Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const useGridScroll = (
(params: Partial<GridCellIndexCoordinates>) => {
const totalRowCount = gridRowCountSelector(apiRef);
const visibleColumns = gridVisibleColumnDefinitionsSelector(apiRef);
if (totalRowCount === 0 || visibleColumns.length === 0) {
const scrollToHeader = params.rowIndex == null;
if ((!scrollToHeader && totalRowCount === 0) || visibleColumns.length === 0) {
return false;
}

Expand Down
19 changes: 19 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 @@ -319,6 +319,25 @@ describe('<DataGrid /> - Keyboard', () => {
expect(virtualScroller.scrollLeft).not.to.equal(0);
});

it('should scroll horizontally when navigating between column headers with arrows even if rows are empty', function test() {
if (isJSDOM) {
// Need layouting for column virtualization
this.skip();
}
render(
<div style={{ width: 60, height: 300 }}>
<DataGrid autoHeight={isJSDOM} {...getData(10, 10)} rows={[]} />
</div>,
);
getColumnHeaderCell(0).focus();
const virtualScroller = document.querySelector(
'.MuiDataGrid-virtualScroller',
)! as HTMLElement;
expect(virtualScroller.scrollLeft).to.equal(0);
fireEvent.keyDown(document.activeElement!, { key: 'ArrowRight' });
Copy link
Collaborator

Choose a reason for hiding this comment

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

To able to re-enable the ESLint rule in the future.

Suggested change
fireEvent.keyDown(document.activeElement!, { key: 'ArrowRight' });
fireEvent.keyDown(getColumnHeaderCell(0), { key: 'ArrowRight' });

Copy link
Member Author

Choose a reason for hiding this comment

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

@m4theushw are you talking about the following line?

// @ts-ignore Remove once the test utils are typed

The file is full of those document.activeElement! should I remove them?

Copy link
Member Author

Choose a reason for hiding this comment

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

I merged because it's not related to the bug. If moving from document.activeElement to getter methods, I will do a distinct PR cleaning the all file

Copy link
Collaborator

@m4theushw m4theushw Mar 30, 2022

Choose a reason for hiding this comment

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

I was talking about

/* eslint-disable material-ui/disallow-active-element-as-key-event-target */

It's good to not use document.activeElement in new tests. In the future there will be less tests to change.

The file is full of those document.activeElement! should I remove them?

The remaining ones can be done later since it's not the purpose of this PR.

expect(virtualScroller.scrollLeft).not.to.equal(0);
});

it('should move to the first row when pressing "ArrowDown" on a column header on the 1st page', () => {
render(<NavigationTestCaseNoScrollX />);
getColumnHeaderCell(1).focus();
Expand Down