react根据路由切换页面

import * as React from 'react'
import { Menu } from 'antd'
import * as styles from './Aside.module.less'
import { DoubleRightOutlined,DoubleLeftOutlined } from '@ant-design/icons'

import routes from '../../../router'
import { RouteComponentProps, withRouter } from 'react-router-dom'
import { IMenuInfo } from 'src/types'
import { connect } from 'react-redux'
import actions from 'src/store/modules/common/actions'
// import { MenuGroup } from 'src/router/constant'
import 'src/assets/fonts/icon-font.css'

import { bindActionCreators } from 'redux'

const { SubMenu } = Menu

interface IState {
    pathname:string
}
interface ActionProps{
    actions: typeof actions,
    collapsed:boolean,
    authUrls: any[],
}

type IProps = RouteComponentProps<any>&ActionProps

const menuRoutes = routes.filter(item=>{return !item.hide})

class Aside extends React.Component<IProps, IState> {

    state:IState = {
        pathname:'',
    }

    // 点击菜单路由跳转
    handleClick = (menuItem: IMenuInfo, subMenuItem: IMenuInfo | undefined) => {
        const path = subMenuItem ? menuItem.path + subMenuItem.path : menuItem.path
        const { history } = this.props
        history.push({ pathname: path })
    }

    // 获取当前路径匹配的菜单
    getSelectedKey = (pathname: string): string[] => {
        const selectedParentNode = menuRoutes.find((item: IMenuInfo) => {
            let menuNode
            if (item.children) {
                // tslint:disable-next-line: no-unused-expression
                menuNode = (item.children && item.children.filter((subItem: IMenuInfo) => {
                    return item.path + subItem.path === pathname
                })) || []
                return menuNode.length > 0
            } else {
                return item.path === pathname
            }
        }) || menuRoutes[0]
        return [selectedParentNode.path]
    }

    toggleCollapsed = () => {
        this.props.actions.toggleMenuCollapsed()
    }

    unListen:any
    componentDidMount(){
        const { pathname } = this.props.location
        this.updatePathName(pathname)
        this.unListen = this.props.history.listen((listen:any)=>{
            const {pathname} = listen
            this.updatePathName(pathname)
        })
    }

    componentWillUnmount(){
        this.unListen&&this.unListen()
    }

    // 非菜单点击或者导航菜单没有的url 需要页面请求是高亮对应的项需要在本方法做处理
    /**
     * 更新pathname
     * @param pathname  当前变更的pathname
     */
    updatePathName = (pathname:string)=>{
        let pname = pathname
        if(['/intelligentSeat','/help'].includes(pathname)){
            pname = '/dashboard'
        }
        this.setState({
            pathname:pname,
        })
    }

    render() {
        
        const { collapsed, authUrls } = this.props
        const {pathname} = this.state
        const collapsedBtnStyle = { fontSize: '8px', verticalAlign: 'middle' }
        return (
            <div className={styles.aside}>
                <Menu
                    defaultSelectedKeys={[pathname]}
                    selectedKeys={[pathname]}
                    defaultOpenKeys={this.getSelectedKey(pathname)}
                    inlineCollapsed={collapsed}
                    mode="inline"
                    theme="light"
                >
                    {
                        menuRoutes.map((menuItem: IMenuInfo) => {
                            if (menuItem.children && menuItem.children.length > 0) {
                                if(authUrls.length && authUrls.includes(menuItem.path))
                                return (
                                    <SubMenu key={menuItem.path} title={menuItem.meta.title} icon={<span className={`iconfont ${menuItem.icon ||''}`}></span>}>
                                        {menuItem.children && menuItem.children.map((subMenuItem: IMenuInfo) => {
                                            if(!subMenuItem.hide){
                                                if(authUrls.length && authUrls.includes(menuItem.path + subMenuItem.path))
                                                return (
                                                    <Menu.Item
                                                        onClick={() => this.handleClick(menuItem, subMenuItem)}
                                                        key={menuItem.path + subMenuItem.path}
                                                    >
                                                        {subMenuItem.meta.title}
                                                    </Menu.Item>
                                                )
                                            }else{
                                                return null
                                            }
                                        })}
                                    </SubMenu>
                                )
                            } else {
                                if(authUrls.length && authUrls.includes(menuItem.path))
                                return (
                                    <Menu.Item
                                        onClick={() => this.handleClick(menuItem, undefined)}
                                        key={menuItem.path}
                                        icon={<span className={`iconfont ${menuItem.icon || ''}`}></span>}
                                    >
                                        {menuItem.meta.title}
                                    </Menu.Item>
                                )
                            }
                        })
                    }
                </Menu>
                <div className={styles.collaspeBtn} style={{width:collapsed?56:200}} onClick={this.toggleCollapsed}>
                    {collapsed ? <DoubleRightOutlined style={collapsedBtnStyle} /> : <DoubleLeftOutlined style={collapsedBtnStyle} />}
                </div>
            </div>
        )
    }
}

export default connect(
    (state: any) => {
        return {
            menuDisplay: state.commonReducer.menuDisplay,
            collapsed: state.commonReducer.collapsed,
            authUrls: state.login.authUrls,
        }
    },
    dispatch => {
        return {
            actions: bindActionCreators(actions, dispatch),
        }
    }
)(withRouter(Aside))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值