UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

252 lines (212 loc) 7.6 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { 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; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Children, cloneElement, Component } from 'react'; import PropTypes from 'prop-types'; import noop from 'lodash/noop'; import classnames from 'classnames'; import KeyCode from './KeyCode'; import TabPane from './TabPane'; import { generateKey, getDataAttr } from './utils'; function getDefaultActiveKey(props) { var activeKey; Children.forEach(props.children, function (child, index) { if (child && !activeKey && !child.props.disabled) { activeKey = generateKey(child.key, index); } }); return activeKey; } function activeKeyIsValid(props, key) { var keys = Children.map(props.children, function (child, index) { return child && generateKey(child.key, index); }); return keys.indexOf(key) >= 0; } var Tabs = /*#__PURE__*/ function (_Component) { _inherits(Tabs, _Component); var _super = _createSuper(Tabs); function Tabs(props) { var _this; _classCallCheck(this, Tabs); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "onTabClick", function (activeKey) { if (_this.tabBar.props.onTabClick) { _this.tabBar.props.onTabClick(activeKey); } _this.setActiveKey(activeKey); }); _defineProperty(_assertThisInitialized(_this), "onNavKeyDown", function (e) { var eventKeyCode = e.keyCode; if (eventKeyCode === KeyCode.RIGHT || eventKeyCode === KeyCode.DOWN) { e.preventDefault(); var nextKey = _this.getNextActiveKey(true); _this.onTabClick(nextKey); } else if (eventKeyCode === KeyCode.LEFT || eventKeyCode === KeyCode.UP) { e.preventDefault(); var previousKey = _this.getNextActiveKey(false); _this.onTabClick(previousKey); } }); _defineProperty(_assertThisInitialized(_this), "setActiveKey", function (activeKey) { if (_this.state.activeKey !== activeKey) { if (!('activeKey' in _this.props)) { _this.setState({ activeKey: activeKey }); } _this.props.onChange(activeKey); } }); _defineProperty(_assertThisInitialized(_this), "getNextActiveKey", function (next) { var activeKey = _this.state.activeKey; var children = []; Children.forEach(_this.props.children, function (c) { if (c && !c.props.disabled) { if (next) { children.push(c); } else { children.unshift(c); } } }); var length = children.length; var ret = length && generateKey(children[0].key, 0); children.forEach(function (child, i) { if (generateKey(child.key, i) === activeKey) { if (i === length - 1) { ret = generateKey(children[0].key, 0); } else { ret = generateKey(children[i + 1].key, i + 1); } } }); return ret; }); var _activeKey; if ('activeKey' in props) { _activeKey = props.activeKey; } else if ('defaultActiveKey' in props) { _activeKey = props.defaultActiveKey; } else { _activeKey = getDefaultActiveKey(props); } _this.state = { activeKey: _activeKey }; return _this; } _createClass(Tabs, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if ('activeKey' in nextProps) { this.setState({ activeKey: nextProps.activeKey }); } else if (!activeKeyIsValid(nextProps, this.state.activeKey)) { this.setState({ activeKey: getDefaultActiveKey(nextProps) }); } } }, { key: "render", value: function render() { var _classnames; var props = this.props; var prefixCls = props.prefixCls, tabBarPosition = props.tabBarPosition, className = props.className, renderTabContent = props.renderTabContent, renderTabBar = props.renderTabBar, destroyInactiveTabPane = props.destroyInactiveTabPane, keyboard = props.keyboard, restProps = _objectWithoutProperties(props, ["prefixCls", "tabBarPosition", "className", "renderTabContent", "renderTabBar", "destroyInactiveTabPane", "keyboard"]); var cls = classnames((_classnames = {}, _defineProperty(_classnames, prefixCls, 1), _defineProperty(_classnames, "".concat(prefixCls, "-").concat(tabBarPosition), 1), _defineProperty(_classnames, className, !!className), _classnames)); var onKeyDown = keyboard === false ? noop : this.onNavKeyDown; this.tabBar = renderTabBar(); var contents = [cloneElement(this.tabBar, { prefixCls: prefixCls, key: 'tabBar', onKeyDown: onKeyDown, tabBarPosition: tabBarPosition, onTabClick: this.onTabClick, panels: props.children, activeKey: this.state.activeKey }), cloneElement(renderTabContent(), { prefixCls: prefixCls, tabBarPosition: tabBarPosition, activeKey: this.state.activeKey, destroyInactiveTabPane: destroyInactiveTabPane, children: props.children, onChange: this.setActiveKey, key: 'tabContent' })]; if (tabBarPosition === 'bottom') { contents.reverse(); } return React.createElement("div", _extends({ className: cls, style: props.style }, getDataAttr(restProps)), contents); } }]); return Tabs; }(Component); _defineProperty(Tabs, "propTypes", { destroyInactiveTabPane: PropTypes.bool, renderTabBar: PropTypes.func.isRequired, renderTabContent: PropTypes.func.isRequired, onChange: PropTypes.func, children: PropTypes.any, prefixCls: PropTypes.string, className: PropTypes.string, tabBarPosition: PropTypes.string, style: PropTypes.object, activeKey: PropTypes.string, defaultActiveKey: PropTypes.string, keyboard: PropTypes.bool }); _defineProperty(Tabs, "defaultProps", { prefixCls: 'rc-tabs', destroyInactiveTabPane: false, onChange: noop, keyboard: true, tabBarPosition: 'top', style: {} }); _defineProperty(Tabs, "TabPane", TabPane); export { Tabs as default }; //# sourceMappingURL=Tabs.js.map