UNPKG

@pubsweet/ui

Version:

React component library for use in pubsweet apps

162 lines (117 loc) 6.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _uiToolkit = require("@pubsweet/ui-toolkit"); var _atoms = require("../atoms"); 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); } function _templateObject4() { var data = _taggedTemplateLiteral([""]); _templateObject4 = function _templateObject4() { return data; }; return data; } function _templateObject3() { var data = _taggedTemplateLiteral([""]); _templateObject3 = function _templateObject3() { return data; }; return data; } function _templateObject2() { var data = _taggedTemplateLiteral(["\n border-bottom: ", " ", " ", ";\n padding: ", " 1em;\n"]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n display: flex;\n margin-bottom: calc(", " * 3);\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } // TODO: allow the tab content to be separate from the key var TabsContainer = _styledComponents["default"].div(_templateObject(), (0, _uiToolkit.th)('gridUnit')); var Title = _styledComponents["default"].div(_templateObject2(), (0, _uiToolkit.th)('borderWidth'), (0, _uiToolkit.th)('borderStyle'), (0, _uiToolkit.th)('colorBorder'), (0, _uiToolkit.th)('gridUnit')); var TabContainer = _styledComponents["default"].div.attrs(function (props) { return { 'data-test-id': props['data-test-id'] || 'tab-container' }; })(_templateObject3()); var Content = _styledComponents["default"].div(_templateObject4()); var Tabs = /*#__PURE__*/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.state = { activeKey: props.activeKey || null }; return _this; } _createClass(Tabs, [{ key: "componentDidMount", value: function componentDidMount() { var activeKey = this.props.activeKey; // eslint-disable-next-line react/no-did-mount-set-state this.setState({ activeKey: activeKey }); } }, { key: "setActiveKey", value: function setActiveKey(activeKey) { this.setState({ activeKey: activeKey }); if (typeof this.props.onChange === 'function') { this.props.onChange(activeKey); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, sections = _this$props.sections, title = _this$props.title; var activeKey = this.state.activeKey; return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(TabsContainer, null, title && /*#__PURE__*/_react["default"].createElement(Title, null, title), sections.map(function (_ref) { var key = _ref.key, label = _ref.label; return /*#__PURE__*/_react["default"].createElement(TabContainer, { key: key, onClick: function onClick() { return _this2.setActiveKey(key); } }, /*#__PURE__*/_react["default"].createElement(_atoms.Tab, { active: activeKey === key }, label || key)); })), activeKey && /*#__PURE__*/_react["default"].createElement(Content, null, sections.find(function (section) { return section.key === activeKey; }).content)); } }]); return Tabs; }(_react["default"].Component); var _default = Tabs; exports["default"] = _default;