Skip to content

Commit 1beb583

Browse files
authored
feat: include nodePath as argument to template functions in TreeView nodes (#48)
1 parent 9a677e1 commit 1beb583

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

demo/TreeViewExamples.jsx

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { TreeView } from '../dist/main';
3+
import { Form, FormInput, TreeView } from '../dist/main';
44

55
export function TreeViewExamples() {
66
const nodes = [
@@ -51,15 +51,36 @@ export function TreeViewExamples() {
5151
];
5252

5353
return (
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-
/>
54+
<div>
55+
<h1 className="h4">Simple Tree View</h1>
56+
57+
<TreeView
58+
nodes={nodes}
59+
template={(item, index, parentItem) => (
60+
<em>
61+
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})
62+
</em>
63+
)}
64+
childrenPath={'children'}
65+
draggable={true}
66+
/>
67+
68+
<div className="mt-4">
69+
<h1 className="h4">Tree View with Form</h1>
70+
<Form initialValues={{ nodes }} onSubmit={(newValues) => console.log(newValues)}>
71+
<TreeView
72+
nodes={nodes}
73+
template={(item, index, parentItem, path) => (
74+
<em>
75+
<p>Path: path</p>
76+
<FormInput name={`nodes${path}.attrA`} />
77+
</em>
78+
)}
79+
childrenPath={'children'}
80+
draggable={true}
81+
/>
82+
</Form>
83+
</div>
84+
</div>
6485
);
6586
}

src/treeview/TreeView.jsx

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

22-
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template }) {
22+
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template, nodePath }) {
2323
return (
2424
<>
2525
{nodes.map((node, index) => (
@@ -32,28 +32,34 @@ function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template
3232
childrenPath={childrenPath}
3333
depth={depth}
3434
draggable={draggable}
35+
nodePath={`${nodePath}[${index}]`}
3536
/>
3637
))}
3738
</>
3839
);
3940
}
4041

42+
TreeNodes.defaultProps = {
43+
nodePath: '',
44+
};
45+
4146
TreeNodes.propTypes = {
4247
childrenPath: PropTypes.string,
4348
depth: PropTypes.number,
4449
draggable: PropTypes.bool,
4550
nodes: PropTypes.array,
4651
parentNode: PropTypes.object,
4752
template: PropTypes.func,
53+
nodePath: PropTypes.string,
4854
};
4955

50-
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable }) {
56+
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable, nodePath }) {
5157
const childrenNodes = getValueByPath(node, childrenPath);
5258

5359
return (
5460
<div draggable={draggable}>
5561
<li className="list-group-item" style={{ marginLeft: `${depth * 20}px`, borderTopWidth: '1px' }}>
56-
{template(node, index, parentNode)}
62+
{template(node, index, parentNode, nodePath)}
5763
</li>
5864

5965
{childrenNodes && (
@@ -63,6 +69,7 @@ function TreeNode({ node, parentNode, index, template, childrenPath, depth, drag
6369
template={template}
6470
childrenPath={childrenPath}
6571
depth={depth + 1}
72+
nodePath={`${nodePath}.${childrenPath}`}
6673
/>
6774
)}
6875
</div>
@@ -76,4 +83,5 @@ TreeNode.propTypes = {
7683
childrenPath: PropTypes.string,
7784
depth: PropTypes.number,
7885
draggable: PropTypes.bool,
86+
nodePath: PropTypes.string,
7987
};

0 commit comments

Comments
 (0)