UNPKG

@choerodon/master

Version:
129 lines (105 loc) 5.61 kB
import "choerodon-ui/lib/tabs/style"; import _Tabs from "choerodon-ui/lib/tabs"; var _excluded = ["children", "noHeader", "className", "cache"]; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 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; } import React, { useState, useEffect } from 'react'; import classNames from 'classnames'; import { withRouter } from 'react-router-dom'; import { observer } from 'mobx-react-lite'; import PageTab from "./PageTab"; import "./style/PageWrap.less"; var TabPane = _Tabs.TabPane; export var Context = /*#__PURE__*/React.createContext({}); var PageWrap = function PageWrap(_ref) { var children = _ref.children, noHeader = _ref.noHeader, className = _ref.className, cache = _ref.cache, props = _objectWithoutProperties(_ref, _excluded); var keyShowArr = React.Children.map(children, function (child) { return { route: child.props.route, tabKey: child.props.tabKey, alwaysShow: child.props.alwaysShow }; }); var keyArr = React.Children.map(children, function (child) { return child.props.tabKey; }); var Children = React.Children.map(children, function (child) { return child; }); var _useState = useState(null), _useState2 = _slicedToArray(_useState, 2), currentKey = _useState2[0], setCurrentKey = _useState2[1]; function loadMenu() { var location = props.location; var pathname = location.pathname; var activeKey; if (new URLSearchParams(window.location.hash.split('?')[1]).get('activeKey')) { activeKey = new URLSearchParams(window.location.hash.split('?')[1]).get('activeKey'); } setCurrentKey(activeKey || "".concat(keyArr[0])); keyShowArr.forEach(function (menu) { if (menu.route === pathname) { setCurrentKey("".concat(menu.tabKey)); } }); } useEffect(function () { loadMenu(); }, [Children.length]); var callback = function callback(key) { if (cache) { setCurrentKey(key); } else { var realCode = key; var realTabNode = keyShowArr.find(function (v) { return v.tabKey === realCode; }); if (realTabNode && realTabNode.route) { props.history.push("".concat(realTabNode.route).concat(props.location.search)); } } if (props.onChange) { props.onChange(key); } }; return /*#__PURE__*/React.createElement(Context.Provider, { value: { isTab: true } }, /*#__PURE__*/React.createElement(_Tabs, { className: classNames('wrap-tabs', { hasHeader: !noHeader.includes(currentKey) }, className), animated: false, onChange: callback, activeKey: currentKey }, Children.map(function (child) { var type = child.type; if (type === PageTab) { if (keyShowArr && keyShowArr.filter(function (v) { return v.route; }).find(function (v) { return v.tabKey === child.props.tabKey; }) || child.props.alwaysShow) { return /*#__PURE__*/React.createElement(TabPane, { tab: child.props.title, key: child.props.tabKey }, cache || currentKey && currentKey.indexOf(child.props.tabKey) > -1 ? /*#__PURE__*/React.createElement(child.props.component, props) : null); } return null; } return child; }))); }; export default withRouter(observer(PageWrap));