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] Prevent error when deleting the last row #15146

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

dpak-maurya
Copy link
Contributor

@dpak-maurya dpak-maurya commented Oct 27, 2024

Fix #15145

Issue: Error When Deleting the Last Row
Description: When the last row in the Data Grid is deleted, an error occurs: Cannot read properties of undefined (reading 'id'). This happens because the grid tries to focus on a row that no longer exists.

Proposed Solution:

To fix this, we changed the code to safely access the id of the next row using optional chaining. This ensures that if there are no rows left, it won't throw an error.

Code Change:

nextRowId = nextRow?.id ?? null; // Safely access the id

This modification prevents errors and improves the handling of row deletions in the Data Grid.

Issue: Error When Deleting the Last Row
Description: When the last row in the Data Grid is deleted, an error occurs: Cannot read properties of undefined (reading 'id'). This happens because the grid tries to focus on a row that no longer exists.

Proposed Solution:

To fix this, we changed the code to safely access the id of the next row using optional chaining. This ensures that if there are no rows left, it won't throw an error.

Code Change:

javascript
Copy code
const nextRow = currentPage.rows[clamp(lastFocusedRowIndex, 0, currentPage.rows.length - 1)];
nextRowId = nextRow?.id ?? null; // Safely access the id
This modification prevents errors and improves the handling of row deletions in the Data Grid.

Signed-off-by: Deepak Maurya <[email protected]>
@mui-bot
Copy link

mui-bot commented Oct 27, 2024

Deploy preview: https://deploy-preview-15146--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against c257629

@dpak-maurya dpak-maurya changed the title Fix: Prevent error when deleting the last row in Data Grid Prevent error when deleting the last row in Data Grid @mui/x-data-grid ^7.22.0 Oct 27, 2024
@alexfauquette alexfauquette changed the title Prevent error when deleting the last row in Data Grid @mui/x-data-grid ^7.22.0 [DataGrid] Prevent error when deleting the last row Oct 28, 2024
@alexfauquette alexfauquette added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! labels Oct 28, 2024
@KenanYusuf KenanYusuf merged commit 0fc9a15 into mui:master Oct 28, 2024
21 of 22 checks passed
KenanYusuf pushed a commit to KenanYusuf/mui-x that referenced this pull request Oct 28, 2024
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.

[docs] Demo FullFeaturedCrudGrid crashes
4 participants