Skip to content

[docs] Add a recipe for drag and drop row grouping #17638

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

Merged
merged 18 commits into from
May 21, 2025

Conversation

KenanYusuf
Copy link
Member

@KenanYusuf KenanYusuf commented Apr 30, 2025

@KenanYusuf KenanYusuf added docs Improvements or additions to the documentation scope: data grid Changes or issues related to the data grid product feature: Row grouping Related to the data grid Row grouping feature labels Apr 30, 2025
Copy link

github-actions bot commented Apr 30, 2025

Thanks for adding a type label to the PR! 👍

Copy link

Please add one type label to categorize the purpose of this PR appropriately:

bug, regression, enhancement, new feature, release or dependencies

@KenanYusuf KenanYusuf added recipe type: enhancement This is not a bug, nor a new feature labels Apr 30, 2025
@KenanYusuf
Copy link
Member Author

Please add one type label to categorize the purpose of this PR appropriately:

bug, regression, enhancement, new feature, release or dependencies

Not sure a recipe fits into any of these categories 🤷

@mui-bot
Copy link

mui-bot commented Apr 30, 2025

@KenanYusuf KenanYusuf requested a review from a team April 30, 2025 14:16
@KenanYusuf KenanYusuf marked this pull request as draft April 30, 2025 14:22
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.

Looks very nice 👍🏻

};

return (
<Chip
Copy link
Member

Choose a reason for hiding this comment

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

Should we disable these chips when pivot mode is active? They won't work anyway.


return (
<div style={{ height: 400, width: '100%' }}>
<DataGridPremium
Copy link
Member Author

Choose a reason for hiding this comment

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

It would be a much better UX if you could use it with useKeepGroupedColumnsHidden so that when a column gets added to the toolbar, it gets removed from the list of columns and you don't end up with duplicates.

The issue is when a column header gets added to the toolbar on drag over, it is no longer in the DOM, and so the onDragEnd event does not fire. In the demo below you can see how the dragged column state does not get reset.

Screen.Recording.2025-05-02.at.16.40.58.mov

Copy link
Member

Choose a reason for hiding this comment

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

The issue is when a column header gets added to the toolbar on drag over, it is no longer in the DOM, and so the onDragEnd event does not fire.

Is this because we add grouping criteria on dragOver?
I was actually wondering if adding grouping criteria on dragEnd would be a better UX 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I was actually wondering if adding grouping criteria on dragEnd would be a better UX 🤔

Wouldn't it have to be onDrop on the toolbar? If so, we encounter the same issue 😕

Copy link
Member Author

Choose a reason for hiding this comment

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

We could add an API method that we could call in user-land to reset the dragged column state once the column header has been dropped in the toolbar, but it feels very niche and not like something that should be in the public API.

I'm wondering if we should just build this feature into the grid properly so we can share state between the two features and have private API methods where necessary. We could provide a better and more complete UX.

Copy link
Member Author

Choose a reason for hiding this comment

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

Together with @cherniavskii, we found that if we use a regular dragend event listener to run the handleDragEnd function, the state properly resets when a column header is dropped on the toolbar.

@@ -0,0 +1,28 @@
/* eslint-disable no-bitwise */
Copy link
Member Author

@KenanYusuf KenanYusuf May 8, 2025

Choose a reason for hiding this comment

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

@KenanYusuf KenanYusuf marked this pull request as ready for review May 19, 2025 13:20
@KenanYusuf KenanYusuf requested review from cherniavskii and a team May 19, 2025 13:20
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.

LGTM

const handleKeyDown = (
event: React.KeyboardEvent<HTMLDivElement>,
) => {
if (event.key === 'ArrowRight' && event.shiftKey) {
Copy link
Member

Choose a reason for hiding this comment

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

A minor weird behavior I noticed: when reordering with the Shift + Arrow key, the navigation order of keys gets reversed, which is fixed when reordering again with the Shift + Arrow key.

Screen.Recording.2025-05-20.at.12.16.17.AM.mov

I suspect focusableItem.index is not getting properly updated in the Toolbar.

Copy link
Member Author

@KenanYusuf KenanYusuf May 20, 2025

Choose a reason for hiding this comment

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

This is very strange... it doesn't behave like this in dev mode locally, only on the built version.

Copy link
Member Author

@KenanYusuf KenanYusuf May 21, 2025

Choose a reason for hiding this comment

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

I suspect focusableItem.index is not getting properly updated in the Toolbar.

This was indeed the problem, I couldn't find a nice way of reliably tracking the order of toolbar items — instead, I've updated it to get the sort order at the various points it is needed.

keyboard.mp4

Co-authored-by: Bilal Shafi <[email protected]>
Co-authored-by: Andrew Cherniavskii <[email protected]>
Signed-off-by: Kenan Yusuf <[email protected]>
@KenanYusuf KenanYusuf added the feature: Toolbar Related to the data grid Toolbar feature label May 20, 2025
@KenanYusuf KenanYusuf merged commit ac6e193 into mui:master May 21, 2025
23 checks passed
@KenanYusuf KenanYusuf deleted the row-group-recipe branch May 21, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation feature: Row grouping Related to the data grid Row grouping feature feature: Toolbar Related to the data grid Toolbar feature recipe scope: data grid Changes or issues related to the data grid product type: enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants