Skip to content

[tree view] Fix collapsed children not selected#22711

Open
brijeshb42 wants to merge 1 commit into
mui:masterfrom
brijeshb42:fix-treeview-deferred-children-mount
Open

[tree view] Fix collapsed children not selected#22711
brijeshb42 wants to merge 1 commit into
mui:masterfrom
brijeshb42:fix-treeview-deferred-children-mount

Conversation

@brijeshb42

Copy link
Copy Markdown
Contributor

Bumps @mui/material family (material, system, utils, icons-material) to 9.1.0.

9.1.0's Collapse mounts children in a deferred render, so SimpleTreeView's child-discovery effect never re-ran. Force a re-render on child register/unregister so collapsed-parent descendant selection works. Full jsdom suite green on 9.1.0.

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 9, 2026

Copy link
Copy Markdown

Deploy preview

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

Bundle size

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-charts-premium 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 🔺+40B(+0.06%) 🔺+20B(+0.10%)
@mui/x-tree-view-pro 🔺+40B(+0.03%) 🔺+18B(+0.05%)
@mui/x-license 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

…terial to 9.1.0

Bump @mui/material family (material, system, utils, icons-material) to 9.1.0.
9.1.0's Collapse mounts children in a deferred render, so SimpleTreeView's child-discovery effect never re-ran; force a re-render on child register/unregister to fix collapsed-parent descendant selection.
@brijeshb42 brijeshb42 force-pushed the fix-treeview-deferred-children-mount branch from 5e9a3dd to 856d7f7 Compare June 9, 2026 04:09
@brijeshb42 brijeshb42 changed the title [tree view] Fix collapsed children not selected after bumping @mui/material to 9.1.0 [tree view] Fix collapsed children not selected Jun 9, 2026
@brijeshb42 brijeshb42 added type: bug It doesn't behave as expected. scope: tree view Changes related to the tree view. This includes TreeView, TreeItem. labels Jun 9, 2026
@brijeshb42 brijeshb42 requested review from a team and flaviendelangle and removed request for a team June 9, 2026 04:14

@LukasTy LukasTy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review: PR 22711 -- [tree view] Fix collapsed children not selected

Verdict: Approve. Correct, minimal fix for a real 9.1.0 regression: Collapse now mounts unmountOnExit children a commit later, so SimpleTreeView's DOM child-discovery never re-ran; forcing a re-render on register/unregister re-arms the existing selection-recovery path. CI green.

Findings:

  • [Medium] Confirm with the material team whether 9.1.0 Collapse mounting children a commit later was intentional. If not, report upstream -- but keep this fix regardless, since it also hardens the tree against any deferred-mount transition.
  • [Low] Optional: guard rerender() on an actual map change -- skip it when registerChild gets an identical (idAttr, itemId), and use if (map.delete(idAttr)) rerender() in unregisterChild. Trims redundant renders and neutralizes the pre-existing duplicate unregisterChild call (itemPlugin.tsx:44-45).
    --- a/packages/x-tree-view/src/internals/TreeViewProvider/TreeViewChildrenItemProvider.tsx
    +++ b/packages/x-tree-view/src/internals/TreeViewProvider/TreeViewChildrenItemProvider.tsx
           registerChild: (childIdAttribute, childItemId) => {
    +        // Only re-render when the child set actually changes.
    +        if (childrenIdAttrToIdRef.current.get(childIdAttribute) === childItemId) {
    +          return;
    +        }
             childrenIdAttrToIdRef.current.set(childIdAttribute, childItemId);
             rerender();
           },
           unregisterChild: (childIdAttribute) => {
    -        childrenIdAttrToIdRef.current.delete(childIdAttribute);
    -        rerender();
    +        // Map.delete returns true only when an entry existed, so duplicate/no-op calls skip the re-render.
    +        if (childrenIdAttrToIdRef.current.delete(childIdAttribute)) {
    +          rerender();
    +        }
           },
           parentId: itemId,

Perf is a non-issue: the re-render hits only the provider (stable context value + same children element, so no subtree re-render), batches per commit, and this path is SimpleTreeView-only -- RichTreeView never mounts it, so large datasets are unaffected.

fireEvent.click(view.getItemCheckboxInput('1'));
fireEvent.click(view.getItemContent('1'));
expect(view.getSelectedTreeItems()).to.deep.equal(['1', '1.1', '1.2']);
await waitFor(() => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is technically a small BC worth flagging in the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tree view Changes related to the tree view. This includes TreeView, TreeItem. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants