2022-11-09 12:28:04 +08:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { AppstoreOutlined, MailOutlined, SettingOutlined } from '@ant-design/icons';
|
|
|
|
import type { MenuProps } from 'antd';
|
|
|
|
import { Menu } from 'antd';
|
|
|
|
|
|
|
|
type MenuItem = Required<MenuProps>['items'][number];
|
|
|
|
|
|
|
|
const items: MenuItem[] = [
|
2024-04-23 11:01:38 +08:00
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
icon: <MailOutlined />,
|
|
|
|
label: 'Navigation One',
|
|
|
|
children: [
|
|
|
|
{ key: '11', label: 'Option 1' },
|
|
|
|
{ key: '12', label: 'Option 2' },
|
|
|
|
{ key: '13', label: 'Option 3' },
|
|
|
|
{ key: '14', label: 'Option 4' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
icon: <AppstoreOutlined />,
|
|
|
|
label: 'Navigation Two',
|
|
|
|
children: [
|
|
|
|
{ key: '21', label: 'Option 1' },
|
|
|
|
{ key: '22', label: 'Option 2' },
|
|
|
|
{
|
|
|
|
key: '23',
|
|
|
|
label: 'Submenu',
|
|
|
|
children: [
|
|
|
|
{ key: '231', label: 'Option 1' },
|
|
|
|
{ key: '232', label: 'Option 2' },
|
|
|
|
{ key: '233', label: 'Option 3' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '24',
|
|
|
|
label: 'Submenu 2',
|
|
|
|
children: [
|
|
|
|
{ key: '241', label: 'Option 1' },
|
|
|
|
{ key: '242', label: 'Option 2' },
|
|
|
|
{ key: '243', label: 'Option 3' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
icon: <SettingOutlined />,
|
|
|
|
label: 'Navigation Three',
|
|
|
|
children: [
|
|
|
|
{ key: '31', label: 'Option 1' },
|
|
|
|
{ key: '32', label: 'Option 2' },
|
|
|
|
{ key: '33', label: 'Option 3' },
|
|
|
|
{ key: '34', label: 'Option 4' },
|
|
|
|
],
|
|
|
|
},
|
2022-11-09 12:28:04 +08:00
|
|
|
];
|
|
|
|
|
2024-03-25 14:10:34 +08:00
|
|
|
interface LevelKeysProps {
|
|
|
|
key?: string;
|
|
|
|
children?: LevelKeysProps[];
|
|
|
|
}
|
2024-04-23 11:01:38 +08:00
|
|
|
|
2024-03-25 14:10:34 +08:00
|
|
|
const getLevelKeys = (items1: LevelKeysProps[]) => {
|
|
|
|
const key: Record<string, number> = {};
|
|
|
|
const func = (items2: LevelKeysProps[], level = 1) => {
|
|
|
|
items2.forEach((item) => {
|
|
|
|
if (item.key) {
|
|
|
|
key[item.key] = level;
|
|
|
|
}
|
|
|
|
if (item.children) {
|
2024-04-23 11:01:38 +08:00
|
|
|
func(item.children, level + 1);
|
2024-03-25 14:10:34 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
func(items1);
|
|
|
|
return key;
|
|
|
|
};
|
2024-04-23 11:01:38 +08:00
|
|
|
|
2024-03-25 14:10:34 +08:00
|
|
|
const levelKeys = getLevelKeys(items as LevelKeysProps[]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const App: React.FC = () => {
|
2024-03-25 14:10:34 +08:00
|
|
|
const [stateOpenKeys, setStateOpenKeys] = useState(['2', '23']);
|
|
|
|
|
|
|
|
const onOpenChange: MenuProps['onOpenChange'] = (openKeys) => {
|
|
|
|
const currentOpenKey = openKeys.find((key) => stateOpenKeys.indexOf(key) === -1);
|
|
|
|
// open
|
|
|
|
if (currentOpenKey !== undefined) {
|
|
|
|
const repeatIndex = openKeys
|
|
|
|
.filter((key) => key !== currentOpenKey)
|
|
|
|
.findIndex((key) => levelKeys[key] === levelKeys[currentOpenKey]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2024-03-25 14:10:34 +08:00
|
|
|
setStateOpenKeys(
|
|
|
|
openKeys
|
|
|
|
// remove repeat key
|
|
|
|
.filter((_, index) => index !== repeatIndex)
|
|
|
|
// remove current level all child
|
|
|
|
.filter((key) => levelKeys[key] <= levelKeys[currentOpenKey]),
|
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
} else {
|
2024-03-25 14:10:34 +08:00
|
|
|
// close
|
|
|
|
setStateOpenKeys(openKeys);
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Menu
|
|
|
|
mode="inline"
|
2024-03-25 14:10:34 +08:00
|
|
|
defaultSelectedKeys={['231']}
|
|
|
|
openKeys={stateOpenKeys}
|
2022-11-09 12:28:04 +08:00
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
style={{ width: 256 }}
|
|
|
|
items={items}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|