Skip to content
Permalink
Browse files
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.
  • Loading branch information
mlaursen committed Jul 12, 2020
1 parent fadddc7 commit 854762991dfab43a89191ee29cd2acc7e43ec236
Showing 1 changed file with 27 additions and 6 deletions.
@@ -289,13 +289,34 @@ export default function useTreeMovement({
new Set([...expandedIds, ...expectedExpandedIds])
);
if (nextIds.length !== expandedIds.length) {
const index = flattenedItems.findIndex(
({ itemId }) => itemId === item.itemId
);

onMultiItemExpansion(nextIds);
if (index !== -1) {
setFocusedIndex(index);

// since new items will be rendered, need to also update the focused
// index so the currently active item is still the "focused" item
//
// TODO: Look into a much better way to handle this sort of stuff..
// This still doesn't correctly scroll the active element into view.
// I should probably move all the scroll behavior into a useEffect
// for whenever the focusedIndex changes.
let visibleCount = 0;
const lookup: Record<TreeItemId, boolean> = {};
for (let i = 0; i < flattenedItems.length; i += 1) {
const item = flattenedItems[i];
let isVisible = item.parentId === rootId;
if (item.parentId !== null && nextIds.includes(item.parentId)) {
isVisible = !!lookup[item.parentId];
}

lookup[item.itemId] = isVisible;

if (itemId === item.itemId) {
setFocusedIndex(visibleCount);
return;
}

if (isVisible) {
visibleCount += 1;
}
}
}
}

0 comments on commit 8547629

Please sign in to comment.