React+Ant Design左侧二级导航菜单

package.json:

{
  "name": "antd-demo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@ant-design/icons": "^4.2.1",
    "@babel/core": "7.9.0",
    "@svgr/webpack": "4.3.3",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@typescript-eslint/eslint-plugin": "^2.10.0",
    "@typescript-eslint/parser": "^2.10.0",
    "antd": "^4.7.0",
    "axios": "^0.19.2",
    "babel-eslint": "10.1.0",
    "babel-jest": "^24.9.0",
    "babel-loader": "8.1.0",
    "babel-plugin-import": "^1.13.0",
    "babel-plugin-named-asset-import": "^0.3.6",
    "babel-preset-react-app": "^9.1.2",
    "camelcase": "^5.3.1",
    "case-sensitive-paths-webpack-plugin": "2.3.0",
    "css-loader": "3.4.2",
    "dotenv": "8.2.0",
    "dotenv-expand": "5.1.0",
    "echarts": "^4.9.0",
    "echarts-for-react": "^2.0.16",
    "eslint": "^6.6.0",
    "eslint-config-react-app": "^5.2.1",
    "eslint-loader": "3.0.3",
    "eslint-plugin-flowtype": "4.6.0",
    "eslint-plugin-import": "2.20.1",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.19.0",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "4.3.0",
    "fs-extra": "^8.1.0",
    "html-webpack-plugin": "4.0.0-beta.11",
    "identity-obj-proxy": "3.0.0",
    "jest": "24.9.0",
    "jest-environment-jsdom-fourteen": "1.0.1",
    "jest-resolve": "24.9.0",
    "jest-watch-typeahead": "0.4.2",
    "mini-css-extract-plugin": "0.9.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "pnp-webpack-plugin": "1.6.4",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "8.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.13.1",
    "react-app-polyfill": "^1.0.6",
    "react-dev-utils": "^10.2.1",
    "react-dom": "^16.13.1",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.0",
    "react-transition-group": "^4.4.1",
    "redux": "^4.0.5",
    "resolve": "1.15.0",
    "resolve-url-loader": "3.1.1",
    "sass-loader": "8.0.2",
    "semver": "6.3.0",
    "style-loader": "0.23.1",
    "terser-webpack-plugin": "2.3.5",
    "ts-pnp": "1.1.6",
    "url-loader": "2.3.0",
    "webpack": "4.42.0",
    "webpack-dev-server": "3.10.3",
    "webpack-manifest-plugin": "2.2.0",
    "workbox-webpack-plugin": "4.3.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "proxy": "http://localhost:9999",
  "homepage": ".",
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "devDependencies": {
    "less": "^3.12.2",
    "less-loader": "^6.2.0"
  }
}

config/route-config.js(菜单配置文件):

import { SnippetsFilled, PieChartFilled, PieChartOutlined, ProfileOutlined, FileWordOutlined, FileTextOutlined , BarChartOutlined, WhatsAppOutlined, ExclamationCircleOutlined, IeOutlined, SketchOutlined } from '@ant-design/icons';

