Skip to content
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] Tab should update state.focus on edit mode #6668

Open
wants to merge 5 commits into
base: v5.x
Choose a base branch
from

Conversation

yaredtsy
Copy link
Contributor

@yaredtsy yaredtsy commented Oct 29, 2022

Fixes #5952

Before
on Edit mode Tab key was not updating the state.focus. even if the focus of the component changes the state is not updating.
Demo: https://codesandbox.io/s/updaterowsapiref-demo-mui-x-forked-buy7rq?file=/demo.tsx

Now
https://codesandbox.io/s/updaterowsapiref-demo-mui-x-forked-kcn7ft?file=/demo.tsx

@mui-bot
Copy link

mui-bot commented Oct 29, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 483.2 733 664.6 620.86 100.513
Sort 100k rows ms 554.5 1,109.8 667.6 832.5 200.112
Select 100k rows ms 141.8 254.3 222.6 208.82 37.919
Deselect 100k rows ms 154.1 201 187.9 178.92 18.153

Generated by 🚫 dangerJS against b1543c7

@zannager zannager added the component: data grid This is the name of the generic UI component, not the React module! label Oct 31, 2022
@yaredtsy yaredtsy changed the title [DataGrid] Tab should update state.focus on edit mode [DataGrid] Tab should update state.focus on edit mode Nov 21, 2022
Copy link
Member

@cherniavskii cherniavskii left a comment

Choose a reason for hiding this comment

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

Sorry for taking so long to review this PR.
While it fixes the linked issue, I think it's a bit broader topic - we will need to somehow handle the cells with more than one focusable element, like date pickers. Ideally, it should be possible to Tab from input to the icon button.

This PR might be an intermediate solution if it doesn't break anything.

if (cellParams.cellMode !== GridCellModes.Edit && isNavigationKey(event.key)) {
if (
(cellParams.cellMode !== GridCellModes.Edit && isNavigationKey(event.key)) ||
event.key === 'Tab'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
event.key === 'Tab'
(cellParams.cellMode !== GridCellModes.Edit && event.key === 'Tab')

This would fix failed e2e test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would fix failed e2e test

but this was the problem useGridKeyboardNavigation won't publish the event in the edit mode.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I forgot to reverse the check.
I meant this:

- event.key === 'Tab'
+ (cellParams.cellMode === GridCellModes.Edit  && event.key === 'Tab')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hey @cherniavskii

I have adapted @m4theushw solution and I think everything is working now.

Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

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

I was not aware of this PR. I opened #7098 with a different approach that also works with Portals. IMO the keyboard navigation for cells in edit mode should be handled inside the editing hook. When Tab is pressed, we can't only move to the column, it should move to the next editable column. The code below is buggy:

case 'Tab': {
// "Tab" is only triggered by the row / cell editing feature
if (event.shiftKey && colIndexBefore > firstColIndex) {
goToCell(colIndexBefore - 1, getRowIdFromIndex(rowIndexBefore), 'left');
} else if (!event.shiftKey && colIndexBefore < lastColIndex) {
goToCell(colIndexBefore + 1, getRowIdFromIndex(rowIndexBefore), 'right');
}

This PR creates a regression when a editable: false column is between two editable: true columns.

#6668

msedge_URgV2PkvTD

HEAD

msedge_Gjh2j3uTg5

Above the username column is not editable but it gets focused when pressing Tab.

Checking the next valid column to move the focus is a bit complex and shouldn't be inside useGridKeyboardNavigation because it would be poluted with editing stuff.

Have you considered the proposal I made in #5952 (comment)? The current PR is still prone to non-user-originated clicks, like element.click().

if (
(cellParams.cellMode !== GridCellModes.Edit && isNavigationKey(event.key)) ||
event.key === 'Tab'
) {
apiRef.current.publishEvent('cellNavigationKeyDown', cellParams, event);
Copy link
Member

Choose a reason for hiding this comment

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

cellNavigationKeyDown was removed in v6.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have you considered the proposal I made in #5952 (comment)

no, I saw the comment in useGridFocus. I thought someone forgot to update useGridKeyboardNavigation to handle the focus.

// GRID_CELL_NAVIGATION_KEY_DOWN handles the focus on Enter, Tab and navigation keys
if (event.key === 'Enter' || event.key === 'Tab' || isNavigationKey(event.key)) {
return;
}
apiRef.current.setCellFocus(params.id, params.field);

I thought it was a simple bug, I will look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have copied the #7098 solution, and it fixed all issues.

Demo: https://codesandbox.io/s/updaterowsapiref-demo-mui-x-forked-kcn7ft?file=/demo.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants