UNPKG

7.59 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
10
11var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17var _classnames = require('classnames');
18
19var _classnames2 = _interopRequireDefault(_classnames);
20
21var _lodash = require('lodash.omit');
22
23var _lodash2 = _interopRequireDefault(_lodash);
24
25var _utils = require('./utils');
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
34
35function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
36
37var SHOW = 'SHOW';
38var SHOWN = 'SHOWN';
39var HIDE = 'HIDE';
40var HIDDEN = 'HIDDEN';
41
42var propTypes = {
43 isOpen: _react.PropTypes.bool,
44 className: _react.PropTypes.node,
45 tag: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string]),
46 cssModule: _react.PropTypes.object,
47 navbar: _react.PropTypes.bool,
48 delay: _react.PropTypes.oneOfType([_react.PropTypes.shape({ show: _react.PropTypes.number, hide: _react.PropTypes.number }), _react.PropTypes.number]),
49 onOpened: _react.PropTypes.func,
50 onClosed: _react.PropTypes.func
51};
52
53var DEFAULT_DELAYS = {
54 show: 350,
55 hide: 350
56};
57
58var defaultProps = {
59 isOpen: false,
60 tag: 'div',
61 delay: DEFAULT_DELAYS,
62 onOpened: function onOpened() {},
63 onClosed: function onClosed() {}
64};
65
66var Collapse = function (_Component) {
67 _inherits(Collapse, _Component);
68
69 function Collapse(props) {
70 _classCallCheck(this, Collapse);
71
72 var _this = _possibleConstructorReturn(this, (Collapse.__proto__ || Object.getPrototypeOf(Collapse)).call(this, props));
73
74 _this.state = {
75 collapse: props.isOpen ? SHOWN : HIDDEN,
76 height: null
77 };
78 _this.element = null;
79 return _this;
80 }
81
82 _createClass(Collapse, [{
83 key: 'componentWillReceiveProps',
84 value: function componentWillReceiveProps(nextProps) {
85 var _this2 = this;
86
87 var willOpen = nextProps.isOpen;
88 var collapse = this.state.collapse;
89
90 if (willOpen && collapse === HIDDEN) {
91 // will open
92 this.setState({ collapse: SHOW }, function () {
93 // the height transition will work after class "collapsing" applied
94 _this2.setState({ height: _this2.getHeight() });
95 _this2.transitionTag = setTimeout(function () {
96 _this2.setState({
97 collapse: SHOWN,
98 height: null
99 });
100 }, _this2.getDelay('show'));
101 });
102 } else if (!willOpen && collapse === SHOWN) {
103 // will hide
104 this.setState({ height: this.getHeight() }, function () {
105 _this2.setState({
106 collapse: HIDE,
107 height: _this2.getHeight()
108 }, function () {
109 _this2.setState({ height: 0 });
110 });
111 });
112
113 this.transitionTag = setTimeout(function () {
114 _this2.setState({
115 collapse: HIDDEN,
116 height: null
117 });
118 }, this.getDelay('hide'));
119 }
120 // else: do nothing.
121 }
122 }, {
123 key: 'componentDidUpdate',
124 value: function componentDidUpdate(prevProps, prevState) {
125 if (this.state.collapse === SHOWN && prevState && prevState.collapse !== SHOWN) {
126 this.props.onOpened();
127 }
128
129 if (this.state.collapse === HIDDEN && prevState && prevState.collapse !== HIDDEN) {
130 this.props.onClosed();
131 }
132 }
133 }, {
134 key: 'componentWillUnmount',
135 value: function componentWillUnmount() {
136 clearTimeout(this.transitionTag);
137 }
138 }, {
139 key: 'getDelay',
140 value: function getDelay(key) {
141 var delay = this.props.delay;
142
143 if ((typeof delay === 'undefined' ? 'undefined' : _typeof(delay)) === 'object') {
144 return isNaN(delay[key]) ? DEFAULT_DELAYS[key] : delay[key];
145 }
146 return delay;
147 }
148 }, {
149 key: 'getHeight',
150 value: function getHeight() {
151 return this.element.scrollHeight;
152 }
153 }, {
154 key: 'render',
155 value: function render() {
156 var _this3 = this;
157
158 var _omit = (0, _lodash2.default)(this.props, ['isOpen', 'delay', 'onOpened', 'onClosed']),
159 navbar = _omit.navbar,
160 className = _omit.className,
161 cssModule = _omit.cssModule,
162 Tag = _omit.tag,
163 attributes = _objectWithoutProperties(_omit, ['navbar', 'className', 'cssModule', 'tag']);
164
165 var _state = this.state,
166 collapse = _state.collapse,
167 height = _state.height;
168
169 var collapseClass = void 0;
170 switch (collapse) {
171 case SHOW:
172 collapseClass = 'collapsing';
173 break;
174 case SHOWN:
175 collapseClass = 'collapse show';
176 break;
177 case HIDE:
178 collapseClass = 'collapsing';
179 break;
180 case HIDDEN:
181 collapseClass = 'collapse';
182 break;
183 default:
184 // HIDDEN
185 collapseClass = 'collapse';
186 }
187
188 var classes = (0, _utils.mapToCssModules)((0, _classnames2.default)(className, collapseClass, navbar && 'navbar-collapse'), cssModule);
189 var style = height === null ? null : { height: height };
190 return _react2.default.createElement(Tag, _extends({}, attributes, {
191 style: _extends({}, attributes.style, style),
192 className: classes,
193 ref: function ref(c) {
194 _this3.element = c;
195 }
196 }));
197 }
198 }]);
199
200 return Collapse;
201}(_react.Component);
202
203Collapse.propTypes = propTypes;
204Collapse.defaultProps = defaultProps;
205exports.default = Collapse;
\No newline at end of file