1 | import React, { Component } from 'react';
|
2 | import classnames from 'classnames';
|
3 | import { FlexDiv } from 'ws-react-flex-layout';
|
4 | import FontIcon from 'material-ui/FontIcon';
|
5 |
|
6 | class WsReactTreeMenu extends React.Component {
|
7 | constructor(props) {
|
8 | super(props);
|
9 |
|
10 | this.DEFAULT_PREFIX_CLASS = 'menu-tree';
|
11 | }
|
12 |
|
13 | componentWillReceiveProps(newProps) {
|
14 | newProps.items.forEach(item => {
|
15 | if (this.isSelectItem(item)) {
|
16 | item.isOpen = true;
|
17 | this.forceUpdate();
|
18 | }
|
19 | });
|
20 | }
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 | isSelectItem(item) {
|
27 | if (this.props.addConditionForSelect && this.props.addConditionForSelect(item)) {
|
28 | return true;
|
29 | }
|
30 | return false;
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 | toggleChild(item) {
|
38 | item.isOpen = !item.isOpen;
|
39 | this.forceUpdate();
|
40 | }
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 | handlerClick(item) {
|
47 | if (item && item.child && item.child.length) {
|
48 | this.toggleChild(item);
|
49 | }
|
50 | this.props.handlerClick(item);
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 | getClassItem(item, prefixClass) {
|
58 | let itemClassObj = {};
|
59 | itemClassObj[this.DEFAULT_PREFIX_CLASS + '__item--select'] = this.isSelectItem(item);
|
60 | itemClassObj[prefixClass + '__item--select'] = this.isSelectItem(item);
|
61 | return itemClassObj;
|
62 | }
|
63 |
|
64 | render() {
|
65 | const prefixClass = this.props.prefixClass ? this.props.prefixClass : this.DEFAULT_PREFIX_CLASS;
|
66 | return (
|
67 | <div>
|
68 | {
|
69 | this.props.items.map((item, index) =>
|
70 | <div key={index} className={classnames(
|
71 | this.DEFAULT_PREFIX_CLASS + '__item-wrap',
|
72 | prefixClass + '__item-wrap'
|
73 | )}>
|
74 | <FlexDiv row="start start" className={classnames(
|
75 | this.DEFAULT_PREFIX_CLASS + '__item',
|
76 | prefixClass + '__item',
|
77 | 'pointer',
|
78 | this.getClassItem(item, prefixClass)
|
79 | )} onClick={() => this.handlerClick(item)}>
|
80 | <FlexDiv flex={true}>{item.title}</FlexDiv>
|
81 | {
|
82 | !!item.child && !!item.child.length &&
|
83 | <FontIcon className={classnames(
|
84 | 'material-icons',
|
85 | 'pointer'
|
86 | )}>{item.isOpen ? 'keyboard_arrow_up' : 'keyboard_arrow_down'}</FontIcon>
|
87 | }
|
88 | </FlexDiv>
|
89 |
|
90 | {
|
91 | !!item.isOpen && !!item.child && !!item.child.length &&
|
92 | <div className={classnames(
|
93 | this.DEFAULT_PREFIX_CLASS + '__item-child-wrap',
|
94 | prefixClass + '__item-child-wrap'
|
95 | )}>
|
96 | <WsReactTreeMenu
|
97 | prefixClass={prefixClass}
|
98 | addConditionForSelect={this.props.addConditionForSelect}
|
99 | handlerClick={this.props.handlerClick}
|
100 | items={item.child} />
|
101 | </div>
|
102 | }
|
103 | </div>
|
104 | )
|
105 | }
|
106 | </div>
|
107 | );
|
108 | }
|
109 | }
|
110 |
|
111 | export default WsReactTreeMenu; |
\ | No newline at end of file |