Skip to content

feat: include nodePath as argument to template functions in TreeView nodes #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions demo/TreeViewExamples.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { TreeView } from '../dist/main';
import { Form, FormInput, TreeView } from '../dist/main';

export function TreeViewExamples() {
const nodes = [
Expand Down Expand Up @@ -51,15 +51,36 @@ export function TreeViewExamples() {
];

return (
<TreeView
nodes={nodes}
template={(item, index, parentItem) => (
<em>
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})
</em>
)}
childrenPath={'children'}
draggable={true}
/>
<div>
<h1 className="h4">Simple Tree View</h1>

<TreeView
nodes={nodes}
template={(item, index, parentItem) => (
<em>
{item.attrA} (index: {index} parent: {parentItem?.attrA || <em>None</em>})
</em>
)}
childrenPath={'children'}
draggable={true}
/>

<div className="mt-4">
<h1 className="h4">Tree View with Form</h1>
<Form initialValues={{ nodes }} onSubmit={(newValues) => console.log(newValues)}>
<TreeView
nodes={nodes}
template={(item, index, parentItem, path) => (
<em>
<p>Path: path</p>
<FormInput name={`nodes${path}.attrA`} />
</em>
)}
childrenPath={'children'}
draggable={true}
/>
</Form>
</div>
</div>
);
}
14 changes: 11 additions & 3 deletions src/treeview/TreeView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TreeView.propTypes = {
template: PropTypes.func.isRequired,
};

function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template }) {
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template, nodePath }) {
return (
<>
{nodes.map((node, index) => (
Expand All @@ -32,28 +32,34 @@ function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template
childrenPath={childrenPath}
depth={depth}
draggable={draggable}
nodePath={`${nodePath}[${index}]`}
/>
))}
</>
);
}

TreeNodes.defaultProps = {
nodePath: '',
};

TreeNodes.propTypes = {
childrenPath: PropTypes.string,
depth: PropTypes.number,
draggable: PropTypes.bool,
nodes: PropTypes.array,
parentNode: PropTypes.object,
template: PropTypes.func,
nodePath: PropTypes.string,
};

function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable }) {
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable, nodePath }) {
const childrenNodes = getValueByPath(node, childrenPath);

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

{childrenNodes && (
Expand All @@ -63,6 +69,7 @@ function TreeNode({ node, parentNode, index, template, childrenPath, depth, drag
template={template}
childrenPath={childrenPath}
depth={depth + 1}
nodePath={`${nodePath}.${childrenPath}`}
/>
)}
</div>
Expand All @@ -76,4 +83,5 @@ TreeNode.propTypes = {
childrenPath: PropTypes.string,
depth: PropTypes.number,
draggable: PropTypes.bool,
nodePath: PropTypes.string,
};