-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] Refactor keyboard/click event management #3275
Conversation
1ad3e8d
to
53642ca
Compare
ab551e7
to
b69ff2a
Compare
The
this condition prevents from applying keyboard shortcuts if the focus is not on a cell component (for example a select input) To apply keyboard events inside cells (for example when the focus is on a button) the event So the solution still needs improvement, but at least it solves the two issues |
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
b69ff2a
to
5de9f76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid breaking changes, we should still fire columnHeaderNavigationKeydown
even if we do not use it internally.
@@ -103,7 +103,7 @@ const GridHeaderCheckbox = React.forwardRef<HTMLInputElement, GridColumnHeaderPa | |||
event.stopPropagation(); | |||
} | |||
if (isNavigationKey(event.key) && !event.shiftKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we fire the columnHeaderKeyDown
on any key, not just navigation one ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we remove this handler and let columnHeaderKeyDown
to propagate freely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not noticed, but we are already firing columnHeaderKeyDown
on any key except Space (because of the stop event propagation)
The stop eventPropagation is here to avoid a preventDefault
which would disable the validation of the input with Space key.
I'm putting back the columnHeaderNavigationKeyDown
and add a TODO such as it should be remove in v6
For the stopPropagation, I see two options:
- keep the
stopPropagation
and firecolumnHeaderKeyDown
- remove the
stopPropagation
, and call thehandleChange
the the key pressed is a Space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a draft PR to explain the proposed solution in #3265 (comment)
Is it still in draft? I don't know how rigorous should I review it.
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Outdated
Show resolved
Hide resolved
@@ -24,6 +24,10 @@ export function isGridHeaderCellRoot(elem: Element | null): boolean { | |||
return elem != null && elem.classList.contains(gridClasses.columnHeader); | |||
} | |||
|
|||
export function isGridHeaderSelectAllCheckBox(elem: Element | null): boolean { | |||
return elem != null && elem.ariaLabel === 'Select All Rows checkbox'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could have been translated.
GridEventListener<GridEvents.columnHeaderNavigationKeyDown> | ||
>( | ||
(params, event) => { | ||
event.preventDefault(); | ||
if ( | ||
!isGridHeaderCellRoot(event.target as HTMLElement) && | ||
!isGridHeaderSelectAllCheckBox(event.target as HTMLElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking params.field
is safer.
@@ -103,7 +103,7 @@ const GridHeaderCheckbox = React.forwardRef<HTMLInputElement, GridColumnHeaderPa | |||
event.stopPropagation(); | |||
} | |||
if (isNavigationKey(event.key) && !event.shiftKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we remove this handler and let columnHeaderKeyDown
to propagate freely?
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add some test cases to cover the fixed issues.
packages/grid/_modules_/grid/hooks/features/keyboard/useGridKeyboard.ts
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/features/keyboard/useGridKeyboard.ts
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We miss a test for #3275 (comment) in selection.DataGridPro.test.tsx
.
it('should select one row at a time on Shift + Space', () => { | ||
render(<TestDataGridSelection />); | ||
getCell(0, 0).focus(); | ||
fireEvent.keyDown(document.activeElement || document.body, { key: ' ', shiftKey: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why document.activeElement || document.body
? Was it to snooze the ESLint rule "Don't use document.activeElement as a target for keyboard events."? 😁 You can fix it properly by passing the cell element as bellow:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, it is to follow the documentation :)
This will also test that the element in question can even receive keyboard events
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The element is passed to improve readability: mui/material-ui#20945
This will also test that the element in question can even receive keyboard events
This check is embedded into the custom fireEvent
we use. See https://github.com/mui-org/material-ui/blob/69b7e4d911e8ff99d17a74330496ae69e7cc0ade/test/utils/createRenderer.tsx#L622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done the modifications, but I'm not really convinced that it improves readability
The core team has element which are all meaning full, leading to testing code such as
fireEvent.keyDown(button, ...);
fireEvent.keyDown(firstTab, ...);
We are navigating between cells so most of the components with focus are cells which only differ by there coordinates which is translated by
fireEvent.keyDown(cell01, ...);
fireEvent.keyDown(cell23, ...);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take as example the following test case:
expect(getActiveCell()).to.equal('8-1');
fireEvent.keyDown(document.activeElement!, { key: 'ArrowDown' });
fireEvent.keyDown(document.activeElement!, { key: 'ArrowRight' });
fireEvent.keyDown(document.activeElement!, { key: 'ArrowLeft' });
fireEvent.keyDown(document.activeElement!, { key: 'ArrowUp' });
Can you easily spot where the focus is after the last keyDown
? 😛
packages/grid/x-data-grid/src/tests/selection.DataGrid.test.tsx
Outdated
Show resolved
Hide resolved
@m4theushw I added a commit to prevent keyboard "Shift+Space" from starting editing the cell, which is a pain when selecting rows in an editable DataGrid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job.
!isGridHeaderCellRoot(event.target as HTMLElement) && | ||
!isGridHeaderSelectAllCheckBox(event.target as HTMLElement) | ||
) { | ||
if (!params.field) { | ||
return; | ||
} | ||
const dimensions = apiRef.current.getRootDimensions(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This modification
Fix #3265 and fix #3272
This is a first step to handle all the keyboard events in their feature hook #3265 (comment)