import { Menu } from 'antd'; const SubMenu = Menu.SubMenu; const ItemMenu = Menu.Item; import cloneDeep from 'lodash/cloneDeep'; class WeaMenuInline extends React.Component { constructor(props) { super(props); const openKeys = this.getOpenKeys(props.defaultSelectedKey, props.datas); // console.log("openKeys1:",openKeys); this.state = { mode: props.mode || 'Inline', datas: props.datas || [], inlineWidth: props.inlineWidth || 197, verticalWidth: props.verticalWidth || 61, fontSize: props.fontSize, openKeys: openKeys || [], selectedKey: props.defaultSelectedKey || '', // 包含SubMenu,只做单选 defaultSelectedKey: props.defaultSelectedKey || '', // 包含SubMenu,只做单选 canClick: true, selectedType: 0, }; } componentWillReceiveProps(nextProps) { // console.log('datas:',this.props.datas,nextProps.datas,this.props.datas.toString() !== nextProps.datas.toString()); // console.log('defaultSelectedKey',this.props.defaultSelectedKey,nextProps.defaultSelectedKey,this.props.defaultSelectedKey !== nextProps.defaultSelectedKey); // console.log('defaultSelectedKey',this.state.defaultSelectedKey,nextProps.defaultSelectedKey,this.state.defaultSelectedKey !== nextProps.defaultSelectedKey); const openKeys = this.getOpenKeys(nextProps.defaultSelectedKey, nextProps.datas); if ( JSON.stringify(this.props.datas) !== JSON.stringify(nextProps.datas) || (this.props.defaultSelectedKey !== nextProps.defaultSelectedKey && this.state.defaultSelectedKey !== nextProps.defaultSelectedKey) ) { // 计算出openKeys let newProps = { datas: nextProps.datas || [], openKeys: openKeys || [], defaultSelectedKey: nextProps.defaultSelectedKey || '', selectedKey: nextProps.defaultSelectedKey || '', }; // if(this.props.defaultSelectedKey !== nextProps.defaultSelectedKey&&this.state.defaultSelectedKey !== nextProps.defaultSelectedKey) // newProps.openKeys = this.getOpenKeys(nextProps.defaultSelectedKey,nextProps.datas); this.setState(newProps); } } render() { const { mode, datas, verticalWidth } = this.state; const { openKeys, selectedType } = this.state; let that = this; return (
); } getOpenKeys(key, datas) { const data = this.getDataByKey(key, datas); const treeKey = data.treeKey; let treeKeyArr = treeKey ? treeKey.split('{$}') : []; // console.log("treeKey:",treeKey," treeKeyArr:",treeKeyArr); return treeKeyArr; } onModeChange() { const { mode } = this.state; if (typeof this.props.onModeChange == 'function') { this.props.onModeChange(mode); } } getDataByKey(key, datas) { let that = this; let obj = ''; for (let i = 0; i < datas.length && obj == ''; i++) { if (datas[i].id == key) { obj = cloneDeep(datas[i]); } if (obj == '' && datas[i].child && datas[i].child.length > 0) { obj = that.getDataByKey(key, datas[i].child); } } return obj; } changeSelectedKey(key, data, type) { if (window.event && window.event.shiftKey) { window.open(data.fullrouteurl || data.url, '_blank'); return; } if (data && data.target != 'mainFrame') { window.open(data.fullrouteurl || data.url, data.target); return; } const { canClick } = this.state; const { clickBlock } = this.props; const that = this; // let d = new Date(); // console.log(canClick,d.getTime()); if (canClick) { this.setState({ canClick: false, selectedKey: key, selectedType: type, }); if (typeof this.props.onSelect == 'function') { this.props.onSelect(key, data, type); } setTimeout(() => { that.setState({ canClick: true }); }, clickBlock || 300); } } changeOpen(openKeys) { this.setState({ openKeys, }); } renderMenuItem(datas, level) { const selectedClassName = 'wea-menu-selected'; let that = this; const { selectedKey, canClick } = this.state; let datasArr = new Array(); for (let i = 0; i < datas.length; i++) { const data = datas[i]; const isSubMenu = data.child && data.child.length > 0; const isSelected = selectedKey == data.id || `verTop_${selectedKey}` == data.id; if (data.isInlineHide !== 'true') { if (isSubMenu) { datasArr.push(