forked from react-bootstrap/react-bootstrap-bower
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapsableMixin.js
More file actions
132 lines (107 loc) · 3.21 KB
/
CollapsableMixin.js
File metadata and controls
132 lines (107 loc) · 3.21 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
122
123
124
125
126
127
128
129
130
131
132
define(function (require, exports, module) {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();
if (node && nextProps.expanded !== this.props.expanded && this.props.expanded) {
node.style[dimension] = this.getCollapsableDimensionValue() + 'px';
}
},
componentDidUpdate: function (prevProps, prevState) {
if (this.state.collapsing !== prevState.collapsing) {
this._afterRender();
}
},
_afterRender: function () {
if (!this.props.collapsable) {
return;
}
this._addEndTransitionListener();
setTimeout(this._updateDimensionAfterRender, 0);
},
_updateDimensionAfterRender: function () {
var dimension = (typeof this.getCollapsableDimension === 'function') ?
this.getCollapsableDimension() : 'height';
var node = this.getCollapsableDOMNode();
if (node) {
if(this.isExpanded() && !this.state.collapsing) {
node.style[dimension] = 'auto';
} else {
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;
});