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] Remove focus on cell when its row is removed from the data #1995

Merged

Conversation

flaviendelangle
Copy link
Member

Fixes #1958

@flaviendelangle flaviendelangle self-assigned this Jun 29, 2021
@flaviendelangle flaviendelangle marked this pull request as draft June 29, 2021 13:32
@flaviendelangle
Copy link
Member Author

flaviendelangle commented Jun 29, 2021

The issue occurred when removing a row that had a cell focused when removing it.
The next time we clicked on the document, it published the event GRID_CELL_FOCUS_OUT which tried to pass the value of the cell but unsucessfully.

I'm not sure what the best solution here is.
I went for a simple useEffect that checks if the focused cell has been removed whenever the rows prop changes, which solves the issue.

Of course this would require a new test once a solution is decided.

Another issue here is that the typing of getCellValue assures that is does return a value. But if we pass an invalid id, it returns undefined. I think that in a development environment it should throw an explicit error instead of letting the rest of the code crash with errors like cannot access X of undefined.

getRow: (id: GridRowId) => GridRowModel;

@flaviendelangle flaviendelangle requested a review from dtassone June 29, 2021 13:36
@flaviendelangle flaviendelangle changed the title Remove focus on cell when its row is removed from the data [DataGrid] Remove focus on cell when its row is removed from the data Jun 29, 2021
@flaviendelangle flaviendelangle marked this pull request as ready for review June 30, 2021 09:27
@dtassone
Copy link
Member

The issue occurred when removing a row that had a cell focused when removing it.
The next time we clicked on the document, it published the event GRID_CELL_FOCUS_OUT which tried to pass the value of the cell but unsucessfully.

I'm not sure what the best solution here is.
I went for a simple useEffect that checks if the focused cell has been removed whenever the rows prop changes, which solves the issue.

Of course this would require a new test once a solution is decided.

Another issue here is that the typing of getCellValue assures that is does return a value. But if we pass an invalid id, it returns undefined. I think that in a development environment it should throw an explicit error instead of letting the rest of the code crash with errors like cannot access X of undefined.

getRow: (id: GridRowId) => GridRowModel;

The solution looks fine.
I don't think we should throw an error if the id is invalid, I think it should just return null and we should update the type.
One could check if a row is in in the grid using apiRef and getRow

@flaviendelangle
Copy link
Member Author

OK, I'll add some test for the current behavior change
And for the type change I'll do another PR after

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

The bug is fixed: https://codesandbox.io/s/zen-darkness-fyjex 👍. I wonder about:

  1. Would we have the same issue with a removed column? Maybe we shouldn't are yet.
  2. Is the cleanup done soon enough? It seems to, it runs in the same effect timing than convertGridRowsPropToState.

});
});

it('should reset focus when removing the row containing the focus cell', async () => {
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
it('should reset focus when removing the row containing the focus cell', async () => {
it('should reset focus when removing the row containing the focus cell', () => {

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. I think that there is a broader subject than just row removal here indeed. Could there be the same behavior for other things than just focus ?

  2. I also think so.

expect(apiRef.current.getState().focus.cell).to.equal(null);
});

it('should not reset focus when removing a row not containing the focus cell', async () => {
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
it('should not reset focus when removing a row not containing the focus cell', async () => {
it('should not reset focus when removing a row not containing the focus cell', () => {

@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 Jun 30, 2021
@flaviendelangle flaviendelangle merged commit f871bdf into mui:master Jul 5, 2021
@flaviendelangle flaviendelangle deleted the remove-focus-of-removed-rows branch July 5, 2021 09:30
@magrinj
Copy link

magrinj commented Jul 15, 2021

@flaviendelangle Hey ! Thanks for the fix, when this one will be published in an npm release ?

@oliviertassinari
Copy link
Member

@magrinj Next up on our release round-robin is Damien. The last release was 2 weeks ago, so I assume it will happen soon or later.

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.

[XGrid] After updating row props with an array where item was deleted, data grid stops working
4 participants