forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapse.js
More file actions
147 lines (129 loc) · 4.21 KB
/
Copy pathCollapse.js
File metadata and controls
147 lines (129 loc) · 4.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Transition from 'react-transition-group/Transition';
import { mapToCssModules, omit, pick, TransitionTimeouts, TransitionPropTypeKeys, TransitionStatuses } from './utils';
const propTypes = {
...Transition.propTypes,
isOpen: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]),
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
className: PropTypes.node,
navbar: PropTypes.bool,
cssModule: PropTypes.object,
};
const defaultProps = {
...Transition.defaultProps,
isOpen: false,
appear: false,
enter: true,
exit: true,
tag: 'div',
timeout: TransitionTimeouts.Collapse,
};
const transitionStatusToClassHash = {
[TransitionStatuses.ENTERING]: 'collapsing',
[TransitionStatuses.ENTERED]: 'collapse show',
[TransitionStatuses.EXITING]: 'collapsing',
[TransitionStatuses.EXITED]: 'collapse',
};
function getTransitionClass(status) {
return transitionStatusToClassHash[status] || 'collapse';
}
function getHeight(node) {
return node.scrollHeight;
}
class Collapse extends Component {
constructor(props) {
super(props);
this.state = {
height: null
};
['onEntering', 'onEntered', 'onExit', 'onExiting', 'onExited'].forEach((name) => {
this[name] = this[name].bind(this);
});
}
onEntering(node, isAppearing) {
this.setState({ height: getHeight(node) });
this.props.onEntering(node, isAppearing);
}
onEntered(node, isAppearing) {
this.setState({ height: null });
this.props.onEntered(node, isAppearing);
}
onExit(node) {
this.setState({ height: getHeight(node) });
this.props.onExit(node);
}
onExiting(node) {
// getting this variable triggers a reflow
const _unused = node.offsetHeight; // eslint-disable-line no-unused-vars
this.setState({ height: 0 });
this.props.onExiting(node);
}
onExited(node) {
this.setState({ height: null });
this.props.onExited(node);
}
render() {
const {
tag: Tag,
isOpen,
className,
navbar,
cssModule,
children,
...otherProps
} = this.props;
const { height } = this.state;
// In NODE_ENV=production the Transition.propTypes are wrapped which results in an
// empty object "{}". This is the result of the `react-transition-group` babel
// configuration settings. Therefore, to ensure that production builds work without
// error, we can either explicitly define keys or use the Transition.defaultProps.
// Using the Transition.defaultProps excludes any required props. Thus, the best
// solution is to explicitly define required props in our utilities and reference these.
// This also gives us more flexibility in the future to remove the prop-types
// dependency in distribution builds (Similar to how `react-transition-group` does).
// Note: Without omitting the `react-transition-group` props, the resulting child
// Tag component would inherit the Transition properties as attributes for the HTML
// element which results in errors/warnings for non-valid attributes.
const transitionProps = pick(otherProps, TransitionPropTypeKeys);
const childProps = omit(otherProps, TransitionPropTypeKeys);
return (
<Transition
{...transitionProps}
in={isOpen}
onEntering={this.onEntering}
onEntered={this.onEntered}
onExit={this.onExit}
onExiting={this.onExiting}
onExited={this.onExited}
>
{(status) => {
let collapseClass = getTransitionClass(status);
const classes = mapToCssModules(classNames(
className,
collapseClass,
navbar && 'navbar-collapse'
), cssModule);
const style = height === null ? null : { height };
return (
<Tag
{...childProps}
style={{ ...childProps.style, ...style }}
className={classes}
>
{children}
</Tag>
);
}}
</Transition>
);
}
}
Collapse.propTypes = propTypes;
Collapse.defaultProps = defaultProps;
export default Collapse;