Skip to content
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
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ yarn add react-org-tree
import OrgTree from 'react-org-tree';

const horizontal = false; // true:横向 false:纵向
const collapsable = true; // true:可折叠 false:不可折叠
const expandAll = true; // true: 全部展开 false:全部折叠

const data = {
id: 0,
label: 'XXX股份有限公司',
Expand Down Expand Up @@ -45,18 +48,28 @@ const data = {
<OrgTree
data={data}
horizontal={horizontal}
collapsable={collapsable}
expandAll={expandAll}
>
```

### Preview

`horizontal`
`横向(horizontal)`

<img src="https://upload-images.jianshu.io/upload_images/3100736-1b67a2ff46365c3a.png" width="939px" style="display:inline;">

`纵向(vertical)`

<img src="https://upload-images.jianshu.io/upload_images/3100736-efc4287df27b79fc.png" width="854px" style="display:inline;">

`部分展开(expand)`

<img src="https://github.com/artdong/react-org-tree/blob/master/img/1.png" width="939px" style="display:inline;">
<img src="https://upload-images.jianshu.io/upload_images/3100736-ba7d1e5f955f2d7f.png" width="673px" style="display:inline;">

`vertical`
`全部展开(expandAll)`

<img src="https://github.com/artdong/react-org-tree/blob/master/img/2.png" width="854px" style="display:inline;">
<img src="https://upload-images.jianshu.io/upload_images/3100736-9a338b3b8b6ee580.png" width="773px" style="display:inline;">



Expand Down
Binary file added img/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-org-tree",
"version": "0.1.0",
"version": "0.2.0",
"description": "a simple organization tree component based on react",
"main": "dist/bundle.js",
"files": [
Expand Down
27 changes: 25 additions & 2 deletions src/org_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const renderNode = (data, prop) => {

if (isLeaf(data, prop)) {
cls.push('is-leaf');
} else if (prop.collapsable && !data[node.expand]) {
cls.push('collapsed');
}

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

if (data[node.expand]) {
if (!prop.collapsable || data[node.expand]) {
childNodes.push(renderChildren(data.children, prop));
}

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

// 创建展开折叠按钮
export const renderBtn = (data, prop ) => {
const { onExpand } = prop;
const node = prop.node;

let cls = ['org-tree-node-btn'];

if (data[node.expand]) {
cls.push('expanded');
}

return React.createElement('span', {
className: cls.join(' '),
onClick: (e) => typeof onExpand === 'function' && onExpand(e, data)
});
};

// 创建 label 节点
export const renderLabel = (data, prop) => {
const node = prop.node;
Expand All @@ -43,12 +62,16 @@ export const renderLabel = (data, prop) => {
childNodes.push(label);
}

if (prop.collapsable && !isLeaf(data, prop)) {
childNodes.push(renderBtn(data, prop));
}

const cls = ['org-tree-node-label-inner'];

return React.createElement('div', {
className: 'org-tree-node-label',
}, [React.createElement('div', {
className: cls.join(' ')
className: cls.join(' '),
}, childNodes)]);
};

Expand Down
27 changes: 22 additions & 5 deletions src/org_tree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';

import TreeNode from './org_tree.js';
import TreeNode from './org_tree';

//组件
class OrgTree extends Component {
constructor(props) {
super(props);
this.handleExpand = this.handleExpand.bind(this);
this.collapse = this.collapse.bind(this);
this.toggleExpand = this.toggleExpand.bind(this);
}

componentDidMount() {
const { data } = this.props;
this.toggleExpand(data, true);
const { expandAll, data } = this.props;
if(expandAll) this.toggleExpand(data, true);
}

componentWillUnmount() {
}

handleExpand(e, nodeData) {
if ('expand' in nodeData) {
nodeData.expand = !nodeData.expand;
if (!nodeData.expand && nodeData.children) {
this.collapse(nodeData.children);
}
this.forceUpdate();
}else {
nodeData.expand = true;
this.forceUpdate();
}
}

collapse(list) {
let _this = this;
list.forEach(function(child) {
Expand Down Expand Up @@ -49,15 +63,16 @@ class OrgTree extends Component {
}

render() {
const { data, node, horizontal, renderContent } = this.props;
const { horizontal, node, data } = this.props;
return <div className="org-tree-container">
<div className={classnames('org-tree', {
'horizontal': horizontal
})}>
<TreeNode
data={data}
node={node}
renderContent={renderContent}
onExpand={(e, nodeData)=> this.handleExpand(e, nodeData)}
{...this.props}
/>
</div>
</div>;
Expand All @@ -68,6 +83,8 @@ OrgTree.propTypes = {
data: PropTypes.object,
node: PropTypes.object,
horizontal: PropTypes.bool,
collapsable: PropTypes.bool,
expandAll: PropTypes.bool,
renderContent: PropTypes.func
};

Expand Down
39 changes: 34 additions & 5 deletions src/org_tree.less
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
display: table-cell;
vertical-align: top;

&.is-leaf {
&.is-leaf, &.collapsed {
padding-left: 10px;
padding-right: 10px;
}
Expand All @@ -116,6 +116,19 @@
}

}
.collapsable .org-tree-node.collapsed {
padding-bottom: 30px;

.org-tree-node-label:after {
content: '';
position: absolute;
top: 100%;
left: 0;
width: 50%;
height: 20px;
border-right: 1px solid #ddd;
}
}
.org-tree > .org-tree-node {
padding-top: 0;

Expand Down Expand Up @@ -193,6 +206,26 @@
vertical-align: middle;
}

&.collapsable .org-tree-node.collapsed {
padding-right: 30px;

.org-tree-node-label:after {
top: 0;
left: 100%;
width: 20px;
height: 50%;
border-right: 0;
border-bottom: 1px solid #ddd;
}
}

.org-tree-node-btn {
top: 50%;
left: 100%;
margin-top: -11px;
margin-left: 9px;
}

& > .org-tree-node:only-child:before {
border-bottom: 0;
}
Expand Down Expand Up @@ -223,8 +256,4 @@

.text-center {
text-align: center;
}

.org-tree-node-label {
white-space: nowrap;
}