Skip to content

Commit 8cd061b

Browse files
author
dongxingbin
committed
feat:添加可折叠功能
1 parent 672c7c4 commit 8cd061b

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

src/org_tree.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ export const renderNode = (data, prop) => {
1414

1515
if (isLeaf(data, prop)) {
1616
cls.push('is-leaf');
17+
} else if (prop.collapsable && !data[node.expand]) {
18+
cls.push('collapsed');
1719
}
1820

1921
childNodes.push(renderLabel(data, prop));
2022

21-
if (data[node.expand]) {
23+
if (!prop.collapsable || data[node.expand]) {
2224
childNodes.push(renderChildren(data.children, prop));
2325
}
2426

@@ -28,6 +30,23 @@ export const renderNode = (data, prop) => {
2830
}, childNodes);
2931
};
3032

33+
// 创建展开折叠按钮
34+
export const renderBtn = (data, prop ) => {
35+
const { onExpand } = prop;
36+
const node = prop.node;
37+
38+
let cls = ['org-tree-node-btn'];
39+
40+
if (data[node.expand]) {
41+
cls.push('expanded');
42+
}
43+
44+
return React.createElement('span', {
45+
className: cls.join(' '),
46+
onClick: (e) => typeof onExpand === 'function' && onExpand(e, data)
47+
});
48+
};
49+
3150
// 创建 label 节点
3251
export const renderLabel = (data, prop) => {
3352
const node = prop.node;
@@ -43,12 +62,16 @@ export const renderLabel = (data, prop) => {
4362
childNodes.push(label);
4463
}
4564

65+
if (prop.collapsable && !isLeaf(data, prop)) {
66+
childNodes.push(renderBtn(data, prop));
67+
}
68+
4669
const cls = ['org-tree-node-label-inner'];
4770

4871
return React.createElement('div', {
4972
className: 'org-tree-node-label',
5073
}, [React.createElement('div', {
51-
className: cls.join(' ')
74+
className: cls.join(' '),
5275
}, childNodes)]);
5376
};
5477

src/org_tree.jsx

+22-5
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import classnames from 'classnames';
44

5-
import TreeNode from './org_tree.js';
5+
import TreeNode from './org_tree';
66

77
//组件
88
class OrgTree extends Component {
99
constructor(props) {
1010
super(props);
11+
this.handleExpand = this.handleExpand.bind(this);
1112
this.collapse = this.collapse.bind(this);
1213
this.toggleExpand = this.toggleExpand.bind(this);
1314
}
1415

1516
componentDidMount() {
16-
const { data } = this.props;
17-
this.toggleExpand(data, true);
17+
const { expandAll, data } = this.props;
18+
if(expandAll) this.toggleExpand(data, true);
1819
}
1920

2021
componentWillUnmount() {
2122
}
2223

24+
handleExpand(e, nodeData) {
25+
if ('expand' in nodeData) {
26+
nodeData.expand = !nodeData.expand;
27+
if (!nodeData.expand && nodeData.children) {
28+
this.collapse(nodeData.children);
29+
}
30+
this.forceUpdate();
31+
}else {
32+
nodeData.expand = true;
33+
this.forceUpdate();
34+
}
35+
}
36+
2337
collapse(list) {
2438
let _this = this;
2539
list.forEach(function(child) {
@@ -49,15 +63,16 @@ class OrgTree extends Component {
4963
}
5064

5165
render() {
52-
const { data, node, horizontal, renderContent } = this.props;
66+
const { horizontal, node, data } = this.props;
5367
return <div className="org-tree-container">
5468
<div className={classnames('org-tree', {
5569
'horizontal': horizontal
5670
})}>
5771
<TreeNode
5872
data={data}
5973
node={node}
60-
renderContent={renderContent}
74+
onExpand={(e, nodeData)=> this.handleExpand(e, nodeData)}
75+
{...this.props}
6176
/>
6277
</div>
6378
</div>;
@@ -68,6 +83,8 @@ OrgTree.propTypes = {
6883
data: PropTypes.object,
6984
node: PropTypes.object,
7085
horizontal: PropTypes.bool,
86+
collapsable: PropTypes.bool,
87+
expandAll: PropTypes.bool,
7188
renderContent: PropTypes.func
7289
};
7390

src/org_tree.less

+34-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
display: table-cell;
9292
vertical-align: top;
9393

94-
&.is-leaf {
94+
&.is-leaf, &.collapsed {
9595
padding-left: 10px;
9696
padding-right: 10px;
9797
}
@@ -116,6 +116,19 @@
116116
}
117117

118118
}
119+
.collapsable .org-tree-node.collapsed {
120+
padding-bottom: 30px;
121+
122+
.org-tree-node-label:after {
123+
content: '';
124+
position: absolute;
125+
top: 100%;
126+
left: 0;
127+
width: 50%;
128+
height: 20px;
129+
border-right: 1px solid #ddd;
130+
}
131+
}
119132
.org-tree > .org-tree-node {
120133
padding-top: 0;
121134

@@ -193,6 +206,26 @@
193206
vertical-align: middle;
194207
}
195208

209+
&.collapsable .org-tree-node.collapsed {
210+
padding-right: 30px;
211+
212+
.org-tree-node-label:after {
213+
top: 0;
214+
left: 100%;
215+
width: 20px;
216+
height: 50%;
217+
border-right: 0;
218+
border-bottom: 1px solid #ddd;
219+
}
220+
}
221+
222+
.org-tree-node-btn {
223+
top: 50%;
224+
left: 100%;
225+
margin-top: -11px;
226+
margin-left: 9px;
227+
}
228+
196229
& > .org-tree-node:only-child:before {
197230
border-bottom: 0;
198231
}
@@ -223,8 +256,4 @@
223256

224257
.text-center {
225258
text-align: center;
226-
}
227-
228-
.org-tree-node-label {
229-
white-space: nowrap;
230259
}

0 commit comments

Comments
 (0)