UNPKG

ukelli-ui

Version:

ukelli-ui 是基于 React 的 UI 库,提供简约和功能齐全的组件,可高度定制的组件接口,灵活的配置,提供给开发者另一种开发思路,也致力于尝试不同的组件使用和开发方向。

257 lines (203 loc) 9.71 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")); var _tooltip = require("../tooltip"); var _icon = require("../icon"); 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 (_Component) { _inherits(Tabs, _Component); /** * 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; } _createClass(Tabs, [{ key: "getActiveIdx", value: function getActiveIdx() { return this.isControlled ? this.props.activeTabIdx : this.state.activeTabIdx; } // componentDidMount() { // this.setDefaultTabIdx(this.props); // } // setDefaultTabIdx(nextProps) { // const {activeTabIdx = 0} = nextProps; // this.setState({ // activeTabIdx: activeTabIdx // }); // } }, { key: "_onChangeTab", value: function _onChangeTab(tabIdx) { var activeTabIdx = this.getActiveIdx(); if (activeTabIdx === tabIdx) return; this.setState({ activeTabIdx: tabIdx }); } }, { key: "onTapTab", value: function onTapTab(tapIdx) { if (!this.isControlled) this._onChangeTab(tapIdx); var onChangeTab = this.props.onChangeTab; (0, _basicHelper.CallFunc)(onChangeTab)(tapIdx); // if(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, closeable = _this$props.closeable, closeTip = _this$props.closeTip, onClose = _this$props.onClose; var activeTabIdx = this.getActiveIdx(); var tabs = []; var tabContents = []; _react["default"].Children.map(children, function (tabChild, idx) { if (!tabChild || typeof tabChild.type !== 'function') return; var isActive = idx === activeTabIdx; var tabKey = tabChild.key; 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, atRight = _tabChild$props.atRight, label = _tabChild$props.label; var _labelClass = 'tab ' + labelClass + (isActive ? ' active' : ''); _labelClass += atRight ? ' right' : ''; var _tabContent = withContent || !withContent && isActive ? _react["default"].createElement("div", { className: "tab-content " + contentClass + (isActive ? '' : ' hide'), key: tabKey || "tab-con-" + idx, style: height ? { height: height } : {} }, tabChild.props.children) : null; if (!inRow || withContent) tabContents.push(_tabContent); var _con = inRow ? _tabContent : null; var _tab = _react["default"].createElement("div", { key: tabKey || label, className: _labelClass, draggable: true }, _react["default"].createElement("span", { onClick: function onClick(e) { return _this2.onTapTab(idx); } }, tabChild), closeable && _react["default"].createElement(_tooltip.ToolTip, { className: "_close-btn", title: closeTip, clickToClose: true, onClick: function onClick(e) { return onClose(idx); } }, "x"), _con); tabs.push(_tab); }); this.tabs = tabs; this.tabContents = tabContents; 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, onlyContent = _this$props2.onlyContent; 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' : '')) }, !onlyContent && _react["default"].createElement("div", { className: "tab-group", droppable: "true", onDragEnd: function onDragEnd(e) { console.log(e); } }, tabs), inRow ? null : tabContents); } }]); return Tabs; }(_react.Component); exports["default"] = Tabs; _defineProperty(Tabs, "Tab", _tab2["default"]); _defineProperty(Tabs, "propTypes", { /** tab 内容与 tab 标签是否在同一行 */ inRow: _propTypes["default"].bool, /** 是否只渲染 content */ onlyContent: _propTypes["default"].bool, /** tab 内容与 tab 标签是否共存 */ withContent: _propTypes["default"].bool, /** tab 可否关闭 */ closeable: _propTypes["default"].bool, /** 关闭组件的提示 */ closeTip: _propTypes["default"].any, /** 是否启用 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, onlyContent: false, closeable: false });