UNPKG

@grc/business

Version:

更新sider选项 : forceSubMenuRender

142 lines (141 loc) 6.07 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { PureComponent } from "react"; import { Link } from "react-router-dom"; import { ConfigConsumer } from "@grc/base/components/config-provider"; import { Menu } from "@grc/base/components/menu"; import { Icon } from "@grc/base/components/icon"; var MenuComponent = /** @class */ (function (_super) { __extends(MenuComponent, _super); function MenuComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.prefixCls = ""; return _this; } /** * icon */ MenuComponent.prototype.getIcon = function (icon) { if (typeof icon === 'string' && /^\.*?\//.test(icon)) return (React.createElement("img", { src: icon, alt: "icon" })); if (typeof icon === 'string') return React.createElement(Icon, { type: icon }); return icon; }; /** * 获取当前路径 * @param 是否hashHistory */ MenuComponent.prototype.getCurrentPath = function (isHashHistory) { if (isHashHistory === void 0) { isHashHistory = true; } if (!isHashHistory) return location.pathname; return location.hash .replace(/^\#+/, "") .replace(/\?.+/, ""); }; /** * 获取选中的key值 */ MenuComponent.prototype.getSelectKeys = function (pathname, menuItems, selectedKeys) { if (selectedKeys === void 0) { selectedKeys = []; } if (!pathname || !menuItems) return selectedKeys; for (var i = 0; i < menuItems.length; i++) { var _a = menuItems[i], hidden = _a.hidden, text = _a.text, path = _a.path, key = _a.key, children = _a.children, isActive = _a.isActive; if (hidden || !text || selectedKeys.includes(key)) continue; if ((pathname === path) || (isActive && isActive(pathname))) selectedKeys.push(key); if (children && !!children.length) this.getSelectKeys(pathname, children, selectedKeys); } return selectedKeys; }; /** * 获得菜单子节点 */ MenuComponent.prototype.getNavMenuItems = function (menuItems) { var _this = this; if (!menuItems) return []; return menuItems .filter(function (item) { return item.text && !item.hidden; }) .map(function (item) { return _this.getSubMenuOrItem(item); }) .filter(function (item) { return item; }); }; ; MenuComponent.prototype.getSubMenuOrItem = function (item) { if (item.children && !item.hidden && item.children.some(function (child) { return child.text; })) return (React.createElement(Menu.SubMenu, { key: item.key, title: item.icon ? (React.createElement(React.Fragment, null, this.getIcon(item.icon), React.createElement("span", null, item.text))) : item.text }, this.getNavMenuItems(item.children))); return (React.createElement(Menu.Item, { key: item.key }, this.getMenuItemPath(item))); }; /** * 判断是否是http链接.返回 Link 或 a */ MenuComponent.prototype.getMenuItemPath = function (item) { var text = item.text, path = item.path, target = item.target, isActive = item.isActive; var icon = this.getIcon(item.icon); if (!path) return (React.createElement("span", null, text)); if (/^https?:\/\//.test(path)) { return (React.createElement("a", { href: path, className: "" }, icon, React.createElement("span", null, text))); } return (React.createElement(Link, { to: path, target: target }, icon, React.createElement("span", null, text))); }; ; MenuComponent.prototype.render = function () { var _this = this; var _a = this.props, prefixCls = _a.prefixCls, menuItems = _a.menuItems, rest = __rest(_a, ["prefixCls", "menuItems"]); var pathname = this.getCurrentPath(); var selectedKeys = this.getSelectKeys(pathname, menuItems); return (React.createElement(ConfigConsumer, null, function (config) { var getPrefixCls = config.getPrefixCls; _this.prefixCls = prefixCls ? prefixCls : getPrefixCls("menu", "", true); return (React.createElement(Menu, __assign({ selectedKeys: selectedKeys, className: _this.prefixCls }, rest), _this.getNavMenuItems(menuItems))); })); }; return MenuComponent; }(PureComponent)); export default MenuComponent;