This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCollapsableMixin.js
More file actions
120 lines (97 loc) · 2.82 KB
/
CollapsableMixin.js
File metadata and controls
120 lines (97 loc) · 2.82 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
var React = require('react');
var TransitionEvents = require('./utils/TransitionEvents');
var CollapsableMixin = {
propTypes: {
collapsable: React.PropTypes.bool,
defaultExpanded: React.PropTypes.bool,
expanded: React.PropTypes.bool
},
getInitialState: function () {
return {
expanded: this.props.defaultExpanded != null ? this.props.defaultExpanded : null,
collapsing: false
};
},
handleTransitionEnd: function () {
this._collapseEnd = true;
this.setState({
collapsing: false
});
},
componentWillReceiveProps: function (newProps) {
if (this.props.collapsable && newProps.expanded !== this.props.expanded) {
this._collapseEnd = false;
this.setState({
collapsing: true
});
}
},
_addEndTransitionListener: function () {
var node = this.getCollapsableDOMNode();
if (node) {
TransitionEvents.addEndEventListener(
node,
this.handleTransitionEnd
);
}
},
_removeEndTransitionListener: function () {
var node = this.getCollapsableDOMNode();
if (node) {
TransitionEvents.removeEndEventListener(
node,
this.handleTransitionEnd
);
}
},
componentDidMount: function () {
this._afterRender();
},
componentWillUnmount: function () {
this._removeEndTransitionListener();
},
componentWillUpdate: function (nextProps) {
var dimension = (typeof this.getCollapsableDimension === 'function') ?
this.getCollapsableDimension() : 'height';
var node = this.getCollapsableDOMNode();
this._removeEndTransitionListener();
},
componentDidUpdate: function (prevProps, prevState) {
this._afterRender();
},
_afterRender: function () {
if (!this.props.collapsable) {
return;
}
this._addEndTransitionListener();
setTimeout(this._updateDimensionAfterRender, 0);
},
_updateDimensionAfterRender: function () {
var node = this.getCollapsableDOMNode();
if (node) {
var dimension = (typeof this.getCollapsableDimension === 'function') ?
this.getCollapsableDimension() : 'height';
node.style[dimension] = this.isExpanded() ?
this.getCollapsableDimensionValue() + 'px' : '0px';
}
},
isExpanded: function () {
return (this.props.expanded != null) ?
this.props.expanded : this.state.expanded;
},
getCollapsableClassSet: function (className) {
var classes = {};
if (typeof className === 'string') {
className.split(' ').forEach(function (className) {
if (className) {
classes[className] = true;
}
});
}
classes.collapsing = this.state.collapsing;
classes.collapse = !this.state.collapsing;
classes['in'] = this.isExpanded() && !this.state.collapsing;
return classes;
}
};
module.exports = CollapsableMixin;