Skip to content

Commit 0d01881

Browse files
committed
feat(treeview): include an initial version of the TreeView component
1 parent 66dc6d0 commit 0d01881

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

demo/TreeViewExamples.jsx

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
3+
import { TreeView } from '../dist/main';
4+
5+
export function TreeViewExamples() {
6+
const nodes = [
7+
{
8+
attrA: '1. Attr A Value',
9+
children: [
10+
{
11+
attrA: '1.1. Attr A Value',
12+
children: [
13+
{
14+
attrA: '1.1.1. Attr A Value',
15+
children: [
16+
{
17+
attrA: '1.1.1.1. Attr A Value',
18+
children: [
19+
{
20+
attrA: '1.1.1.1.1. Attr A Value',
21+
children: [
22+
{
23+
attrA: '1.1.1.1.1.1. Attr A Value',
24+
children: [
25+
{
26+
attrA: '1.1.1.1.1.1.1. Attr A Value',
27+
},
28+
],
29+
},
30+
],
31+
},
32+
],
33+
},
34+
],
35+
},
36+
],
37+
},
38+
],
39+
},
40+
{
41+
attrA: '2. Attr A Value',
42+
children: [
43+
{
44+
attrA: '2.1. Attr A Value',
45+
},
46+
{
47+
attrA: '2.2. Attr A Value',
48+
},
49+
],
50+
},
51+
];
52+
53+
return <TreeView nodes={nodes} template={(item, index) => <em>{item.attrA}</em>} childrenPath={'children'} draggable={true} />;
54+
}

demo/demo.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/max-dependencies */
12
/* eslint-disable no-console */
23
import React from 'react';
34
import ReactDOM from 'react-dom';
@@ -12,14 +13,15 @@ import { DialogExamples } from './DialogExamples';
1213
import { ListGroupExamples } from './ListGroupExamples';
1314
import { ToastsExamples } from './ToastsExamples';
1415
import { DropdownExamples } from './DropdownExamples';
16+
import { TreeViewExamples } from './TreeViewExamples';
1517

1618
ReactDOM.render(
1719
<div className="mt-3">
1820
<React.StrictMode>
1921
<StatefulTabs
2022
vertical={true}
2123
onlyRenderActiveTab={true}
22-
initialTab={0}
24+
initialTab={8}
2325
tabs={[
2426
{
2527
title: 'Dialog',
@@ -57,6 +59,10 @@ ReactDOM.render(
5759
</ToastsContainer>
5860
),
5961
},
62+
{
63+
title: 'TreeView',
64+
content: <TreeViewExamples />,
65+
},
6066
]}
6167
/>
6268
</React.StrictMode>

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './mixed';
55
export * from './table';
66
export * from './tabs';
77
export * from './toasts';
8+
export * from './treeview';
89
export * from './utils';

src/treeview/TreeView.jsx

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import { getValueByPath } from '../utils';
5+
6+
export function TreeView({ childrenPath, draggable, nodes, template }) {
7+
return (
8+
<div className="rbu-treeview">
9+
<ul className="list-group">
10+
<TreeNodes depth={0} nodes={nodes} template={template} childrenPath={childrenPath} draggable={draggable} />
11+
</ul>
12+
</div>
13+
);
14+
}
15+
TreeView.propTypes = {
16+
childrenPath: PropTypes.string.isRequired,
17+
draggable: PropTypes.bool,
18+
nodes: PropTypes.array.isRequired,
19+
template: PropTypes.func.isRequired,
20+
};
21+
22+
function TreeNodes({ childrenPath, depth, draggable, nodes, template }) {
23+
return (
24+
<>
25+
{nodes.map((node, index) => (
26+
<TreeNode key={index} node={node} index={index} template={template} childrenPath={childrenPath} depth={depth} />
27+
))}
28+
</>
29+
);
30+
}
31+
32+
TreeNodes.propTypes = {
33+
childrenPath: PropTypes.string,
34+
depth: PropTypes.number,
35+
draggable: PropTypes.bool,
36+
nodes: PropTypes.array,
37+
template: PropTypes.func,
38+
};
39+
40+
function TreeNode({ node, index, template, childrenPath, depth, draggable }) {
41+
const childrenNodes = getValueByPath(node, childrenPath);
42+
43+
return (
44+
<div draggable={draggable}>
45+
<li className="list-group-item" style={{ marginLeft: `${depth * 20}px`, borderTopWidth: '1px' }}>
46+
{template(node, index)}
47+
</li>
48+
49+
{childrenNodes && (
50+
<TreeNodes nodes={childrenNodes} template={template} childrenPath={childrenPath} depth={depth + 1} />
51+
)}
52+
</div>
53+
);
54+
}
55+
TreeNode.propTypes = {
56+
node: PropTypes.object,
57+
index: PropTypes.number,
58+
template: PropTypes.func,
59+
childrenPath: PropTypes.string,
60+
depth: PropTypes.number,
61+
draggable: PropTypes.bool,
62+
};

src/treeview/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { TreeView } from './TreeView';
2+
3+
export * from './TreeView';

0 commit comments

Comments
 (0)