import React from "react"; import { Menu } from "antd"; import { Menu2layersProps } from "../../tmpl/interface"; import MenuRender from "./menu-render"; import { LocaleContext } from "../../locale/LocaleProvider"; interface Menu2layersState { openKeys?: string[]; } class Menu2layers extends React.PureComponent< Menu2layersProps, Menu2layersState > { static contextType = LocaleContext; context: React.ContextType; constructor(props) { super(props); this.state = { openKeys: [], }; } componentDidMount() { const { currentL2MenuId, blockMenu, collapsed } = this.props; const { openKeys } = this.state; let openKey = blockMenu ? MenuRender.findOpenId(blockMenu, currentL2MenuId) : undefined; if (openKey && openKeys.length < 1) { this.setState({ openKeys: [openKey.toString()], }); } } componentDidUpdate(prevProps) { const { currentL2MenuId, blockMenu, collapsed } = this.props; let { currentL2MenuId: preMenuId, blockMenu: preBlockMenu } = prevProps; const { openKeys } = this.state; if (!preBlockMenu || !blockMenu || blockMenu.id != preBlockMenu.id) { //console.log('currentL2MenuId:',currentL2MenuId); let openKey = blockMenu ? MenuRender.findOpenId(blockMenu, currentL2MenuId) : undefined; if (openKey) { this.setState({ openKeys: [openKey], }); } } else { let openKey = blockMenu ? MenuRender.findOpenId(blockMenu, currentL2MenuId) : undefined; let preOpenKey = blockMenu ? MenuRender.findOpenId(preBlockMenu, preMenuId) : undefined; if ((!preOpenKey && openKey) || (openKey && openKey != preOpenKey)) { this.setState({ openKeys: [openKey], }); } } } handleOpen = (openKeys) => { if (openKeys && openKeys.length > 1) { openKeys.splice(0, 1); } this.setState({ openKeys, }); }; get computedBlockMenu() { const { translate } = this.context; const menu = JSON.parse(JSON.stringify(this.props.blockMenu)); menu && Array.isArray(menu.items) && menu.items.forEach((item) => { item.title = translate("${" + item.title + "}"); return item; }); return menu; } render() { const { blockMenu, collapsed, currentL2MenuId, basePath, ...other } = this.props; const { openKeys } = this.state; const items = blockMenu ? MenuRender.getMenu(this.computedBlockMenu, undefined, basePath) : []; other.title = ""; return ( ); } } export default Menu2layers;