UNPKG

ukelli-ui

Version:

[![Build Status](https://travis-ci.org/ukelli/ukelli-ui.svg?branch=master)](https://travis-ci.org/ukelli/ukelli-ui) [![install size](https://packagephobia.now.sh/badge?p=ukelli-ui)](https://packagephobia.now.sh/result?p=ukelli-ui)

233 lines (182 loc) 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); var _tab2 = _interopRequireDefault(require("./tab")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _typeof(obj) { 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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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 _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; } /** * 提供多种不同 Tab 切换方式与模版 * * @export * @class Tabs * @extends {PureComponent} */ var Tabs = /*#__PURE__*/ function (_PureComponent) { _inherits(Tabs, _PureComponent); /** * Tab 的引用 * * @static * @memberof Tabs * @public */ function Tabs(props) { var _this; _classCallCheck(this, Tabs); _this = _possibleConstructorReturn(this, _getPrototypeOf(Tabs).call(this, props)); _this.state = { activeTabIdx: props.activeTabIdx || props.defaultTab || 0 }; _this.isControlled = props.hasOwnProperty('activeTabIdx'); return _this; } // componentDidMount() { // this.setDefaultTabIdx(this.props); // } _createClass(Tabs, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if (this.isControlled && this.props.activeTabIdx !== nextProps.activeTabIdx) { this._onChangeTab(nextProps.activeTabIdx); } } // setDefaultTabIdx(nextProps) { // const {activeTabIdx = 0} = nextProps; // this.setState({ // activeTabIdx: activeTabIdx // }); // } }, { key: "_onChangeTab", value: function _onChangeTab(tabIdx) { if (this.state.activeTabIdx === tabIdx) return; this.setState({ activeTabIdx: tabIdx }); } }, { key: "onTapTab", value: function onTapTab(tapIdx) { if (!this.isControlled) this._onChangeTab(tapIdx); var onChangeTab = this.props.onChangeTab; if ((0, _basicHelper.IsFunc)(onChangeTab)) onChangeTab(tapIdx); } }, { key: "getTabContents", value: function getTabContents() { var _this2 = this; var _this$props = this.props, children = _this$props.children, height = _this$props.height, inRow = _this$props.inRow, withContent = _this$props.withContent, closeabled = _this$props.closeabled, onClose = _this$props.onClose; var activeTabIdx = this.state.activeTabIdx; var tabs = []; var tabContents = []; _react.default.Children.map(children, function (tabChild, idx) { if (!tabChild || typeof tabChild.type !== 'function') return; var isActive = idx === activeTabIdx; var _tabChild$props = tabChild.props, _tabChild$props$conte = _tabChild$props.contentClass, contentClass = _tabChild$props$conte === void 0 ? '' : _tabChild$props$conte, _tabChild$props$label = _tabChild$props.labelClass, labelClass = _tabChild$props$label === void 0 ? '' : _tabChild$props$label; var _labelClass = 'tab ' + labelClass + (isActive ? ' active' : ''); _labelClass += tabChild.props.atRight ? ' right' : ''; var _tabContent = withContent || !withContent && isActive ? _react.default.createElement("div", { className: "tab-content " + contentClass + (isActive ? '' : ' hide'), key: tabChild.key || "tab-con-" + idx, style: height ? { height: height } : {} }, tabChild.props.children || null) : undefined; if (!inRow || withContent) tabContents.push(_tabContent); var _con = inRow ? _tabContent : null; var _tab = _react.default.createElement("div", { key: "tab-" + idx, className: _labelClass }, _react.default.createElement("span", { onClick: function onClick(e) { return _this2.onTapTab(idx); } }, tabChild), closeabled ? _react.default.createElement("span", { className: "close-btn", onClick: function onClick(e) { return onClose(idx); } }, "x") : null, _con); tabs.push(_tab); }); return { tabs: tabs, tabContents: tabContents }; } }, { key: "render", value: function render() { var _this$props2 = this.props, _this$props2$classNam = _this$props2.className, className = _this$props2$classNam === void 0 ? 'tabs-container' : _this$props2$classNam, inRow = _this$props2.inRow, withContent = _this$props2.withContent; var _this$getTabContents = this.getTabContents(), tabs = _this$getTabContents.tabs, tabContents = _this$getTabContents.tabContents; return _react.default.createElement("div", { className: className + (inRow ? ' in-row' : '' + (withContent ? ' common-mode' : '')) }, _react.default.createElement("div", { className: "tab-group" }, tabs), inRow ? null : tabContents); } }]); return Tabs; }(_react.PureComponent); exports.default = Tabs; _defineProperty(Tabs, "Tab", _tab2.default); _defineProperty(Tabs, "propTypes", { /** tab 内容与 tab 标签是否在同一行 */ inRow: _propTypes.default.bool, /** tab 内容与 tab 标签是否共存 */ withContent: _propTypes.default.bool, /** tab 可否关闭 */ closeabled: _propTypes.default.bool, /** 是否启用 step 分步模式 */ stepMode: _propTypes.default.bool, /** children */ children: _propTypes.default.any, /** tab 内容的高度 */ height: _propTypes.default.string, /** 当前激活的 idx,如果设置了,则为受控组件 */ activeTabIdx: _propTypes.default.number, /** 初始化是的默认 tab 位置 */ defaultTab: _propTypes.default.number, /** className */ className: _propTypes.default.string, /** tab 改变时的回调 */ onChangeTab: _propTypes.default.func, /** tab 关闭时的回调 */ onClose: _propTypes.default.func }); _defineProperty(Tabs, "defaultProps", { inRow: false, withContent: false, closeabled: false });