Skip to content

Commit

Permalink
Refactor expandChildNodes function and update setExpandedNodes logic
Browse files Browse the repository at this point in the history
  • Loading branch information
syedsalehinipg committed Mar 12, 2024
1 parent b40802d commit 943d7eb
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/TreeViewList/TreeViewList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,14 @@ const TreeViewList = ({
}
}
};
expandChildNodes(treeDisplayItems);
// update the expanded nodes with the searched nodes

// Convert the Set to an Array for comparison
const newExpandedNodes = [...searchedNodes];
// recursively expand the child nodes
expandChildNodes(treeDisplayItems);

// Only update the state if there are changes
// update the expanded nodes with the searched nodes
setExpandedNodes(prevState => {
const isSame =
prevState.length === newExpandedNodes.length &&
prevState.every((node, index) => node === newExpandedNodes[index]);

return isSame ? prevState : newExpandedNodes;
const merged = new Set([...prevState, ...searchedNodes]);
return Array.from(merged);
});
};

Expand Down

0 comments on commit 943d7eb

Please sign in to comment.