2016-04-27 17:44:25 +08:00
|
|
|
---
|
2022-02-14 15:21:33 +08:00
|
|
|
order: 6
|
2016-08-26 14:42:41 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 切换菜单类型
|
|
|
|
en-US: Switch the menu type
|
2016-04-27 17:44:25 +08:00
|
|
|
---
|
2016-04-27 15:26:03 +08:00
|
|
|
|
2016-08-26 14:42:41 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-04-27 15:26:03 +08:00
|
|
|
展示动态切换模式。
|
|
|
|
|
2016-08-26 14:42:41 +08:00
|
|
|
## en-US
|
|
|
|
|
2020-10-12 12:00:00 +08:00
|
|
|
Show the dynamic switching mode (between `inline` and `vertical`).
|
2016-08-26 14:42:41 +08:00
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
```tsx
|
2019-11-28 12:34:33 +08:00
|
|
|
import {
|
|
|
|
AppstoreOutlined,
|
2022-05-23 14:37:16 +08:00
|
|
|
CalendarOutlined,
|
2020-05-03 11:28:15 +08:00
|
|
|
LinkOutlined,
|
2022-05-23 14:37:16 +08:00
|
|
|
MailOutlined,
|
|
|
|
SettingOutlined,
|
2019-11-28 12:34:33 +08:00
|
|
|
} from '@ant-design/icons';
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Divider, Menu, Switch } from 'antd';
|
|
|
|
import type { MenuProps, MenuTheme } from 'antd/es/menu';
|
|
|
|
import React, { useState } from 'react';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
type MenuItem = Required<MenuProps>['items'][number];
|
|
|
|
|
|
|
|
function getItem(
|
|
|
|
label: React.ReactNode,
|
|
|
|
key?: React.Key | null,
|
|
|
|
icon?: React.ReactNode,
|
|
|
|
children?: MenuItem[],
|
|
|
|
): MenuItem {
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
icon,
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
} as MenuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const items: MenuItem[] = [
|
|
|
|
getItem('Navigation One', '1', <MailOutlined />),
|
|
|
|
getItem('Navigation Two', '2', <CalendarOutlined />),
|
|
|
|
getItem('Navigation Two', 'sub1', <AppstoreOutlined />, [
|
|
|
|
getItem('Option 3', '3'),
|
|
|
|
getItem('Option 4', '4'),
|
|
|
|
getItem('Submenu', 'sub1-2', null, [getItem('Option 5', '5'), getItem('Option 6', '6')]),
|
|
|
|
]),
|
|
|
|
getItem('Navigation Three', 'sub2', <SettingOutlined />, [
|
|
|
|
getItem('Option 7', '7'),
|
|
|
|
getItem('Option 8', '8'),
|
|
|
|
getItem('Option 9', '9'),
|
|
|
|
getItem('Option 10', '10'),
|
|
|
|
]),
|
|
|
|
getItem(
|
|
|
|
<a href="https://ant.design" target="_blank" rel="noopener noreferrer">
|
|
|
|
Ant Design
|
|
|
|
</a>,
|
|
|
|
'link',
|
|
|
|
<LinkOutlined />,
|
|
|
|
),
|
|
|
|
];
|
2016-04-27 15:26:03 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [mode, setMode] = useState<'vertical' | 'inline'>('inline');
|
|
|
|
const [theme, setTheme] = useState<MenuTheme>('light');
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
const changeMode = (value: boolean) => {
|
2020-11-09 08:57:24 +08:00
|
|
|
setMode(value ? 'vertical' : 'inline');
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
const changeTheme = (value: boolean) => {
|
2020-11-09 08:57:24 +08:00
|
|
|
setTheme(value ? 'dark' : 'light');
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-11-09 08:57:24 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Switch onChange={changeMode} /> Change Mode
|
|
|
|
<Divider type="vertical" />
|
|
|
|
<Switch onChange={changeTheme} /> Change Style
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<Menu
|
|
|
|
style={{ width: 256 }}
|
|
|
|
defaultSelectedKeys={['1']}
|
|
|
|
defaultOpenKeys={['sub1']}
|
|
|
|
mode={mode}
|
|
|
|
theme={theme}
|
2022-03-18 15:20:35 +08:00
|
|
|
items={items}
|
|
|
|
/>
|
2020-11-09 08:57:24 +08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2017-01-16 21:13:59 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|