Skip to content

Commit 8547629

Browse files
committed
fix: Tree focused index after expanding all with asterisk
This doesn't really fix it 100% since I need to also scroll the item back into view after the expansion occurs. I probably need to redo the tree movement for that to work though.
1 parent fadddc7 commit 8547629

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

packages/tree/src/useTreeMovement.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,34 @@ export default function useTreeMovement({
289289
new Set([...expandedIds, ...expectedExpandedIds])
290290
);
291291
if (nextIds.length !== expandedIds.length) {
292-
const index = flattenedItems.findIndex(
293-
({ itemId }) => itemId === item.itemId
294-
);
295-
296292
onMultiItemExpansion(nextIds);
297-
if (index !== -1) {
298-
setFocusedIndex(index);
293+
294+
// since new items will be rendered, need to also update the focused
295+
// index so the currently active item is still the "focused" item
296+
//
297+
// TODO: Look into a much better way to handle this sort of stuff..
298+
// This still doesn't correctly scroll the active element into view.
299+
// I should probably move all the scroll behavior into a useEffect
300+
// for whenever the focusedIndex changes.
301+
let visibleCount = 0;
302+
const lookup: Record<TreeItemId, boolean> = {};
303+
for (let i = 0; i < flattenedItems.length; i += 1) {
304+
const item = flattenedItems[i];
305+
let isVisible = item.parentId === rootId;
306+
if (item.parentId !== null && nextIds.includes(item.parentId)) {
307+
isVisible = !!lookup[item.parentId];
308+
}
309+
310+
lookup[item.itemId] = isVisible;
311+
312+
if (itemId === item.itemId) {
313+
setFocusedIndex(visibleCount);
314+
return;
315+
}
316+
317+
if (isVisible) {
318+
visibleCount += 1;
319+
}
299320
}
300321
}
301322
}

0 commit comments

Comments
 (0)