UNPKG

react-bootify

Version:
257 lines (213 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _Transition = _interopRequireDefault(require("react-transition-group/Transition")); var _utils = require("../utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function debug() {//console.log.apply(null, arguments); } var Collapse = /*#__PURE__*/ function (_React$Component) { _inherits(Collapse, _React$Component); function Collapse(props) { var _this; _classCallCheck(this, Collapse); _this = _possibleConstructorReturn(this, _getPrototypeOf(Collapse).call(this, props)); _this.state = {}; _this.state.height = null; _this.state.opacity = null; _this.onEntering = _this.onEntering.bind(_assertThisInitialized(_this)); _this.onEntered = _this.onEntered.bind(_assertThisInitialized(_this)); _this.onExiting = _this.onExiting.bind(_assertThisInitialized(_this)); _this.onExited = _this.onExited.bind(_assertThisInitialized(_this)); _this.onExit = _this.onExit.bind(_assertThisInitialized(_this)); _this.onEnter = _this.onEnter.bind(_assertThisInitialized(_this)); return _this; } _createClass(Collapse, [{ key: "onEnter", value: function onEnter(node) { debug('onEnter state'); this.setState({ height: 0, opacity: 0 }); } }, { key: "onEntering", value: function onEntering(node, isAppearing) { debug('onEntering'); this.setState({ height: node.scrollHeight, opacity: 1 }); } }, { key: "onEntered", value: function onEntered(node, isAppearing) { debug('onEntered'); } }, { key: "onExit", value: function onExit(node) { debug('onExit'); } }, { key: "onExiting", value: function onExiting(node) { debug('onExiting'); // Getting this variable triggers a reflow var _unused = node.offsetHeight; // eslint-disable-line no-unused-vars this.setState({ height: 0, opacity: 0 }); } }, { key: "onExited", value: function onExited(node) { debug('onExited'); this.setState({ height: null, opacity: null }); } }, { key: "collapsingStyle", value: function collapsingStyle(style) { style = _objectSpread({}, style, {}, { position: 'ralative', overflow: 'hidden' }); if (this.props.fade) { style = _objectSpread({}, style, {}, { transition: "height ".concat(timeout / 1000, "s ease, opacity ").concat(timeout / 1000, "s ease") }); style = _objectSpread({}, style, {}, { height: this.state.height, opacity: this.state.opacity }); } else { style = _objectSpread({}, style, {}, { transition: "height ".concat(timeout / 1000, "s ease") }); style = _objectSpread({}, style, {}, { height: this.state.height }); } return style; } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, show = _this$props.show, fade = _this$props.fade, timeout = _this$props.timeout, children = _this$props.children, other = _objectWithoutProperties(_this$props, ["show", "fade", "timeout", "children"]); var collapsingStyle = function collapsingStyle() { var style = {}; style = _objectSpread({}, style, {}, { position: 'ralative', overflow: 'hidden' }); if (_this2.props.fade) { style = _objectSpread({}, style, {}, { transition: "height ".concat(timeout / 1000, "s ease, opacity ").concat(timeout / 1000, "s ease") }); style = _objectSpread({}, style, {}, { height: _this2.state.height, opacity: _this2.state.opacity }); } else { style = _objectSpread({}, style, {}, { transition: "height ".concat(timeout / 1000, "s ease") }); style = _objectSpread({}, style, {}, { height: _this2.state.height }); } return style; }; return _react["default"].createElement(_Transition["default"], { "in": show, timeout: timeout, onEnter: this.onEnter, onEntering: this.onEntering, onEntered: this.onEntered, onExit: this.onExit, onExiting: this.onExiting, onExited: this.onExited }, function (state) { var child = _react["default"].Children.toArray(children); if ((0, _utils.isArray)(child)) child = child[0]; var style = child.props.style || {}; switch (state) { case 'entering': { style = _objectSpread({}, style, {}, collapsingStyle()); break; } ; case 'entered': { break; } ; case 'exiting': { style = _objectSpread({}, style, {}, collapsingStyle()); break; } ; case 'exited': { style = _objectSpread({}, style, {}, { display: 'none' }); break; } ; } debug('Style for state', state, style); return _react["default"].cloneElement(child, _objectSpread({ style: style }, other)); }); } }]); return Collapse; }(_react["default"].Component); exports["default"] = Collapse; ; Collapse.propTypes = { show: _propTypes["default"].bool, fade: _propTypes["default"].bool, timeout: _propTypes["default"].number }; Collapse.defaultProps = { timeout: 350, fade: false, show: false };