UNPKG

react-cm-ui

Version:
262 lines (214 loc) 12.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _lodash = _interopRequireDefault(require("lodash")); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _detailsWindowPropTypes = require("./detailsWindowPropTypes"); var _constants = require("../../global/constants"); var _infoBar = _interopRequireDefault(require("../infoBar")); var _detailsWindowColumnContainer = _interopRequireDefault(require("./detailsWindowColumnContainer")); 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 _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 _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 = { bleed: _propTypes["default"].bool, className: _propTypes["default"].string, color: _propTypes["default"].oneOf([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), columnProps: _detailsWindowPropTypes.columnPropsPropTypesShape, columns: _propTypes["default"].arrayOf(_detailsWindowPropTypes.columnPropTypesShape).isRequired, data: _propTypes["default"].shape({}).isRequired, /** * Used for DOM testing. https://testing-library.com/docs/queries/bytestid/ */ dataTestId: _propTypes["default"].string, expandableColumns: _propTypes["default"].arrayOf(_detailsWindowPropTypes.columnPropTypesShape), forwardedRef: _propTypes["default"].oneOfType([_propTypes["default"].func, _propTypes["default"].shape({ current: _propTypes["default"].instanceOf(Element) })]), moduleType: _propTypes["default"].string, style: _propTypes["default"].shape({}) }; var defaultProps = { bleed: true, className: undefined, color: undefined, columnProps: undefined, dataTestId: "".concat(_constants.UI_CLASS_NAME, "-details_window"), expandableColumns: undefined, forwardedRef: undefined, moduleType: undefined, style: {} }; var DetailsWindow = /*#__PURE__*/function (_React$PureComponent) { _inherits(DetailsWindow, _React$PureComponent); var _super = _createSuper(DetailsWindow); function DetailsWindow() { var _this; _classCallCheck(this, DetailsWindow); _this = _super.call(this); _this.state = { isExpanded: false }; _this.expandableContainerHeight = null; _this.onExpandButtonToggle = _this.onExpandButtonToggle.bind(_assertThisInitialized(_this)); _this.onExpandTransitionComplete = _this.onExpandTransitionComplete.bind(_assertThisInitialized(_this)); _this.previousExpandableContainerHeight = null; _this.setColumnContainerHeight = _this.setColumnContainerHeight.bind(_assertThisInitialized(_this)); return _this; } _createClass(DetailsWindow, [{ key: "componentDidMount", value: function componentDidMount() { this.toggleExpandableContainer(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var expandableColumns = this.props.expandableColumns; var prevExpandableColumns = prevProps.expandableColumns; if (_lodash["default"].isEmpty(prevExpandableColumns) && !_lodash["default"].isEmpty(expandableColumns)) { this.toggleExpandableContainer(); } } }, { key: "onExpandButtonToggle", value: function onExpandButtonToggle() { var _this2 = this; this.setState(function (prevState) { return { isExpanded: !prevState.isExpanded }; }, function () { _this2.toggleExpandableContainer(); }); } }, { key: "onExpandTransitionComplete", value: function onExpandTransitionComplete() { var moduleType = this.props.moduleType; var bemClassName = "".concat(moduleType, "--details_window"); var exandableContainer = _reactDom["default"].findDOMNode(this.exandableContainerRef); // eslint-disable-line react/no-find-dom-node, max-len exandableContainer.classList.remove("".concat(bemClassName, "_exapndable_columns_container-contracted-active"), "".concat(bemClassName, "_exapndable_columns_container-expanded-active")); exandableContainer.removeEventListener(_domUtils["default"].cssTransitionType(exandableContainer), this.onExpandTransitionComplete); } }, { key: "setColumnContainerHeight", value: function setColumnContainerHeight(height) { if (_lodash["default"].isNil(this.expandableContainerHeight) || this.expandableContainerHeight > height) { this.expandableContainerHeight = height; } } }, { key: "toggleExpandableContainer", value: function toggleExpandableContainer() { var _this$props = this.props, expandableColumns = _this$props.expandableColumns, moduleType = _this$props.moduleType; var bemClassName = "".concat(moduleType, "--details_window"); var isExpanded = this.state.isExpanded; var hasExpandableColumns = !_lodash["default"].isEmpty(expandableColumns); if (hasExpandableColumns && this.exandableContainerRef) { var exandableContainer = _reactDom["default"].findDOMNode(this.exandableContainerRef); // eslint-disable-line react/no-find-dom-node, max-len exandableContainer.style.height = "".concat(isExpanded ? this.expandableContainerHeight : 0, "px"); exandableContainer.classList.add("".concat(bemClassName, "_exapndable_columns_container-").concat(isExpanded ? 'expanded' : 'contracted', "-active")); exandableContainer.addEventListener(_domUtils["default"].cssTransitionType(exandableContainer), this.onExpandTransitionComplete); } } }, { key: "render", value: function render() { var _this3 = this; var _this$props2 = this.props, bleed = _this$props2.bleed, className = _this$props2.className, color = _this$props2.color, columnProps = _this$props2.columnProps, columns = _this$props2.columns, data = _this$props2.data, dataTestId = _this$props2.dataTestId, expandableColumns = _this$props2.expandableColumns, forwardedRef = _this$props2.forwardedRef, style = _this$props2.style, moduleType = _this$props2.moduleType; var isExpanded = this.state.isExpanded; var hasExpandableColumns = !_lodash["default"].isEmpty(expandableColumns); var bemClassName = "".concat(moduleType, "--details_window"); var containerClasses = (0, _classnames["default"])('ui', bemClassName, className, _defineProperty({}, "".concat(bemClassName, "-bleed"), bleed)); var detailsColumnKeyNum = 1; var detailsColumnKeyNumExpanded = 1; var horizontalSpacing; if (columnProps) { horizontalSpacing = columnProps.horizontalSpacing; } var expandableJSX; if (hasExpandableColumns) { var _ClassNames2; var shouldShowExpanded = isExpanded; var expandableColumnsContainerName = "".concat(bemClassName, "_exapndable_columns_container"); var expandableContainerClasses = (0, _classnames["default"])(expandableColumnsContainerName, (_ClassNames2 = {}, _defineProperty(_ClassNames2, "".concat(expandableColumnsContainerName, "-expanded"), shouldShowExpanded), _defineProperty(_ClassNames2, "".concat(expandableColumnsContainerName, "-contracted"), !shouldShowExpanded), _ClassNames2)); expandableJSX = /*#__PURE__*/_react["default"].createElement("div", { className: expandableContainerClasses, ref: function ref(_ref) { _this3.exandableContainerRef = _ref; } }, _lodash["default"].map(expandableColumns, function (column) { detailsColumnKeyNumExpanded += 1; return /*#__PURE__*/_react["default"].createElement(_detailsWindowColumnContainer["default"], { column: column, columnProps: columnProps, data: data, key: "".concat(moduleType, "DetailsColumn-").concat(detailsColumnKeyNumExpanded), moduleType: moduleType, setColumnContainerHeight: _this3.setColumnContainerHeight }); })); } return /*#__PURE__*/_react["default"].createElement("div", { className: containerClasses, "data-testid": dataTestId, ref: forwardedRef, style: style }, /*#__PURE__*/_react["default"].createElement(_infoBar["default"], { color: color }, /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(bemClassName, "_columns_container"), style: { marginLeft: horizontalSpacing ? "-".concat(horizontalSpacing, "px") : null, marginRight: horizontalSpacing ? "-".concat(horizontalSpacing, "px") : null } }, _lodash["default"].map(columns, function (column) { detailsColumnKeyNum += 1; return /*#__PURE__*/_react["default"].createElement(_detailsWindowColumnContainer["default"], { column: column, columnProps: columnProps, data: data, isExpanded: isExpanded, key: "".concat(moduleType, "DetailsColumn-").concat(detailsColumnKeyNum), moduleType: moduleType, onExpandButtonToggle: _this3.onExpandButtonToggle }); })), expandableJSX)); } }]); return DetailsWindow; }(_react["default"].PureComponent); DetailsWindow.propTypes = propTypes; DetailsWindow.defaultProps = defaultProps; var _default = DetailsWindow; exports["default"] = _default;