mirror of https://github.com/openspug/spug
fix issue for sider
parent
a8db60345c
commit
8bf264bcd2
|
@ -1,4 +1,4 @@
|
||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import { Layout, Menu } from 'antd';
|
import { Layout, Menu } from 'antd';
|
||||||
import { hasPermission, history } from 'libs';
|
import { hasPermission, history } from 'libs';
|
||||||
import styles from './layout.module.less';
|
import styles from './layout.module.less';
|
||||||
|
@ -6,19 +6,21 @@ import menus from '../routes';
|
||||||
import logo from './logo-spug.png';
|
import logo from './logo-spug.png';
|
||||||
import logoText from './logo-text.png';
|
import logoText from './logo-text.png';
|
||||||
|
|
||||||
const initPath = window.location.pathname;
|
let selectedKey = window.location.pathname;
|
||||||
let openKeys = [];
|
const OpenKeysMap = {};
|
||||||
loop:
|
for (let item of menus) {
|
||||||
for (let item of menus.filter(x => x.child)) {
|
if (item.child) {
|
||||||
for (let sub of item.child) {
|
for (let sub of item.child) {
|
||||||
if (sub.path === initPath) {
|
if (sub.title) OpenKeysMap[sub.path] = item.title
|
||||||
openKeys = [item.title]
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if (item.title) {
|
||||||
|
OpenKeysMap[item.path] = 1
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default function Sider(props) {
|
export default function Sider(props) {
|
||||||
|
const [openKeys, setOpenKeys] = useState([]);
|
||||||
|
|
||||||
function makeMenu(menu) {
|
function makeMenu(menu) {
|
||||||
if (menu.auth && !hasPermission(menu.auth)) return null;
|
if (menu.auth && !hasPermission(menu.auth)) return null;
|
||||||
if (!menu.title) return null;
|
if (!menu.title) return null;
|
||||||
|
@ -42,6 +44,14 @@ export default function Sider(props) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tmp = window.location.pathname;
|
||||||
|
const openKey = OpenKeysMap[tmp];
|
||||||
|
if (openKey) {
|
||||||
|
selectedKey = tmp;
|
||||||
|
if (openKey !== 1 && !openKeys.includes(openKey)) {
|
||||||
|
setOpenKeys([...openKeys, openKey])
|
||||||
|
}
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Layout.Sider width={208} collapsed={props.collapsed} className={styles.sider}>
|
<Layout.Sider width={208} collapsed={props.collapsed} className={styles.sider}>
|
||||||
<div className={styles.logo}>
|
<div className={styles.logo}>
|
||||||
|
@ -53,8 +63,9 @@ export default function Sider(props) {
|
||||||
theme="dark"
|
theme="dark"
|
||||||
mode="inline"
|
mode="inline"
|
||||||
className={styles.menus}
|
className={styles.menus}
|
||||||
defaultSelectedKeys={[initPath]}
|
selectedKeys={[selectedKey]}
|
||||||
defaultOpenKeys={openKeys}
|
openKeys={openKeys}
|
||||||
|
onOpenChange={setOpenKeys}
|
||||||
onSelect={menu => history.push(menu.key)}>
|
onSelect={menu => history.push(menu.key)}>
|
||||||
{menus.map(menu => makeMenu(menu))}
|
{menus.map(menu => makeMenu(menu))}
|
||||||
</Menu>
|
</Menu>
|
||||||
|
|
Loading…
Reference in New Issue