Skip to content

Commit a60abb8

Browse files
joaoassisbJoão Vitorassisrafael
authored
feat: include relative path as argument to render each node in tree view (#45)
* feat: include a afterChange function in useFormControl * wip * wip * wip * wip * wip * docs: includes instructions to run the application * docs: fix readme subtitles * wip * feat: includes exports of FormLabel and FormValidationFeedbac * wip * feat: pass the relative path to each node in tree view * wip * wip * wip * wip Co-authored-by: João Vitor <[email protected]> Co-authored-by: Igor Rafael <[email protected]>
1 parent f32e462 commit a60abb8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

demo/TreeViewExamples.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export function TreeViewExamples() {
5353
return (
5454
<TreeView
5555
nodes={nodes}
56-
template={(item, index, parentItem) => (
56+
template={(item, index, parentItem, relativePath) => (
5757
<em>
58-
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})
58+
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})<p>Path: {relativePath}</p>
5959
</em>
6060
)}
6161
childrenPath={'children'}

src/treeview/TreeView.jsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ export function TreeView({ childrenPath, draggable, nodes, template }) {
77
return (
88
<div className="rbu-treeview">
99
<ul className="list-group">
10-
<TreeNodes depth={0} nodes={nodes} template={template} childrenPath={childrenPath} draggable={draggable} />
10+
<TreeNodes
11+
depth={0}
12+
nodes={nodes}
13+
template={template}
14+
childrenPath={childrenPath}
15+
draggable={draggable}
16+
relativePath={`${childrenPath}`}
17+
/>
1118
</ul>
1219
</div>
1320
);
@@ -19,7 +26,7 @@ TreeView.propTypes = {
1926
template: PropTypes.func.isRequired,
2027
};
2128

22-
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template }) {
29+
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template, relativePath }) {
2330
return (
2431
<>
2532
{nodes.map((node, index) => (
@@ -32,6 +39,7 @@ function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template
3239
childrenPath={childrenPath}
3340
depth={depth}
3441
draggable={draggable}
42+
relativePath={`${relativePath}[${index}]`}
3543
/>
3644
))}
3745
</>
@@ -45,15 +53,16 @@ TreeNodes.propTypes = {
4553
nodes: PropTypes.array,
4654
parentNode: PropTypes.object,
4755
template: PropTypes.func,
56+
relativePath: PropTypes.string,
4857
};
4958

50-
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable }) {
59+
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable, relativePath }) {
5160
const childrenNodes = getValueByPath(node, childrenPath);
5261

5362
return (
5463
<div draggable={draggable}>
5564
<li className="list-group-item" style={{ marginLeft: `${depth * 20}px`, borderTopWidth: '1px' }}>
56-
{template(node, index, parentNode)}
65+
{template(node, index, parentNode, relativePath)}
5766
</li>
5867

5968
{childrenNodes && (
@@ -63,6 +72,7 @@ function TreeNode({ node, parentNode, index, template, childrenPath, depth, drag
6372
template={template}
6473
childrenPath={childrenPath}
6574
depth={depth + 1}
75+
relativePath={`${relativePath}.${childrenPath}`}
6676
/>
6777
)}
6878
</div>
@@ -76,4 +86,5 @@ TreeNode.propTypes = {
7686
childrenPath: PropTypes.string,
7787
depth: PropTypes.number,
7888
draggable: PropTypes.bool,
89+
relativePath: PropTypes.string,
7990
};

0 commit comments

Comments
 (0)