mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 16:04:23 +00:00
Refactor Node component to destructure props and improve onNodeClick handling
This commit is contained in:
@@ -41,21 +41,24 @@ const FileIcon = ({ node }: { node: NodeApi<FileNode> }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Define a Node component that matches what React-Arborist expects
|
// Define a Node component that matches what React-Arborist expects
|
||||||
function Node(props: {
|
function Node({
|
||||||
|
node,
|
||||||
|
style,
|
||||||
|
dragHandle,
|
||||||
|
onNodeClick,
|
||||||
|
...rest
|
||||||
|
}: {
|
||||||
node: NodeApi<FileNode>;
|
node: NodeApi<FileNode>;
|
||||||
style: React.CSSProperties;
|
style: React.CSSProperties;
|
||||||
dragHandle: React.Ref<HTMLDivElement>;
|
dragHandle?: React.Ref<HTMLDivElement>;
|
||||||
}) {
|
onNodeClick?: (node: NodeApi<FileNode>) => void;
|
||||||
const { node, style, dragHandle } = props;
|
// Accept any extra props from Arborist, but do not use an index signature
|
||||||
|
} & Record<string, unknown>) {
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
if (node.isInternal) {
|
if (node.isInternal) {
|
||||||
node.toggle();
|
node.toggle();
|
||||||
} else {
|
} else if (typeof onNodeClick === 'function') {
|
||||||
const treeProps = node.tree.props;
|
onNodeClick(node);
|
||||||
if (typeof treeProps.onNodeClick === 'function') {
|
|
||||||
treeProps.onNodeClick(node);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -73,6 +76,7 @@ function Node(props: {
|
|||||||
overflow: 'hidden',
|
overflow: 'hidden',
|
||||||
}}
|
}}
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
|
{...rest}
|
||||||
>
|
>
|
||||||
<FileIcon node={node} />
|
<FileIcon node={node} />
|
||||||
<span
|
<span
|
||||||
@@ -106,6 +110,14 @@ const FileTree: React.FC<FileTreeProps> = ({
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handler for node click
|
||||||
|
const onNodeClick = (node: NodeApi<FileNode>) => {
|
||||||
|
const fileNode = node.data;
|
||||||
|
if (!node.isInternal) {
|
||||||
|
void handleFileSelect(fileNode.path);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={target}
|
ref={target}
|
||||||
@@ -120,22 +132,13 @@ const FileTree: React.FC<FileTreeProps> = ({
|
|||||||
indent={24}
|
indent={24}
|
||||||
rowHeight={28}
|
rowHeight={28}
|
||||||
onActivate={(node) => {
|
onActivate={(node) => {
|
||||||
const fileNode = node.data as FileNode;
|
|
||||||
if (!node.isInternal) {
|
|
||||||
void handleFileSelect(fileNode.path);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
{...({
|
|
||||||
// Use a spread with type assertion to add onNodeClick
|
|
||||||
onNodeClick: (node: NodeApi<FileNode>) => {
|
|
||||||
const fileNode = node.data;
|
const fileNode = node.data;
|
||||||
if (!node.isInternal) {
|
if (!node.isInternal) {
|
||||||
void handleFileSelect(fileNode.path);
|
void handleFileSelect(fileNode.path);
|
||||||
}
|
}
|
||||||
},
|
}}
|
||||||
} as any)}
|
|
||||||
>
|
>
|
||||||
{Node}
|
{(props) => <Node {...props} onNodeClick={onNodeClick} />}
|
||||||
</Tree>
|
</Tree>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user