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] Fix focus on checkbox cells #3501

Merged
merged 3 commits into from Jan 24, 2022

Conversation

alexfauquette
Copy link
Member

@alexfauquette alexfauquette commented Dec 23, 2021

Fix #3382

The stop propagation prevents having the selection event dispatched two times when clicking on the checkbox. One time by checkbox change, and the other one by row click event.

The problem is that it also prevents setting the focus on the cell. So now, interacting with the checkbox set the focus on its cell.

I did not add tests to detect the reported bug, because jsdom does not mock correctly, scrolling.

@alexfauquette alexfauquette self-assigned this Dec 23, 2021
@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! labels Dec 28, 2021
Copy link
Member

@flaviendelangle flaviendelangle left a comment

Choose a reason for hiding this comment

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

We could maybe add a test where we focus the checkbox, the click the body, then check if the checkbox is still focused

@m4theushw
Copy link
Member

The stop propagation prevents having the selection event dispatched two times when clicking on the checkbox. One time by checkbox change, and the other one by row click event.

How about removing event.stopPropagation and, on handleRowClick, do nothing if the event.target is inside the checkbox column? I started to do that to avoid selecting the row when opening the actions column menu. The alternative would be to stop propagation but I didn't want this hack.

https://github.com/mui-org/material-ui-x/blob/dd4b11fb73dd33c4945550619bf517c05bc4211b/packages/grid/_modules_/grid/hooks/features/selection/useGridSelection.ts#L338-L340

We can go even further in this approach and add a prop to GridColDef (see the description of #3492) to not select rows when an element inside the cell is clicked. I think that with more features being added this will become a common problem.

@alexfauquette
Copy link
Member Author

I agree on removing event.stopPropagation this would also fix #3382

But only blocking a column because in this case, it is the checkbox column that has similar interaction than row click seems a bit flacky.

Another idea would be to add some property to the event when a modification has been done. For example, when clicking on the checkbox, after modifying the selection, it add event.selectionDone: true such that when the row is listening to a click, it knows that selection has already been done by another listener before.

@m4theushw
Copy link
Member

Why do you think it's flaky? I see it differently. There're cell components that may receive clicks inside, since they contain interactive elements (e.g. the checkbox and the actions column), and the click shouldn't select the row. We already have disableSelectionOnClick but it disables the selection when clicking any column, not individually. The problem with keeping event.stopPropagation() is that eventually we will have to revisit this component to remove it.

@alexfauquette
Copy link
Member Author

Flaky is probably not the correct word. It is more about how to generalize the solution.

I feel like each time we will have a conflict between the event listener of nested components, we will have to find a hack in the listener of the parents to remove the duplicate. I would like to find, a solution to systematically prevent the parent from doing something already done by a child.

But I do not find a clear way of doing that, and it seems that most of the stopPropagation calls are made to deal with ActionCell/row interactions. So yes your solution should work, and we will see in the future if new features imply to redesign our strategy or not

@m4theushw
Copy link
Member

I feel like each time we will have a conflict between the event listener of nested components, we will have to find a hack in the listener of the parents to remove the duplicate.

I don't think it's a hack. We only need to ignore all clicks coming from a specific field. The code to check which field was clicked is the same.

I would like to find, a solution to systematically prevent the parent from doing something already done by a child.

This is complicated. We don't know what the child did. Currently, we only have problem with selection, but there could be other listeners on rowClick.

@alexfauquette
Copy link
Member Author

I followed @m4theushw suggestion. If the row click is from a checkbox cell, it does not trigger the selection

@mui-bot
Copy link

mui-bot commented Jan 20, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 196.7 410.2 296.4 291.12 74.557
Sort 100k rows ms 368.9 805.4 695.5 660.36 157.068
Select 100k rows ms 209.5 383 253.6 276.58 60.148
Deselect 100k rows ms 150.3 257.7 185.9 200.1 46.904

Generated by 🚫 dangerJS against e1d4eda

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.

This also fixes another bug I noticed:

  1. Open https://codesandbox.io/s/datagriddemo-material-demo-forked-s3u4i?file=/demo.js
  2. Select one row
  3. Click "Toggle checkbox selection"
  4. 💥

Since there's no click sent by the checkbox, it doesn't clean the last clicked cell below:

https://github.com/mui-org/material-ui-x/blob/ed223c349df133419b025b08199c7e36ff82f1d4/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts#L134

@alexfauquette
Copy link
Member Author

You find really strange bugs 😅 I confirm, it does not appears on this branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work 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.

[DataGrid] Persisted checkbox focus
5 participants