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

[TreeView] Support reordering of items in SimpleTreeView #12369

Merged
merged 62 commits into from
Apr 9, 2024

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Mar 7, 2024

Fix #10235

Problem

The registration is done by the children when some of their props changes.
But if you just swap to items, nothing is called and the indexes in the state becomes outdated, causing multi-selection to break.

New approach

For both RichTreeView and SimpleTreeView

  • For each item with children, store its children ids in an order array in state.items.itemOrderedChildrenIds
  • Remove the index from state.items.nodeMap

Benefis

  • instance.getItemOrderedChildrenIds (which replaces instance.getChildrenIds) has now a complexity of O(1) instead of O(n). It is used for most keyboard navigation and range selection.

Costs

  • The complexity to get the index of a given item is now O(n) instead of O(1). We need to explore the optimal data structure for the indexes depending on which access is the most frequent (getting an item index, changing an item index, getting the next item after some item, getting an ordered list of the items, ...). For now the new structure is super simple so at least we can easily modify it.

For SimpleTreeView

  • On every render, each item generates the new list of its children ids using querySelector, if the list has changed it updates state.items.itemOrderedChildrenIds

Benefis

  • Can now re-order items on SimpleTreeView
  • We no longer have any binary position search based on the DOM position

Costs

  • We have some cost on every render to check if the children have changed (should not be noticeable at all since SimpleTreeView is not meant for big dataset, IMHO the data structure optimization for state.items.itemOrderedChildrenIds are a lot more important to help the RichTreeView scale on big datasets).

Follow up

  • state.items.itemOrderedChildrenIds could be a ref since it's only used in even callbacks. We need to check if this can avoid a re-render (not sure since it's always updated when children changes, which should cause a re-render of its own unless it's an index swap. So the optimization might be pointless).

@flaviendelangle flaviendelangle added bug 🐛 Something doesn't work component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! labels Mar 7, 2024
@flaviendelangle flaviendelangle marked this pull request as draft March 7, 2024 17:14
@flaviendelangle flaviendelangle self-assigned this Mar 7, 2024
@mui-bot
Copy link

mui-bot commented Mar 7, 2024

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

Generated by 🚫 dangerJS against 10e37b4

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 11, 2024
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 11, 2024
@flaviendelangle flaviendelangle force-pushed the reorder-simple-tree-view branch 14 times, most recently from bf4c4d2 to 87afd8a Compare April 5, 2024 10:28
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 8, 2024
Copy link

github-actions bot commented Apr 8, 2024

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 8, 2024
Copy link

github-actions bot commented Apr 8, 2024

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 8, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 9, 2024
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

The updated approach looks great! 🎉 👍

@flaviendelangle flaviendelangle merged commit 5c31f93 into mui:master Apr 9, 2024
17 checks passed
@flaviendelangle flaviendelangle deleted the reorder-simple-tree-view branch April 9, 2024 09:27
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: tree view TreeView, TreeItem. 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.

[TreeView] After reordering TreeItem in TreeView, multiple selection broken
4 participants