const MenuList = [
    {
        title: '首页', // 菜单标题名称
        key: '/home', // 对应的path
        icon: SnippetsFilled, // 图标名称,
        children: [ // 子菜单列表
            {
                title: '监控页',
                key: '/monitor',
                icon: 'bars'
            },
            {
                title: '分析页',
                key: '/home',
                icon: 'bars'
            },
            {
                title: '工作台',
                key: '/workbench',
                icon: 'bars'
            }
        ]
    },
    {
        title: '数据录入',
        key: '/data_input',
        icon: PieChartFilled,
        children: [ // 子菜单列表
            {
                title: 'form',
                key: '/form',
                icon: 'bars'
            }
        ]
    },
    {
        title: '数据展示',
        key: '/data_show',
        icon: BarChartOutlined,
        children: [
            {
                title: 'table',
                key: '/table',
                icon: 'bar-chart'
            },
            {
                title: 'timeline ',
                key: '/timeline',
                icon: 'line-chart'
            },
            {
                title: 'descriptions ',
                key: '/descriptions',
                icon: 'line-chart'
            }
        ]
    },
    {
        title: '反馈',
        key: '/feedback',
        icon: WhatsAppOutlined,
        children: [
            {
                title: 'modal',
                key: '/modal1',
                icon: 'bar-chart'
            },
            {
                title: 'notification',
                key: '/notification',
                icon: 'line-chart'
            }
        ]
    },
    {
        title: '图表',
        key: '/charts',
        icon: PieChartOutlined,
        children: [
            {
                title: 'bar',
                key: '/bar',
                icon: 'bar-chart'
            },
            {
                title: 'line',
                key: '/line',
                icon: 'line-chart'
            },
            {
                title: 'pie',
                key: '/pie',
                icon: 'pie-chart'
            }
        ]
    },
    {
        title: '异常页',
        key: '/abnormal',
        icon: ExclamationCircleOutlined,
        children: [
            {
                title: '403',
                key: '/403',
                icon: 'bar-chart'
            },
            {
                title: '404',
                key: '/404',
                icon: 'line-chart'
            },
            {
                title: '500',
                key: '/500',
                icon: 'pie-chart'
            }
        ]
    },
    {
        title: '表单页',
        key: '/form',
        icon: FileTextOutlined,
        children: [
            {
                title: '基础表单',
                key: '/base-form',
                icon: 'bar-chart'
            },
            {
                title: '模态表单',
                key: '/modal-form',
                icon: 'bar-chart'
            },
            {
                title: '分步表单',
                key: '/step-form',
                icon: 'line-chart'
            },
            {
                title: '高级表单',
                key: '/advanced-form',
                icon: 'pie-chart'
            }
        ]
    },
    {
        title: '列表页',
        key: '/list',
        icon: ProfileOutlined,
        children: [
            {
                title: '搜索列表',
                key: '/search-list',
                icon: 'bar-chart'
            },
            {
                title: '查询表格',
                key: '/query-table',
                icon: 'line-chart'
            },
            {
                title: '标准列表',
                key: '/standard-list',
                icon: 'pie-chart'
            },
            {
                title: '卡片列表',
                key: '/card-list',
                icon: 'pie-chart'
            },
            {
                title: '树列表',
                key: '/tree-list',
                icon: 'pie-chart'
            }
        ]
    },
    {
        title: '详情页',
        key: '/detail',
        icon: FileWordOutlined,
        children: [
            {
                title: '基础详情页',
                key: '/base-detail',
                icon: 'bar-chart'
            },
            {
                title: '高级详情页',
                key: '/senior-detail',
                icon: 'line-chart'
            }
        ]
    },
    {
        title: '网盘',
        key: '/webdisk',
        icon: IeOutlined,
        children: [
            {
                title: '管理员管理',
                key: '/administrator',
                icon: 'bar-chart'
            },
            {
                title: '审计日志管理',
                key: '/log',
                icon: 'line-chart'
            },
            {
                title: '收到的共享',
                key: '/myshare',
                icon: 'line-chart'
            },
            {
                title: 'USBKEY',
                key: '/usbkey',
                icon: 'line-chart'
            }
        ]
    },
    {
        title: '加固',
        key: '/firmstor',
        icon: SketchOutlined,
        children: [
            {
                title: '基本信息',
                key: '/baseinfo',
                icon: 'bar-chart'
            },
            {
                title: '保险箱文件恢复',
                key: '/file-recovery',
                icon: 'bar-chart'
            }
        ]
    }
]

export default MenuList

left-nav.jsx:

import React, { Component } from 'react'
import { Menu } from 'antd';
import { Link, withRouter } from 'react-router-dom'
import MenuList from '../../config/route-config'
import './index.less'

const { SubMenu } = Menu;

class LeftNav extends Component {
    state = {
        rootSubmenuKeys: [], //只展开当前父级菜单
        openKeys: [], // 默认展开的菜单项
    };

    // 只展开当前父级菜单
    onOpenChange = openKeys => {
        const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
        if (this.state.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
            this.setState({ openKeys });
        } else {
            this.setState({
                openKeys: latestOpenKey ? [latestOpenKey] : [],
            });
        }
    };

    // 获取左侧菜单
    getMenuNodes = MenuList => {
        const path = this.props.location.pathname;

        return MenuList.map(item => {
            if (!item.children) {
                return (
                    <Menu.Item key={item.key} icon={<item.icon />}>
                        <Link to={item.key}>
                            <span>{item.title}</span>
                        </Link>
                    </Menu.Item>
                )
            } else {
                this.state.rootSubmenuKeys.push(item.key);

                // 查找一个与当前请求路径匹配的子Item
                const childItem = item.children.find(cItem => path.indexOf(cItem.key) === 0);
                // 如果存在, 说明当前item的子列表需要打开
                if (childItem) {
                    this.setState({
                        openKeys: [item.key]
                    })
                }

                return (
                    <SubMenu
                        key={item.key}
                        icon={<item.icon />}
                        title={item.title}
                    >
                        {this.getMenuNodes(item.children)}
                    </SubMenu>
                )
            }
        })
    }

    componentWillMount() {
        this.menuNodes = this.getMenuNodes(MenuList);
    }

    render() {
        /*selectedKeys:控制默认高亮菜单项,defaultOpenKeys、openKeys、onOpenChange:控制菜单展开*/
        return (
            <Menu
                theme="dark"
                mode="inline"
                selectedKeys={this.props.location.pathname}
                defaultOpenKeys={this.state.openKeys}
                openKeys={this.state.openKeys}
                onOpenChange={this.onOpenChange}
                style={{ height: '100%', borderRight: 0, paddingTop: '60px' }}
            >
                {
                    this.menuNodes
                }
            </Menu>
        )
    }
}

export default withRouter(LeftNav)

index.less:

.ant-menu {
    overflow: auto!important;
}
.ant-menu::-webkit-scrollbar {
    /*滚动条整体样式*/
    width : 5px;  /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
}
.ant-menu::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    background   : #535353;
}
.ant-menu::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 10px;
    background   : #ededed;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值