1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 |
|
7 | var _collapseStyles;
|
8 |
|
9 | import classNames from 'classnames';
|
10 | import css from 'dom-helpers/style';
|
11 | import React from 'react';
|
12 | import PropTypes from 'prop-types';
|
13 | import Transition, { EXITED, ENTERED, ENTERING, EXITING } from 'react-transition-group/Transition';
|
14 |
|
15 | import capitalize from './utils/capitalize';
|
16 | import createChainedFunction from './utils/createChainedFunction';
|
17 |
|
18 | var MARGINS = {
|
19 | height: ['marginTop', 'marginBottom'],
|
20 | width: ['marginLeft', 'marginRight']
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 | function triggerBrowserReflow(node) {
|
26 | node.offsetHeight;
|
27 | }
|
28 |
|
29 | function getDimensionValue(dimension, elem) {
|
30 | var value = elem['offset' + capitalize(dimension)];
|
31 | var margins = MARGINS[dimension];
|
32 |
|
33 | return value + parseInt(css(elem, margins[0]), 10) + parseInt(css(elem, margins[1]), 10);
|
34 | }
|
35 |
|
36 | var collapseStyles = (_collapseStyles = {}, _collapseStyles[EXITED] = 'collapse', _collapseStyles[EXITING] = 'collapsing', _collapseStyles[ENTERING] = 'collapsing', _collapseStyles[ENTERED] = 'collapse in', _collapseStyles);
|
37 |
|
38 | var propTypes = {
|
39 | |
40 |
|
41 |
|
42 | in: PropTypes.bool,
|
43 |
|
44 | |
45 |
|
46 |
|
47 | mountOnEnter: PropTypes.bool,
|
48 |
|
49 | |
50 |
|
51 |
|
52 | unmountOnExit: PropTypes.bool,
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | appear: PropTypes.bool,
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | timeout: PropTypes.number,
|
66 |
|
67 | |
68 |
|
69 |
|
70 | onEnter: PropTypes.func,
|
71 | |
72 |
|
73 |
|
74 | onEntering: PropTypes.func,
|
75 | |
76 |
|
77 |
|
78 | onEntered: PropTypes.func,
|
79 | |
80 |
|
81 |
|
82 | onExit: PropTypes.func,
|
83 | |
84 |
|
85 |
|
86 | onExiting: PropTypes.func,
|
87 | |
88 |
|
89 |
|
90 | onExited: PropTypes.func,
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | dimension: PropTypes.oneOfType([PropTypes.oneOf(['height', 'width']), PropTypes.func]),
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | getDimensionValue: PropTypes.func,
|
109 |
|
110 | |
111 |
|
112 |
|
113 | role: PropTypes.string
|
114 | };
|
115 |
|
116 | var defaultProps = {
|
117 | in: false,
|
118 | timeout: 300,
|
119 | mountOnEnter: false,
|
120 | unmountOnExit: false,
|
121 | appear: false,
|
122 |
|
123 | dimension: 'height',
|
124 | getDimensionValue: getDimensionValue
|
125 | };
|
126 |
|
127 | var Collapse = function (_React$Component) {
|
128 | _inherits(Collapse, _React$Component);
|
129 |
|
130 | function Collapse() {
|
131 | var _temp, _this, _ret;
|
132 |
|
133 | _classCallCheck(this, Collapse);
|
134 |
|
135 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
136 | args[_key] = arguments[_key];
|
137 | }
|
138 |
|
139 | return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleEnter = function (elem) {
|
140 | elem.style[_this.getDimension()] = '0';
|
141 | }, _this.handleEntering = function (elem) {
|
142 | var dimension = _this.getDimension();
|
143 | elem.style[dimension] = _this._getScrollDimensionValue(elem, dimension);
|
144 | }, _this.handleEntered = function (elem) {
|
145 | elem.style[_this.getDimension()] = null;
|
146 | }, _this.handleExit = function (elem) {
|
147 | var dimension = _this.getDimension();
|
148 | elem.style[dimension] = _this.props.getDimensionValue(dimension, elem) + 'px';
|
149 | triggerBrowserReflow(elem);
|
150 | }, _this.handleExiting = function (elem) {
|
151 | elem.style[_this.getDimension()] = '0';
|
152 | }, _temp), _possibleConstructorReturn(_this, _ret);
|
153 | }
|
154 |
|
155 | Collapse.prototype.getDimension = function getDimension() {
|
156 | return typeof this.props.dimension === 'function' ? this.props.dimension() : this.props.dimension;
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | Collapse.prototype._getScrollDimensionValue = function _getScrollDimensionValue(elem, dimension) {
|
163 | return elem['scroll' + capitalize(dimension)] + 'px';
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | Collapse.prototype.render = function render() {
|
173 | var _this2 = this;
|
174 |
|
175 | var _props = this.props,
|
176 | onEnter = _props.onEnter,
|
177 | onEntering = _props.onEntering,
|
178 | onEntered = _props.onEntered,
|
179 | onExit = _props.onExit,
|
180 | onExiting = _props.onExiting,
|
181 | className = _props.className,
|
182 | children = _props.children,
|
183 | props = _objectWithoutProperties(_props, ['onEnter', 'onEntering', 'onEntered', 'onExit', 'onExiting', 'className', 'children']);
|
184 |
|
185 | delete props.dimension;
|
186 | delete props.getDimensionValue;
|
187 |
|
188 | var handleEnter = createChainedFunction(this.handleEnter, onEnter);
|
189 | var handleEntering = createChainedFunction(this.handleEntering, onEntering);
|
190 | var handleEntered = createChainedFunction(this.handleEntered, onEntered);
|
191 | var handleExit = createChainedFunction(this.handleExit, onExit);
|
192 | var handleExiting = createChainedFunction(this.handleExiting, onExiting);
|
193 |
|
194 | return React.createElement(
|
195 | Transition,
|
196 | _extends({}, props, {
|
197 | 'aria-expanded': props.role ? props.in : null,
|
198 | onEnter: handleEnter,
|
199 | onEntering: handleEntering,
|
200 | onEntered: handleEntered,
|
201 | onExit: handleExit,
|
202 | onExiting: handleExiting
|
203 | }),
|
204 | function (state, innerProps) {
|
205 | return React.cloneElement(children, _extends({}, innerProps, {
|
206 | className: classNames(className, children.props.className, collapseStyles[state], _this2.getDimension() === 'width' && 'width')
|
207 | }));
|
208 | }
|
209 | );
|
210 | };
|
211 |
|
212 | return Collapse;
|
213 | }(React.Component);
|
214 |
|
215 | Collapse.propTypes = propTypes;
|
216 | Collapse.defaultProps = defaultProps;
|
217 |
|
218 | export default Collapse; |
\ | No newline at end of file |