UNPKG

polestar-ui-kit

Version:

## Install

338 lines (251 loc) 13.7 kB
"use strict"; 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); } require("core-js/modules/es.array.filter"); require("core-js/modules/es.array.find"); require("core-js/modules/es.array.for-each"); require("core-js/modules/es.array.includes"); require("core-js/modules/es.array.is-array"); require("core-js/modules/es.array.splice"); require("core-js/modules/es.date.to-string"); require("core-js/modules/es.function.name"); require("core-js/modules/es.object.get-prototype-of"); require("core-js/modules/es.object.set-prototype-of"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.reflect.construct"); require("core-js/modules/es.regexp.exec"); require("core-js/modules/es.regexp.to-string"); require("core-js/modules/es.string.includes"); require("core-js/modules/es.string.split"); require("core-js/modules/web.dom-collections.for-each"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.TabsTabPane = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _antd = require("antd"); var _classnames = _interopRequireDefault(require("classnames")); var _uuid = require("uuid"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _extends() { _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; }; return _extends.apply(this, arguments); } 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 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 Tabs = function (_React$Component) { _inherits(Tabs, _React$Component); var _super = _createSuper(Tabs); function Tabs(props) { var _this; _classCallCheck(this, Tabs); _this = _super.call(this, props); _this.onChange = function (activeKey) { _this.setState({ activeKey: activeKey }); if (_this.props.onChange) { _this.props.onChange(activeKey); } }; _this.onEdit = function (targetKey, action) { _this["".concat(action, "Tab")](targetKey); if (_this.props.onEdit) { _this.props.onEdit(targetKey, action); } }; _this.getActiveKey = function () { return _this.state.activeKey; }; _this.getKeys = function () { var _this$props = _this.props, editable = _this$props.editable, editableCard = _this$props.editableCard; var children; var keys = []; if (editable || editableCard) { children = _this.state.children; } else { children = _this.props.children; } if (Array.isArray(children) === true) { children.forEach(function (child) { if (child) { keys.push(child.key); } }); } else { keys.push(children.key); } return keys; }; _this.findKey = function (tabKey) { var keys = _this.getKeys(); var _findkey = keys.find(function (key) { return key === tabKey; }); return _findkey; }; _this.changeTab = function (activeKey) { _this.onChange(activeKey); }; _this.addTab = function (pane, index) { if (_react["default"].isValidElement(pane) && pane.type.name === 'TabPane') { var activeKey = pane.key; var children = _toConsumableArray(_this.state.children); if (typeof index === 'undefined') { children.push(pane); } else { children.splice(index, 0, pane); } _this.setState({ children: children, activeKey: activeKey }); } else { throw new Error('Tab Content must be <Tabs.TabPane />'); } }; _this.removeTab = function (targetKey) { var activeKey = _this.state.activeKey; var lastIndex; _this.state.children.forEach(function (child, i) { if (child.key === targetKey) { lastIndex = i - 1; } }); var children = _this.state.children.filter(function (child) { return child.key !== targetKey; }); if (children.length && activeKey === targetKey) { if (lastIndex >= 0) { activeKey = !children[lastIndex].props.disabled ? children[lastIndex].key : _this.findEnabledTabOnRemove(children, lastIndex); } else { activeKey = !children[0].props.disabled ? children[0].key : undefined; } } _this.setState({ children: children, activeKey: activeKey }); }; _this.findEnabledTabOnRemove = function (children, lastIndex) { var activeKey; for (var i = lastIndex; i >= 0; i--) { if (!children[i].props.disabled) { activeKey = children[i].key; break; } } return activeKey; }; var _children = props.children, defaultActiveKey = props.defaultActiveKey; _this.state = { children: _children, activeKey: defaultActiveKey || _children && _children.length > 0 && _children[0].key }; _this.uuid = (0, _uuid.v4)(); return _this; } _createClass(Tabs, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props2 = this.props, tabWidth = _this$props2.tabWidth, tabHeight = _this$props2.tabHeight; if (typeof tabWidth !== 'undefined' || typeof tabHeight !== 'undefined') { var tabBarDom = document.querySelector(".ps-tabs[data-id=\"".concat(this.uuid, "\"] > .ant-tabs-bar")); if (tabBarDom.classList.contains('ant-tabs-top-bar') || tabBarDom.classList.contains('ant-tabs-bottom-bar')) { var tabContainerDom = document.querySelector(".ps-tabs[data-id=\"".concat(this.uuid, "\"] > .ant-tabs-bar > .ant-tabs-nav-container")); tabContainerDom.style.height = typeof tabHeight !== 'undefined' && "".concat(tabHeight, "px"); } var tabDoms = document.querySelectorAll(".ps-tabs[data-id=\"".concat(this.uuid, "\"] > .ant-tabs-bar .ant-tabs-tab")); if (tabDoms) { tabDoms.forEach(function (tabDom) { tabDom.style.width = typeof tabWidth !== 'undefined' && "".concat(tabWidth, "px"); tabDom.style.height = typeof tabHeight !== 'undefined' && "".concat(tabHeight, "px"); }); } } } }, { key: "render", value: function render() { var _this$props3 = this.props, className = _this$props3.className, editableCard = _this$props3.editableCard, editable = _this$props3.editable, defaultActiveKey = _this$props3.defaultActiveKey, activeKey = _this$props3.activeKey, animated = _this$props3.animated, height = _this$props3.height, children = _this$props3.children, onChange = _this$props3.onChange, onEdit = _this$props3.onEdit, rest = _objectWithoutProperties(_this$props3, ["className", "editableCard", "editable", "defaultActiveKey", "activeKey", "animated", "height", "children", "onChange", "onEdit"]); var type = 'line'; var classNameArr = className && className.split(' '); if (classNameArr && classNameArr.includes('ps-tabs-card')) { type = 'card'; } if (editableCard === true) { type = 'editable-card'; } return _react["default"].createElement(_antd.Tabs, _extends({ "data-id": this.uuid, className: (0, _classnames["default"])('ps-tabs', className), style: { height: height }, activeKey: this.state.activeKey, animated: animated, onChange: this.onChange, onEdit: this.onEdit, type: type }, rest), editable || editableCard ? this.state.children : children); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (typeof props.activeKey !== 'undefined' && props.activeKey !== state.activeKey) { return { activeKey: props.activeKey }; } return null; } }]); return Tabs; }(_react["default"].Component); Tabs.propTypes = { height: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]), editableCard: _propTypes["default"].bool, editable: _propTypes["default"].bool, tabWidth: _propTypes["default"].number, tabHeight: _propTypes["default"].number, onChange: _propTypes["default"].func }; Tabs.defaultProps = { editableCard: false, editable: false, height: '100%', animated: false }; var TabsTabPane = _antd.Tabs.TabPane; exports.TabsTabPane = TabsTabPane; var _default = Tabs; exports["default"] = _default;