Skip to content

Commit 94f30d0

Browse files
committed
fix(useTreeController): fix toggle expanded for active trail
1 parent 1012482 commit 94f30d0

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/use-tree-controller.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ export function treeControllerFromUpdateState<T>(updateState: (f: TreeStateUpdat
2727
}));
2828
};
2929
obj.toggleExpanded = (id: string) => {
30-
obj.updateState!(({ expandedIds, ...rest }) => ({
31-
...rest,
32-
expandedIds: { ...expandedIds, [id]: !expandedIds || !expandedIds[id] },
33-
}));
30+
obj.updateState!(({ expandedIds, ...rest }, { allNodes }) => {
31+
const explicitExpandedState = expandedIds ? expandedIds[id] : undefined;
32+
const isExpanded = (explicitExpandedState === true)
33+
|| (allNodes[id] && allNodes[id].isActiveTrail && explicitExpandedState === undefined);
34+
return {
35+
...rest,
36+
expandedIds: { ...expandedIds, [id]: !isExpanded },
37+
};
38+
});
3439
};
3540
obj.setActiveId = (id: string | null) => {
3641
obj.updateState!((st) => ({ ...st, activeId: id }));

0 commit comments

Comments
 (0)