Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export function TreeViewChildrenItemProvider(props: TreeViewChildrenItemProvider

const { store, rootRef } = useTreeViewContext<SimpleTreeViewStore<any>>();
const childrenIdAttrToIdRef = React.useRef<Map<string, string>>(new Map());
// Re-run the children-discovery effect below whenever a child registers/unregisters, so it
// picks up children once they are mounted in the DOM. This is required because some transition
// components (for example `@mui/material`'s `Collapse`) mount the children in a deferred render
// that does not re-render this provider on its own.
const [, rerender] = React.useReducer((s: number) => s + 1, 0);

React.useEffect(() => {
if (!rootRef.current) {
Expand Down Expand Up @@ -57,9 +62,14 @@ export function TreeViewChildrenItemProvider(props: TreeViewChildrenItemProvider

const value = React.useMemo<TreeViewChildrenItemContextValue>(
() => ({
registerChild: (childIdAttribute, childItemId) =>
childrenIdAttrToIdRef.current.set(childIdAttribute, childItemId),
unregisterChild: (childIdAttribute) => childrenIdAttrToIdRef.current.delete(childIdAttribute),
registerChild: (childIdAttribute, childItemId) => {
childrenIdAttrToIdRef.current.set(childIdAttribute, childItemId);
rerender();
},
unregisterChild: (childIdAttribute) => {
childrenIdAttrToIdRef.current.delete(childIdAttribute);
rerender();
},
parentId: itemId,
}),
[itemId],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spy } from 'sinon';
import { fireEvent, act } from '@mui/internal-test-utils';
import { fireEvent, act, waitFor } from '@mui/internal-test-utils';
import { describeTreeView } from 'test/utils/tree-view/describeTreeView';
import { clearWarningsCache } from '@mui/x-internals/warning';
import { TreeViewAnyStore } from '../../models';
Expand Down Expand Up @@ -880,7 +880,7 @@ describeTreeView<TreeViewAnyStore>(
expect(view.getSelectedTreeItems()).to.deep.equal(['1', '1.2']);
});

it('should select all the children when selecting a collapsed parent and then expanding', () => {
it('should select all the children when selecting a collapsed parent and then expanding', async () => {
const view = render({
multiSelect: true,
checkboxSelection: true,
Expand All @@ -890,7 +890,9 @@ describeTreeView<TreeViewAnyStore>(

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.

expect(view.getSelectedTreeItems()).to.deep.equal(['1', '1.1', '1.2']);
});
});
});

Expand Down
Loading
Loading