Skip to content

Commit

Permalink
fix: Tree focused index after expanding all with asterisk
Browse files Browse the repository at this point in the history
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 8547629
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/tree/src/useTreeMovement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit 8547629

Please sign in to comment.