OrgTree.js
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';
import cx from 'classnames';
import s from './form.scss';
import { Tree,TreeSelect} from 'antd';
const TreeNode = TreeSelect.TreeNode;
import {getOrganizations,getCompany} from '../../../utils/orgUtil';
import {noop, setLeaf, getNewTreeData} from '../../../utils/commonUtils';
import {connect} from 'react-redux';
import {notification} from 'antd';
const openNotificationWithIcon = function (type) {
notification[type]({
message: '提示',
description: '上级部门不能选自己',
});
};
class OrgTree extends React.Component {
constructor(props) {
super(props);
this.onLoadData=this.onLoadData.bind(this);
this.onSelect=this.onSelect.bind(this);
this.state={
treeData: [],
value:''
};
}
static propTypes = {
onSelect:PropTypes.func,
defaultValue:PropTypes.string
}
static defaultProps={
defaultValue:''
}
componentDidMount(){
const {defaultValue}=this.props;
this.setState({
value:defaultValue
});
getCompany({}).then((data)=>{
console.log('my antform company isssss:', data);
data.items[0]['key'] = data.items[0].uuid;
this.setState({
treeData : data.items
});
});
}
componentWillReceiveProps (nextProps) {
const {defaultValue}=nextProps;
if(defaultValue&&this.props.defaultValue!=defaultValue){
this.setState({
value:defaultValue
});
}
}
onLoadData(treeNode) {
return new Promise((resolve) => {
const { treeData } = this.state;
let _treeData = treeData;
getOrganizations({pId:treeNode.props.eventKey}).then((data)=>{
if (data.items&&data.items.length>0) {
for (let item of data.items) {
item['key']=item.uuid;
}
}
getNewTreeData(_treeData, treeNode.props.eventKey, data.items);
this.setState({
treeData : _treeData
});
});
resolve();
});
}
onSelect(value, node, extra){
const {currentOrganization}=this.props;
if(value==currentOrganization.uuid){
openNotificationWithIcon('warning');
return false;
}
this.setState({ value });
this.props.onSelect(value, node);
}
render(){
const self=this;
const loop = (data) => data.map((item) => {
if (item.children) {
return <TreeNode title={item.name} key={item.key} value={item.key}>{loop(item.children)}</TreeNode>;
}
let isLeaf = false;
if (item.is_leaf=='y') {
isLeaf = true;
}
return <TreeNode title={item.name} key={item.key} value={item.key} isLeaf={isLeaf}></TreeNode>;
});
const treeNodes = loop(this.state.treeData);
return(
<TreeSelect
value={this.state.value}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
placeholder="请选择"
allowClear
onSelect={this.onSelect}
loadData={this.onLoadData} >
{treeNodes}
</TreeSelect>
)
}
}
// const mapStateToProps = (state) => {
// const { organization:{ currentOrganization } } = state;
// return {
// currentOrganization
// };
// }
// export default connect(mapStateToProps)(OrgTree);
export default OrgTree;