[DataGrid] Prevent scroll when selecting rows #2558
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2508
Before: https://codesandbox.io/s/material-demo-forked-5u9pt?file=/demo.js
After: https://codesandbox.io/s/material-demo-forked-sw3r0?file=/demo.js
This bug is pretty interesting. Let's imagine that the first row is already selected. The selection and the focus are both listing to the click event. The user clicks into a different row and a rerender is triggered. While that, the focus state is also updated, which triggers another rerender. When the cell of the first selected row renders (it's not selected anymore) it still thinks that it has focus so it will call
.focus()
. The problem is that if this first cell is not visible, the browser will try to scroll to it. My fix is to pass an option to.focus
to disable the scroll. This doesn't work in all browsers, so we use our own API to provide an alternative. I don't see a problem disabling the scroll, in this demo we're already not relaying on it but on the scroll APIs.