@@ -2,24 +2,38 @@ import React, { Component } from 'react';
22import PropTypes from 'prop-types' ;
33import classnames from 'classnames' ;
44
5- import TreeNode from './org_tree.js ' ;
5+ import TreeNode from './org_tree' ;
66
77//组件
88class 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
0 commit comments