"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _classnames2 = _interopRequireDefault(require("classnames")); var _KeyCode = _interopRequireDefault(require("./KeyCode")); var _TabPane = _interopRequireDefault(require("./TabPane")); var _utils = require("./utils"); 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 _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 _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; } 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 _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 noop() {} function getDefaultActiveKey(props) { var activeKey; _react.default.Children.forEach(props.children, function (child) { if (child && !activeKey && !child.props.disabled) { activeKey = child.key; } }); return activeKey; } function activeKeyIsValid(props, key) { var keys = _react.default.Children.map(props.children, function (child) { return child && child.key; }); return keys.indexOf(key) >= 0; } var Tabs = /*#__PURE__*/function (_React$Component) { _inherits(Tabs, _React$Component); function Tabs(props) { var _this; _classCallCheck(this, Tabs); _this = _possibleConstructorReturn(this, _getPrototypeOf(Tabs).call(this, props)); _this.onTabClick = function (activeKey, e) { if (_this.tabBar.props.onTabClick) { _this.tabBar.props.onTabClick(activeKey, e); } _this.setActiveKey(activeKey); }; _this.onNavKeyDown = function (e) { var keyboard = _this.props.keyboard; if (!keyboard) { return; } var eventKeyCode = e.keyCode; if (eventKeyCode === _KeyCode.default.RIGHT || eventKeyCode === _KeyCode.default.DOWN) { e.preventDefault(); var nextKey = _this.getNextActiveKey(true); _this.onTabClick(nextKey); } else if (eventKeyCode === _KeyCode.default.LEFT || eventKeyCode === _KeyCode.default.UP) { e.preventDefault(); var previousKey = _this.getNextActiveKey(false); _this.onTabClick(previousKey); } }; _this.onScroll = function (_ref) { var target = _ref.target, currentTarget = _ref.currentTarget; if (target === currentTarget && target.scrollLeft > 0) { target.scrollLeft = 0; } }; _this.setActiveKey = function (activeKey) { if (_this.state.activeKey !== activeKey) { if (!('activeKey' in _this.props)) { _this.setState({ activeKey: activeKey }); } _this.props.onChange(activeKey); } }; _this.getNextActiveKey = function (next) { var activeKey = _this.state.activeKey; var children = []; _react.default.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 && children[0].key; children.forEach(function (child, i) { if (child.key === activeKey) { if (i === length - 1) { ret = children[0].key; } else { ret = children[i + 1].key; } } }); 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: "componentWillUnmount", value: function componentWillUnmount() { this.destroy = true; } }, { key: "render", value: function render() { var _classnames; var props = this.props; var prefixCls = props.prefixCls, navWrapper = props.navWrapper, tabBarPosition = props.tabBarPosition, className = props.className, renderTabContent = props.renderTabContent, renderTabBar = props.renderTabBar, destroyInactiveTabPane = props.destroyInactiveTabPane, direction = props.direction, id = props.id, restProps = _objectWithoutProperties(props, ["prefixCls", "navWrapper", "tabBarPosition", "className", "renderTabContent", "renderTabBar", "destroyInactiveTabPane", "direction", "id"]); var cls = (0, _classnames2.default)((_classnames = {}, _defineProperty(_classnames, prefixCls, 1), _defineProperty(_classnames, "".concat(prefixCls, "-").concat(tabBarPosition), 1), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classnames)); this.tabBar = renderTabBar(); var tabBar = _react.default.cloneElement(this.tabBar, { prefixCls: prefixCls, navWrapper: navWrapper, key: 'tabBar', onKeyDown: this.onNavKeyDown, tabBarPosition: tabBarPosition, onTabClick: this.onTabClick, panels: props.children, activeKey: this.state.activeKey, direction: this.props.direction, id: id }); var tabContent = _react.default.cloneElement(renderTabContent(), { prefixCls: prefixCls, tabBarPosition: tabBarPosition, activeKey: this.state.activeKey, destroyInactiveTabPane: destroyInactiveTabPane, children: props.children, onChange: this.setActiveKey, key: 'tabContent', direction: this.props.direction, id: id }); var contents = []; if (tabBarPosition === 'bottom') { contents.push(tabContent, tabBar); } else { contents.push(tabBar, tabContent); } return _react.default.createElement("div", _extends({ className: cls, style: props.style }, (0, _utils.getDataAttr)(restProps), { onScroll: this.onScroll, id: id }), contents); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { var newState = {}; if ('activeKey' in props) { newState.activeKey = props.activeKey; } else if (!activeKeyIsValid(props, state.activeKey)) { newState.activeKey = getDefaultActiveKey(props); } if (Object.keys(newState).length > 0) { return newState; } return null; } }]); return Tabs; }(_react.default.Component); Tabs.defaultProps = { prefixCls: 'rc-tabs', destroyInactiveTabPane: false, onChange: noop, keyboard: true, navWrapper: function navWrapper(arg) { return arg; }, tabBarPosition: 'top', children: null, style: {}, direction: 'ltr' }; Tabs.TabPane = _TabPane.default; var _default = Tabs; exports.default = _default;