| 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 |
120x
120x
120x
124x
35x
34x
41x
35x
35x
25x
3x
3x
3x
3x
3x
38x
38x
38x
34x
| import _ from 'lodash';
import React from 'react';
import { Motion, spring } from 'react-motion';
import { QUICK_SLIDE_MOTION } from '../../constants/motion-spring';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
const cx = lucidClassNames.bind('&-Collapsible');
const {
any,
bool,
node,
number,
string,
} = React.PropTypes;
/**
* {"categories": ["utility"]}
*
* This is a simple container that can render content as expanded or collapsed.
*/
const Collapsible = createClass({
displayName: 'Collapsible',
_isPrivate: true,
propTypes: {
/**
* Expandable content.
*/
children: node,
/**
* Appended to the component-specific class names set on the root
* element.
*/
className: string,
/**
* Indicates that the component is in the "expanded" state when true
* and in the "unexpanded" state when false.
*/
isExpanded: bool,
/**
* Show an animated transition for alternating values of `isExpanded`.
*/
isAnimated: bool,
/**
* If true, do not render children when fully collapsed.
*/
isMountControlled: bool,
/**
* If `isMountControlled` is true, this value sets is the minimum height
* the container needs to reach to not render any children.
*/
mountControlThreshold: number,
/**
* Pass in a custom root element type.
*/
rootType: any,
},
getDefaultProps() {
return {
isExpanded: true,
isAnimated: true,
isMountControlled: true,
mountControlThreshold: 4,
rootType: 'div',
};
},
getInitialState() {
return {
maxHeight: null,
};
},
storeRef(name) {
return (ref) => {
this.Refs[name] = ref;
};
},
componentWillMount() {
this.Refs = {};
this.isAnimated = false;
},
componentDidMount() {
_.delay(() => {
this.setState({
maxHeight: _.get(this.Refs, 'root.scrollHeight'),
});
this.isAnimated = this.props.isAnimated;
}, 32);
},
componentDidUpdate() {
Eif (this.isMounted()) {
this.isAnimated = false;
_.delay(() => {
if (this.props.isExpanded) {
const maxHeight = _.get(this.Refs, 'root.scrollHeight');
if (maxHeight !== this.state.maxHeight) {
this.setState({
maxHeight,
});
}
}
this.isAnimated = this.props.isAnimated;
}, 32);
}
},
render() {
const {
children,
className,
isExpanded,
isMountControlled,
mountControlThreshold,
rootType,
...passThroughs,
} = this.props;
const {
maxHeight,
} = this.state;
return (
<Motion
style={this.isAnimated ? {
height: (isExpanded && !_.isNull(maxHeight) ? spring(maxHeight, QUICK_SLIDE_MOTION) : spring(0, QUICK_SLIDE_MOTION)),
} : {
height: (isExpanded && !_.isNull(maxHeight) ? maxHeight : 0),
}}
>
{tween => (
React.createElement(rootType, {
...omitProps(passThroughs, Collapsible),
ref: this.storeRef('root'),
className: cx('&', className),
style: {
height: (tween.height !== maxHeight ? (
tween.height < 0 ? 0 : tween.height
) : null),
overflow: 'hidden',
padding: 0,
...passThroughs.style,
},
}, [
(<div key='content' className={cx('&-content')} style={{ margin: 0 }}>
{isMountControlled && !isExpanded ? (
_.isNull(maxHeight) || Math.abs(tween.height) > mountControlThreshold ? children : null
) : children}
</div>),
])
)}
</Motion>
);
},
});
export default Collapsible;
|