ant-design/components/menu/demo/switch-mode.md

99 lines
2.1 KiB
Markdown
Raw Normal View History

2016-04-27 17:44:25 +08:00
---
order: 6
title:
zh-CN: 切换菜单类型
en-US: Switch the menu type
2016-04-27 17:44:25 +08:00
---
## zh-CN
展示动态切换模式。
## en-US
Show the dynamic switching mode (between `inline` and `vertical`).
```tsx
import type { MenuProps } from 'antd';
import { Menu, Switch, Divider } from 'antd';
import {
MailOutlined,
CalendarOutlined,
AppstoreOutlined,
SettingOutlined,
LinkOutlined,
} from '@ant-design/icons';
2018-06-27 15:55:04 +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 />,
),
];
const Demo = () => {
const [mode, setMode] = React.useState<'inline' | 'vertical'>('inline');
const [theme, setTheme] = React.useState<'dark' | 'light'>('light');
2018-06-27 15:55:04 +08:00
const changeMode = (value: boolean) => {
setMode(value ? 'vertical' : 'inline');
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const changeTheme = (value: boolean) => {
setTheme(value ? 'dark' : 'light');
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +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}
items={items}
/>
</>
);
};
export default Demo;
2019-05-07 14:57:32 +08:00
```