|
1 |
| -import { ElementType, useRef } from "react"; |
| 1 | +import { ElementType, useEffect } from "react"; |
2 | 2 | import { Link } from "@react-md/link";
|
3 | 3 | import {
|
4 | 4 | BaseTreeItem,
|
@@ -100,15 +100,23 @@ export function useLayoutNavigation<
|
100 | 100 | const { expandedIds, onItemExpansion, onMultiItemExpansion } =
|
101 | 101 | useTreeItemExpansion(() => getParentIds(itemId, navItems));
|
102 | 102 |
|
103 |
| - const prevItemId = useRef(itemId); |
104 |
| - const prevNavItems = useRef(navItems); |
105 |
| - if (prevItemId.current !== itemId || prevNavItems.current !== navItems) { |
106 |
| - prevItemId.current = itemId; |
107 |
| - prevNavItems.current = navItems; |
108 |
| - onMultiItemExpansion( |
109 |
| - Array.from(new Set([...expandedIds, ...getParentIds(itemId, navItems)])) |
110 |
| - ); |
111 |
| - } |
| 103 | + useEffect(() => { |
| 104 | + onMultiItemExpansion((prevExpandedIds) => { |
| 105 | + const nextExpandedIds = [ |
| 106 | + ...new Set([...prevExpandedIds, ...getParentIds(itemId, navItems)]), |
| 107 | + ]; |
| 108 | + if (nextExpandedIds.length !== prevExpandedIds.length) { |
| 109 | + return nextExpandedIds; |
| 110 | + } |
| 111 | + |
| 112 | + const prevSorted = prevExpandedIds.slice().sort(); |
| 113 | + const nextSorted = nextExpandedIds.slice().sort(); |
| 114 | + |
| 115 | + return nextSorted.some((itemId, index) => itemId !== prevSorted[index]) |
| 116 | + ? nextSorted |
| 117 | + : prevSorted; |
| 118 | + }); |
| 119 | + }, [itemId, navItems, onMultiItemExpansion]); |
112 | 120 |
|
113 | 121 | return {
|
114 | 122 | navItems,
|
|
0 commit comments