Select root of tree by default in tree view

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2024-09-08 20:53:59 +02:00
parent cdcd43af5b
commit 1f1ca37fd7
1 changed files with 26 additions and 19 deletions

View File

@ -92,6 +92,13 @@ const TreeNode: FC<{
sortedLabelCards: [],
});
// Select the node when it is mounted and it is the root of the tree.
useEffect(() => {
if (parentRef === undefined) {
setSelectedNode({ id: nodeID, node: node });
}
}, [parentRef, setSelectedNode, nodeID, node]);
// Deselect node when node is unmounted.
useEffect(() => {
return () => {
@ -400,26 +407,26 @@ const TreeNode: FC<{
</Box>
</div>
);
} else {
return (
<div>
{innerNode}
{children.map((child, idx) => (
<Box ml={nodeIndent} key={idx}>
<TreeNode
node={child}
selectedNode={selectedNode}
setSelectedNode={setSelectedNode}
parentRef={nodeRef}
reverse={false}
childIdx={idx}
reportNodeState={childReportNodeState}
/>
</Box>
))}
</div>
);
}
return (
<div>
{innerNode}
{children.map((child, idx) => (
<Box ml={nodeIndent} key={idx}>
<TreeNode
node={child}
selectedNode={selectedNode}
setSelectedNode={setSelectedNode}
parentRef={nodeRef}
reverse={false}
childIdx={idx}
reportNodeState={childReportNodeState}
/>
</Box>
))}
</div>
);
};
export default TreeNode;