2022-02-14 15:21:33 +08:00
|
|
|
---
|
|
|
|
order: 5
|
|
|
|
title:
|
|
|
|
zh-CN: 子菜单主题
|
|
|
|
en-US: Sub-menu theme
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
内建了两套主题 `light` 和 `dark`,默认 `light`。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
The Sub-menu will inherit the theme of `Menu`, but you can override this at the `SubMenu` level via the `theme` prop.
|
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
```tsx
|
|
|
|
import { Menu, Switch, MenuProps } from 'antd';
|
2022-02-14 15:21:33 +08:00
|
|
|
import { MailOutlined } from '@ant-design/icons';
|
|
|
|
|
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[],
|
|
|
|
theme?: 'light' | 'dark',
|
|
|
|
): MenuItem {
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
icon,
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
theme,
|
|
|
|
} as MenuItem;
|
|
|
|
}
|
2022-02-14 15:21:33 +08:00
|
|
|
|
|
|
|
const SubMenuTheme = () => {
|
2022-03-18 15:20:35 +08:00
|
|
|
const [theme, setTheme] = React.useState<'light' | 'dark'>('light');
|
2022-02-14 15:21:33 +08:00
|
|
|
const [current, setCurrent] = React.useState('1');
|
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
const changeTheme = (value: boolean) => {
|
2022-02-14 15:21:33 +08:00
|
|
|
setTheme(value ? 'dark' : 'light');
|
|
|
|
};
|
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
const onClick: MenuProps['onClick'] = e => {
|
2022-02-14 15:21:33 +08:00
|
|
|
setCurrent(e.key);
|
|
|
|
};
|
|
|
|
|
2022-03-18 15:20:35 +08:00
|
|
|
const items: MenuItem[] = [
|
|
|
|
getItem(
|
|
|
|
'Navigation One',
|
|
|
|
'sub1',
|
|
|
|
<MailOutlined />,
|
|
|
|
[getItem('Option 1', '1'), getItem('Option 2', '2'), getItem('Option 3', '3')],
|
|
|
|
theme,
|
|
|
|
),
|
|
|
|
getItem('Option 5', '5'),
|
|
|
|
getItem('Option 6', '6'),
|
|
|
|
];
|
|
|
|
|
2022-02-14 15:21:33 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Switch
|
|
|
|
checked={theme === 'dark'}
|
|
|
|
onChange={changeTheme}
|
|
|
|
checkedChildren="Dark"
|
|
|
|
unCheckedChildren="Light"
|
|
|
|
/>
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<Menu
|
2022-03-18 15:20:35 +08:00
|
|
|
onClick={onClick}
|
2022-02-14 15:21:33 +08:00
|
|
|
style={{ width: 256 }}
|
|
|
|
defaultOpenKeys={['sub1']}
|
|
|
|
selectedKeys={[current]}
|
|
|
|
mode="vertical"
|
|
|
|
theme="dark"
|
2022-03-18 15:20:35 +08:00
|
|
|
items={items}
|
|
|
|
/>
|
2022-02-14 15:21:33 +08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <SubMenuTheme />;
|
2022-02-14 15:21:33 +08:00
|
|
|
```
|