Skip to content

Commit

Permalink
fix(tree-view): do not traverse original nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Nov 22, 2024
1 parent 07d226c commit b8dc2f5
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/TreeView/TreeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,22 @@
});
/**
* Recursively flattens a tree of nodes into a single array
* @param {Array<TreeNode & { nodes?: TreeNode[] }>} nodes
* @returns {Array<TreeNode>}
*/
function traverse(nodes) {
let _nodes = [];
nodes.forEach((node) => {
_nodes.push(node);
if (Array.isArray(node.nodes)) {
_nodes = [...nodes, ...traverse(node.nodes)];
return nodes.reduce((acc, node) => {
acc.push(node);
if (Array.isArray(node.nodes) && node.nodes.length > 0) {
acc.push(...traverse(node.nodes));
}
});
return _nodes;
return acc;
}, []);
}
$: nodes = traverse(nodes);
$: nodeIds = nodes.map((node) => node.id);
$: flattenedNodes = traverse(nodes);
$: nodeIds = flattenedNodes.map((node) => node.id);
$: activeNodeId.set(activeId);
$: selectedNodeIds.set(selectedIds);
$: expandedNodeIds.set(expandedIds);
Expand Down

0 comments on commit b8dc2f5

Please sign in to comment.