import React, { CSSProperties } from "react"; import { Dropdown, Menu, MenuItemProps, MenuProps } from "antd"; import { BlockMenu, Level2Menu } from "../../tmpl/interface"; import MenuRender from "./menu-render"; import { LocaleContext } from "../../locale/LocaleProvider"; import "./block.css"; import { NavLink } from "react-router-dom"; import { inherits } from "util"; export interface BlockProps { blocks: BlockMenu[]; currentBlockId?: string; currentL2MenuId?: string; activeColor?: string; } interface BlockState {} class Block extends React.PureComponent { static contextType = LocaleContext; context: React.ContextType; constructor(props) { super(props); } getSubMenu = (blockMenu: BlockMenu) => { return MenuRender.getMenu(blockMenu, null); }; findBlockId = (blocks: BlockMenu[], currentL2MenuId: string) => { let blockId: string = undefined; for (const block of blocks) { if (block.l1Menus) { for (const menul1 of block.l1Menus) { for (const menul2 of menul1.l2Menus) { if (menul2.id === currentL2MenuId) { blockId = block.id; return blockId; } } } } else { for (const menul1 of block.items) { for (const menul2 of menul1.items) { if (menul2.id === currentL2MenuId) { blockId = block.id; return blockId; } } } } } return blockId; }; get computedBlocks() { const { translate } = this.context; const blocks: BlockMenu[] = JSON.parse(JSON.stringify(this.props.blocks)); blocks && Array.isArray(blocks) && blocks.forEach((block) => { block.title = translate("${" + block.title + "}"); const dropdownMenu = block.items; dropdownMenu && Array.isArray(dropdownMenu) && dropdownMenu.forEach((menu) => { menu.title = translate("${" + menu.title + "}"); }); }); return blocks; } get menuItems() { const blocks = this.computedBlocks; const items: MenuProps["items"] = []; console.log(items); blocks.forEach((block) => { const item: typeof items[0] = { className: "menu-item", key: block.id, children: this.getSubMenu(block), }; item.label = ( {block.title} ); items.push(item); }); return items; } getDefaultIndexPath(menuItem) { if (menuItem.items) return this.getDefaultIndexPath(menuItem.items[0]); return menuItem.path; } render() { const { currentBlockId, blocks, currentL2MenuId, activeColor } = this.props; const syyy = { "--header-block-active-bottom-color": activeColor ? activeColor : "#1E90FF", "--header-top-active-background": activeColor ? activeColor : "#1E90FF", } as CSSProperties; if (!blocks || blocks.length < 1) { return <>; } let blockId = currentBlockId; if (currentL2MenuId && !blockId) { blockId = this.findBlockId(blocks, currentL2MenuId); } if (!blockId) { blockId = blocks.at(0).id; } // return this.computedBlocks.map((item, index) => { // return ( //
// // // {item.title} // // //
// ); // }); return ( ); } } export default Block;