forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollapsableMixin.js
116 lines (93 loc) · 2.77 KB
/
CollapsableMixin.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
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
import ReactTransitionEvents from './react-es6/lib/ReactTransitionEvents';
var CollapsableMixin = {
getInitialState: function() {
return {
isOpen: this.props.defaultOpen != null ? this.props.defaultOpen : null,
isCollapsing: false
};
},
handleTransitionEnd: function () {
this._collapseEnd = true;
this.setState({
isCollapsing: false
});
},
componentWillReceiveProps: function (newProps) {
if (this.props.isCollapsable && newProps.isOpen !== this.props.isOpen) {
this._collapseEnd = false;
this.setState({
isCollapsing: true
});
}
},
_addEndTransitionListener: function () {
var node = this.getCollapsableDOMNode();
if (node) {
ReactTransitionEvents.addEndEventListener(
node,
this.handleTransitionEnd
);
}
},
_removeEndTransitionListener: function () {
var node = this.getCollapsableDOMNode();
if (node) {
ReactTransitionEvents.addEndEventListener(
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();
if (node && nextProps.isOpen !== this.props.isOpen && this.props.isOpen) {
node.style[dimension] = this.getCollapsableDimensionValue() + 'px';
}
},
componentDidUpdate: function () {
this._afterRender();
},
_afterRender: function () {
if (!this.props.isCollapsable) {
return;
}
this._addEndTransitionListener();
setTimeout(this._updateDimensionAfterRender, 0);
},
_updateDimensionAfterRender: function () {
var dimension = (typeof this.getCollapsableDimension === 'function') ?
this.getCollapsableDimension() : 'height';
var node = this.getCollapsableDOMNode();
if (node) {
node.style[dimension] = this.isOpen() ?
this.getCollapsableDimensionValue() + 'px' : '0px';
}
},
isOpen: function () {
return (this.props.isOpen != null) ? this.props.isOpen : this.state.isOpen;
},
getCollapsableClassSet: function (className) {
var classes = {};
if (typeof className === 'string') {
className.split(' ').forEach(function (className) {
if (className) {
classes[className] = true;
}
});
}
classes.collapsing = this.state.isCollapsing;
classes.collapse = !this.state.isCollapsing;
classes['in'] = this.isOpen() && !this.state.isCollapsing;
return classes;
}
};
export default = CollapsableMixin;