UNPKG

react-cm-ui

Version:
206 lines (167 loc) 8.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _reactCustomScrollbars = _interopRequireDefault(require("react-custom-scrollbars")); var _constants = require("../../global/constants"); var _domUtils = _interopRequireDefault(require("../../utils/domUtils")); 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 _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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var propTypes = { children: _propTypes["default"].node, className: _propTypes["default"].string, color: _propTypes["default"].oneOf(['blue', 'grey', 'white']), isOpen: _propTypes["default"].bool, /** * Used for DOM testing. https://testing-library.com/docs/queries/bytestid/ */ dataTestId: _propTypes["default"].string, position: _propTypes["default"].oneOf(['left', 'right']), style: _propTypes["default"].shape({}), width: _propTypes["default"].string }; var defaultProps = { children: undefined, className: undefined, color: 'white', dataTestId: "".concat(_constants.UI_CLASS_NAME, "-drawer_wing"), isOpen: undefined, position: 'right', style: {}, width: '100%' }; var hasClassName = 'drawer-has_wing'; var animateOutClassName = 'drawer--wing-animate-out'; var DrawerWing = /*#__PURE__*/function (_React$PureComponent) { _inherits(DrawerWing, _React$PureComponent); var _super = _createSuper(DrawerWing); function DrawerWing() { var _this; _classCallCheck(this, DrawerWing); _this = _super.call(this); _this.state = { isOpen: false }; _this.closestDrawerContainerEl = null; _this.onCloseAnimationComplete = _this.onCloseAnimationComplete.bind(_assertThisInitialized(_this)); return _this; } _createClass(DrawerWing, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this2 = this; var _this$props = this.props, isOpen = _this$props.isOpen, width = _this$props.width; if (!prevProps.isOpen && isOpen) { this.setState({ isOpen: isOpen }, function () { _this2.onOpen(); }); } if (prevProps.isOpen && !isOpen) { this.onBeforeClose(); } if (prevProps.width !== width && prevProps.isOpen && isOpen) { this.setWidth(); } } }, { key: "onBeforeClose", value: function onBeforeClose() { var animationEvent = _domUtils["default"].cssTransitionType(this.closestDrawerContainerEl); this.drawerWingRef.closest('.ui.drawer').classList.add(animateOutClassName); this.closestDrawerContainerEl.style.transform = 'translate3d(0, 0, 0)'; this.closestDrawerContainerEl.addEventListener(animationEvent, this.onCloseAnimationComplete); } }, { key: "onCloseAnimationComplete", value: function onCloseAnimationComplete() { var _this3 = this; this.drawerWingRef.closest('.ui.drawer').classList.remove(hasClassName, animateOutClassName); this.setState({ isOpen: false }, function () { var animationEvent = _domUtils["default"].cssTransitionType(_this3.closestDrawerContainerEl); _this3.closestDrawerContainerEl.removeEventListener(animationEvent, _this3.onCloseAnimationComplete); }); } }, { key: "onOpen", value: function onOpen() { this.drawerWingRef.closest('.ui.drawer').classList.add(hasClassName); this.setWidth(); } }, { key: "setWidth", value: function setWidth() { var _this$props2 = this.props, position = _this$props2.position, width = _this$props2.width; var isPositionLeft = position === 'left'; this.closestDrawerContainerEl = this.drawerWingRef.closest('.drawer-container'); this.closestDrawerContainerEl.style.transform = "\n translate3d(".concat(isPositionLeft ? width : "-".concat(width), ", 0, 0)\n "); } }, { key: "render", value: function render() { var _this4 = this; var _this$props3 = this.props, children = _this$props3.children, className = _this$props3.className, color = _this$props3.color, dataTestId = _this$props3.dataTestId, position = _this$props3.position, style = _this$props3.style, width = _this$props3.width; var isOpen = this.state.isOpen; if (!isOpen) { return false; } var isPositionLeft = position === 'left'; var containerClasses = (0, _classnames["default"])('ui', 'drawer--wing', className, { 'color-blue': color === 'blue', 'color-grey': color === 'grey', 'color-white': !color || color === 'grey', 'left-position': isPositionLeft }); return /*#__PURE__*/_react["default"].createElement("div", { className: containerClasses, "data-testid": dataTestId, ref: function ref(_ref) { _this4.drawerWingRef = _ref; }, style: style }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", { className: "drawer--wing-container", style: { width: width || null } }, /*#__PURE__*/_react["default"].createElement(_reactCustomScrollbars["default"], { autoHide: true }, /*#__PURE__*/_react["default"].createElement("div", { className: "drawer--wing-container-inner" }, children))))); } }]); return DrawerWing; }(_react["default"].PureComponent); DrawerWing.propTypes = propTypes; DrawerWing.defaultProps = defaultProps; var _default = DrawerWing; exports["default"] = _default;