forked from chenglou/react-treeview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-treeview.js
68 lines (59 loc) · 1.78 KB
/
react-treeview.js
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
/** * @jsx React.DOM */
(function (root, React, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
var carry = function(React){
return factory(root, React);
};
define(['react'], carry);
} else {
// Browser globals
root.TreeView = factory(root, React);
}
})(this, typeof require === 'function' ? require('react') : React, function(window, React){
'use strict';
var TreeView = React.createClass({displayName: 'TreeView',
propTypes: {
collapsed: React.PropTypes.bool,
defaultCollapsed: React.PropTypes.bool,
nodeLabel: React.PropTypes.renderable.isRequired
},
getInitialState: function() {
return {collapsed: this.props.defaultCollapsed};
},
handleClick: function(a, b, c) {
this.setState({
collapsed: !this.state.collapsed
});
this.props.onClick && this.props.onClick(a, b, c);
},
render: function() {
var collapsed = this.props.collapsed != null ?
this.props.collapsed :
this.state.collapsed;
var arrowClassName = 'tree-view_arrow';
var containerClassName = 'tree-view_children';
if (collapsed) {
arrowClassName += ' tree-view_arrow-collapsed';
containerClassName += ' tree-view_children-collapsed';
}
var arrow =
React.DOM.div( {className:arrowClassName, onClick:this.handleClick}, "▾");
return (
React.DOM.div( {className:"tree-view"},
this.transferPropsTo(arrow),
this.props.nodeLabel,
React.DOM.div( {className:containerClassName},
this.props.children
)
)
);
}
});
if (typeof module === 'undefined') {
window.TreeView = TreeView;
} else {
module.exports = TreeView;
}
return TreeView;
});