Skip to content

Commit 163543a

Browse files
committed
fix(treeview): include parentNode on template function
1 parent 319a617 commit 163543a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

demo/TreeViewExamples.jsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ export function TreeViewExamples() {
5151
];
5252

5353
return (
54-
<TreeView nodes={nodes} template={(item) => <em>{item.attrA}</em>} childrenPath={'children'} draggable={true} />
54+
<TreeView
55+
nodes={nodes}
56+
template={(item, index, parentItem) => (
57+
<em>
58+
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})
59+
</em>
60+
)}
61+
childrenPath={'children'}
62+
draggable={true}
63+
/>
5564
);
5665
}

src/treeview/TreeView.jsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ TreeView.propTypes = {
1919
template: PropTypes.func.isRequired,
2020
};
2121

22-
function TreeNodes({ childrenPath, depth, draggable, nodes, template }) {
22+
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template }) {
2323
return (
2424
<>
2525
{nodes.map((node, index) => (
2626
<TreeNode
2727
key={index}
2828
node={node}
29+
parentNode={parentNode}
2930
index={index}
3031
template={template}
3132
childrenPath={childrenPath}
@@ -42,26 +43,34 @@ TreeNodes.propTypes = {
4243
depth: PropTypes.number,
4344
draggable: PropTypes.bool,
4445
nodes: PropTypes.array,
46+
parentNode: PropTypes.object,
4547
template: PropTypes.func,
4648
};
4749

48-
function TreeNode({ node, index, template, childrenPath, depth, draggable }) {
50+
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable }) {
4951
const childrenNodes = getValueByPath(node, childrenPath);
5052

5153
return (
5254
<div draggable={draggable}>
5355
<li className="list-group-item" style={{ marginLeft: `${depth * 20}px`, borderTopWidth: '1px' }}>
54-
{template(node, index)}
56+
{template(node, index, parentNode)}
5557
</li>
5658

5759
{childrenNodes && (
58-
<TreeNodes nodes={childrenNodes} template={template} childrenPath={childrenPath} depth={depth + 1} />
60+
<TreeNodes
61+
nodes={childrenNodes}
62+
parentNode={node}
63+
template={template}
64+
childrenPath={childrenPath}
65+
depth={depth + 1}
66+
/>
5967
)}
6068
</div>
6169
);
6270
}
6371
TreeNode.propTypes = {
6472
node: PropTypes.object,
73+
parentNode: PropTypes.object,
6574
index: PropTypes.number,
6675
template: PropTypes.func,
6776
childrenPath: PropTypes.string,

0 commit comments

Comments
 (0)