diff --git a/demo/TreeViewExamples.jsx b/demo/TreeViewExamples.jsx index 02d4972..1d899a1 100644 --- a/demo/TreeViewExamples.jsx +++ b/demo/TreeViewExamples.jsx @@ -53,9 +53,9 @@ export function TreeViewExamples() { return ( ( + template={(item, index, parentItem, relativePath) => ( - {item.attrA} (index: {index} parent: {parentItem?.attrA || None}) + {item.attrA} (index: {index} parent: {parentItem?.attrA || None})

Path: {relativePath}

)} childrenPath={'children'} diff --git a/src/treeview/TreeView.jsx b/src/treeview/TreeView.jsx index 7cba627..631cb4b 100644 --- a/src/treeview/TreeView.jsx +++ b/src/treeview/TreeView.jsx @@ -7,7 +7,14 @@ export function TreeView({ childrenPath, draggable, nodes, template }) { return (
); @@ -19,7 +26,7 @@ TreeView.propTypes = { template: PropTypes.func.isRequired, }; -function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template }) { +function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template, relativePath }) { return ( <> {nodes.map((node, index) => ( @@ -32,6 +39,7 @@ function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template childrenPath={childrenPath} depth={depth} draggable={draggable} + relativePath={`${relativePath}[${index}]`} /> ))} @@ -45,15 +53,16 @@ TreeNodes.propTypes = { nodes: PropTypes.array, parentNode: PropTypes.object, template: PropTypes.func, + relativePath: PropTypes.string, }; -function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable }) { +function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable, relativePath }) { const childrenNodes = getValueByPath(node, childrenPath); return (
  • - {template(node, index, parentNode)} + {template(node, index, parentNode, relativePath)}
  • {childrenNodes && ( @@ -63,6 +72,7 @@ function TreeNode({ node, parentNode, index, template, childrenPath, depth, drag template={template} childrenPath={childrenPath} depth={depth + 1} + relativePath={`${relativePath}.${childrenPath}`} /> )}
    @@ -76,4 +86,5 @@ TreeNode.propTypes = { childrenPath: PropTypes.string, depth: PropTypes.number, draggable: PropTypes.bool, + relativePath: PropTypes.string, };