Menu导航菜单

为页面和功能提供导航的菜单列表。

何时使用

导航菜单是一个网站的灵魂,用户依赖导航在各个页面中进行跳转。一般分为顶部导航和侧边导航,顶部导航提供全局性的类目和功能,侧边导航提供多级结构来收纳和排列网站架构。

更多布局和导航的使用可以参考:通用布局

代码演示

Menu导航菜单 - 图1

顶部导航

水平的顶部导航菜单。

  1. import { Menu } from 'antd';
  2. import { MailOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. class App extends React.Component {
  5. state = {
  6. current: 'mail',
  7. };
  8. handleClick = e => {
  9. console.log('click ', e);
  10. this.setState({ current: e.key });
  11. };
  12. render() {
  13. const { current } = this.state;
  14. return (
  15. <Menu onClick={this.handleClick} selectedKeys={[current]} mode="horizontal">
  16. <Menu.Item key="mail" icon={<MailOutlined />}>
  17. Navigation One
  18. </Menu.Item>
  19. <Menu.Item key="app" disabled icon={<AppstoreOutlined />}>
  20. Navigation Two
  21. </Menu.Item>
  22. <SubMenu key="SubMenu" icon={<SettingOutlined />} title="Navigation Three - Submenu">
  23. <Menu.ItemGroup title="Item 1">
  24. <Menu.Item key="setting:1">Option 1</Menu.Item>
  25. <Menu.Item key="setting:2">Option 2</Menu.Item>
  26. </Menu.ItemGroup>
  27. <Menu.ItemGroup title="Item 2">
  28. <Menu.Item key="setting:3">Option 3</Menu.Item>
  29. <Menu.Item key="setting:4">Option 4</Menu.Item>
  30. </Menu.ItemGroup>
  31. </SubMenu>
  32. <Menu.Item key="alipay">
  33. <a href="https://ant.design" target="_blank" rel="noopener noreferrer">
  34. Navigation Four - Link
  35. </a>
  36. </Menu.Item>
  37. </Menu>
  38. );
  39. }
  40. }
  41. ReactDOM.render(<App />, mountNode);

Menu导航菜单 - 图2

内嵌菜单

垂直菜单,子菜单内嵌在菜单区域。

  1. import { Menu } from 'antd';
  2. import { AppstoreOutlined, MailOutlined, SettingOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. class Sider extends React.Component {
  5. handleClick = e => {
  6. console.log('click ', e);
  7. };
  8. render() {
  9. return (
  10. <Menu
  11. onClick={this.handleClick}
  12. style={{ width: 256 }}
  13. defaultSelectedKeys={['1']}
  14. defaultOpenKeys={['sub1']}
  15. mode="inline"
  16. >
  17. <SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One">
  18. <Menu.ItemGroup key="g1" title="Item 1">
  19. <Menu.Item key="1">Option 1</Menu.Item>
  20. <Menu.Item key="2">Option 2</Menu.Item>
  21. </Menu.ItemGroup>
  22. <Menu.ItemGroup key="g2" title="Item 2">
  23. <Menu.Item key="3">Option 3</Menu.Item>
  24. <Menu.Item key="4">Option 4</Menu.Item>
  25. </Menu.ItemGroup>
  26. </SubMenu>
  27. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  28. <Menu.Item key="5">Option 5</Menu.Item>
  29. <Menu.Item key="6">Option 6</Menu.Item>
  30. <SubMenu key="sub3" title="Submenu">
  31. <Menu.Item key="7">Option 7</Menu.Item>
  32. <Menu.Item key="8">Option 8</Menu.Item>
  33. </SubMenu>
  34. </SubMenu>
  35. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  36. <Menu.Item key="9">Option 9</Menu.Item>
  37. <Menu.Item key="10">Option 10</Menu.Item>
  38. <Menu.Item key="11">Option 11</Menu.Item>
  39. <Menu.Item key="12">Option 12</Menu.Item>
  40. </SubMenu>
  41. </Menu>
  42. );
  43. }
  44. }
  45. ReactDOM.render(<Sider />, mountNode);

Menu导航菜单 - 图3

缩起内嵌菜单

内嵌菜单可以被缩起/展开。

你可以在 Layout 里查看侧边布局结合的完整示例。

  1. import { Menu, Button } from 'antd';
  2. import {
  3. AppstoreOutlined,
  4. MenuUnfoldOutlined,
  5. MenuFoldOutlined,
  6. PieChartOutlined,
  7. DesktopOutlined,
  8. ContainerOutlined,
  9. MailOutlined,
  10. } from '@ant-design/icons';
  11. const { SubMenu } = Menu;
  12. class App extends React.Component {
  13. state = {
  14. collapsed: false,
  15. };
  16. toggleCollapsed = () => {
  17. this.setState({
  18. collapsed: !this.state.collapsed,
  19. });
  20. };
  21. render() {
  22. return (
  23. <div style={{ width: 256 }}>
  24. <Button type="primary" onClick={this.toggleCollapsed} style={{ marginBottom: 16 }}>
  25. {React.createElement(this.state.collapsed ? MenuUnfoldOutlined : MenuFoldOutlined)}
  26. </Button>
  27. <Menu
  28. defaultSelectedKeys={['1']}
  29. defaultOpenKeys={['sub1']}
  30. mode="inline"
  31. theme="dark"
  32. inlineCollapsed={this.state.collapsed}
  33. >
  34. <Menu.Item key="1" icon={<PieChartOutlined />}>
  35. Option 1
  36. </Menu.Item>
  37. <Menu.Item key="2" icon={<DesktopOutlined />}>
  38. Option 2
  39. </Menu.Item>
  40. <Menu.Item key="3" icon={<ContainerOutlined />}>
  41. Option 3
  42. </Menu.Item>
  43. <SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One">
  44. <Menu.Item key="5">Option 5</Menu.Item>
  45. <Menu.Item key="6">Option 6</Menu.Item>
  46. <Menu.Item key="7">Option 7</Menu.Item>
  47. <Menu.Item key="8">Option 8</Menu.Item>
  48. </SubMenu>
  49. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  50. <Menu.Item key="9">Option 9</Menu.Item>
  51. <Menu.Item key="10">Option 10</Menu.Item>
  52. <SubMenu key="sub3" title="Submenu">
  53. <Menu.Item key="11">Option 11</Menu.Item>
  54. <Menu.Item key="12">Option 12</Menu.Item>
  55. </SubMenu>
  56. </SubMenu>
  57. </Menu>
  58. </div>
  59. );
  60. }
  61. }
  62. ReactDOM.render(<App />, mountNode);

Menu导航菜单 - 图4

只展开当前父级菜单

点击菜单,收起其他展开的所有菜单,保持菜单聚焦简洁。

  1. import { Menu } from 'antd';
  2. import { AppstoreOutlined, MailOutlined, SettingOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. // submenu keys of first level
  5. const rootSubmenuKeys = ['sub1', 'sub2', 'sub4'];
  6. const Sider = () => {
  7. const [openKeys, setOpenKeys] = React.useState(['sub1']);
  8. const onOpenChange = keys => {
  9. const latestOpenKey = keys.find(key => openKeys.indexOf(key) === -1);
  10. if (rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  11. setOpenKeys(keys);
  12. } else {
  13. setOpenKeys(latestOpenKey ? [latestOpenKey] : []);
  14. }
  15. };
  16. return (
  17. <Menu mode="inline" openKeys={openKeys} onOpenChange={onOpenChange} style={{ width: 256 }}>
  18. <SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One">
  19. <Menu.Item key="1">Option 1</Menu.Item>
  20. <Menu.Item key="2">Option 2</Menu.Item>
  21. <Menu.Item key="3">Option 3</Menu.Item>
  22. <Menu.Item key="4">Option 4</Menu.Item>
  23. </SubMenu>
  24. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  25. <Menu.Item key="5">Option 5</Menu.Item>
  26. <Menu.Item key="6">Option 6</Menu.Item>
  27. <SubMenu key="sub3" title="Submenu">
  28. <Menu.Item key="7">Option 7</Menu.Item>
  29. <Menu.Item key="8">Option 8</Menu.Item>
  30. </SubMenu>
  31. </SubMenu>
  32. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  33. <Menu.Item key="9">Option 9</Menu.Item>
  34. <Menu.Item key="10">Option 10</Menu.Item>
  35. <Menu.Item key="11">Option 11</Menu.Item>
  36. <Menu.Item key="12">Option 12</Menu.Item>
  37. </SubMenu>
  38. </Menu>
  39. );
  40. };
  41. ReactDOM.render(<Sider />, mountNode);

Menu导航菜单 - 图5

垂直菜单

子菜单是弹出的形式。

  1. import { Menu } from 'antd';
  2. import { MailOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. function handleClick(e) {
  5. console.log('click', e);
  6. }
  7. ReactDOM.render(
  8. <Menu onClick={handleClick} style={{ width: 256 }} mode="vertical">
  9. <SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One">
  10. <Menu.ItemGroup title="Item 1">
  11. <Menu.Item key="1">Option 1</Menu.Item>
  12. <Menu.Item key="2">Option 2</Menu.Item>
  13. </Menu.ItemGroup>
  14. <Menu.ItemGroup title="Iteom 2">
  15. <Menu.Item key="3">Option 3</Menu.Item>
  16. <Menu.Item key="4">Option 4</Menu.Item>
  17. </Menu.ItemGroup>
  18. </SubMenu>
  19. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  20. <Menu.Item key="5">Option 5</Menu.Item>
  21. <Menu.Item key="6">Option 6</Menu.Item>
  22. <SubMenu key="sub3" title="Submenu">
  23. <Menu.Item key="7">Option 7</Menu.Item>
  24. <Menu.Item key="8">Option 8</Menu.Item>
  25. </SubMenu>
  26. </SubMenu>
  27. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  28. <Menu.Item key="9">Option 9</Menu.Item>
  29. <Menu.Item key="10">Option 10</Menu.Item>
  30. <Menu.Item key="11">Option 11</Menu.Item>
  31. <Menu.Item key="12">Option 12</Menu.Item>
  32. </SubMenu>
  33. </Menu>,
  34. mountNode,
  35. );

Menu导航菜单 - 图6

主题

内建了两套主题 lightdark,默认 light

  1. import { Menu, Switch } from 'antd';
  2. import { MailOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. class Sider extends React.Component {
  5. state = {
  6. theme: 'dark',
  7. current: '1',
  8. };
  9. changeTheme = value => {
  10. this.setState({
  11. theme: value ? 'dark' : 'light',
  12. });
  13. };
  14. handleClick = e => {
  15. console.log('click ', e);
  16. this.setState({
  17. current: e.key,
  18. });
  19. };
  20. render() {
  21. return (
  22. <>
  23. <Switch
  24. checked={this.state.theme === 'dark'}
  25. onChange={this.changeTheme}
  26. checkedChildren="Dark"
  27. unCheckedChildren="Light"
  28. />
  29. <br />
  30. <br />
  31. <Menu
  32. theme={this.state.theme}
  33. onClick={this.handleClick}
  34. style={{ width: 256 }}
  35. defaultOpenKeys={['sub1']}
  36. selectedKeys={[this.state.current]}
  37. mode="inline"
  38. >
  39. <SubMenu key="sub1" icon={<MailOutlined />} title="Navigation One">
  40. <Menu.Item key="1">Option 1</Menu.Item>
  41. <Menu.Item key="2">Option 2</Menu.Item>
  42. <Menu.Item key="3">Option 3</Menu.Item>
  43. <Menu.Item key="4">Option 4</Menu.Item>
  44. </SubMenu>
  45. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  46. <Menu.Item key="5">Option 5</Menu.Item>
  47. <Menu.Item key="6">Option 6</Menu.Item>
  48. <SubMenu key="sub3" title="Submenu">
  49. <Menu.Item key="7">Option 7</Menu.Item>
  50. <Menu.Item key="8">Option 8</Menu.Item>
  51. </SubMenu>
  52. </SubMenu>
  53. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  54. <Menu.Item key="9">Option 9</Menu.Item>
  55. <Menu.Item key="10">Option 10</Menu.Item>
  56. <Menu.Item key="11">Option 11</Menu.Item>
  57. <Menu.Item key="12">Option 12</Menu.Item>
  58. </SubMenu>
  59. </Menu>
  60. </>
  61. );
  62. }
  63. }
  64. ReactDOM.render(<Sider />, mountNode);

Menu导航菜单 - 图7

切换菜单类型

展示动态切换模式。

  1. import { Menu, Switch, Divider } from 'antd';
  2. import {
  3. MailOutlined,
  4. CalendarOutlined,
  5. AppstoreOutlined,
  6. SettingOutlined,
  7. LinkOutlined,
  8. } from '@ant-design/icons';
  9. const { SubMenu } = Menu;
  10. const Demo = () => {
  11. const [mode, setMode] = React.useState('inline');
  12. const [theme, setTheme] = React.useState('light');
  13. const changeMode = value => {
  14. setMode(value ? 'vertical' : 'inline');
  15. };
  16. const changeTheme = value => {
  17. setTheme(value ? 'dark' : 'light');
  18. };
  19. return (
  20. <>
  21. <Switch onChange={changeMode} /> Change Mode
  22. <Divider type="vertical" />
  23. <Switch onChange={changeTheme} /> Change Style
  24. <br />
  25. <br />
  26. <Menu
  27. style={{ width: 256 }}
  28. defaultSelectedKeys={['1']}
  29. defaultOpenKeys={['sub1']}
  30. mode={mode}
  31. theme={theme}
  32. >
  33. <Menu.Item key="1" icon={<MailOutlined />}>
  34. Navigation One
  35. </Menu.Item>
  36. <Menu.Item key="2" icon={<CalendarOutlined />}>
  37. Navigation Two
  38. </Menu.Item>
  39. <SubMenu key="sub1" icon={<AppstoreOutlined />} title="Navigation Two">
  40. <Menu.Item key="3">Option 3</Menu.Item>
  41. <Menu.Item key="4">Option 4</Menu.Item>
  42. <SubMenu key="sub1-2" title="Submenu">
  43. <Menu.Item key="5">Option 5</Menu.Item>
  44. <Menu.Item key="6">Option 6</Menu.Item>
  45. </SubMenu>
  46. </SubMenu>
  47. <SubMenu key="sub2" icon={<SettingOutlined />} title="Navigation Three">
  48. <Menu.Item key="7">Option 7</Menu.Item>
  49. <Menu.Item key="8">Option 8</Menu.Item>
  50. <Menu.Item key="9">Option 9</Menu.Item>
  51. <Menu.Item key="10">Option 10</Menu.Item>
  52. </SubMenu>
  53. <Menu.Item key="link" icon={<LinkOutlined />}>
  54. <a href="https://ant.design" target="_blank" rel="noopener noreferrer">
  55. Ant Design
  56. </a>
  57. </Menu.Item>
  58. </Menu>
  59. </>
  60. );
  61. };
  62. ReactDOM.render(<Demo />, mountNode);

API

  1. <Menu>
  2. <Menu.Item>菜单项</Menu.Item>
  3. <SubMenu title="子菜单">
  4. <Menu.Item>子菜单项</Menu.Item>
  5. </SubMenu>
  6. </Menu>

Menu

参数说明类型默认值版本
defaultOpenKeys初始展开的 SubMenu 菜单项 key 数组string[]-
defaultSelectedKeys初始选中的菜单项 key 数组string[]-
expandIcon自定义展开图标ReactNode | (props: SubMenuProps & { isSubMenu: boolean }) => ReactNode-4.9.0
forceSubMenuRender在子菜单展示之前就渲染进 DOMbooleanfalse
inlineCollapsedinline 时菜单是否收起状态boolean-
inlineIndentinline 模式的菜单缩进宽度number24
mode菜单类型,现在支持垂直、水平、和内嵌模式三种vertical | horizontal | inlinevertical
multiple是否允许多选booleanfalse
openKeys当前展开的 SubMenu 菜单项 key 数组string[]-
overflowedIndicator自定义 Menu 折叠时的图标ReactNode-
selectable是否允许选中booleantrue
selectedKeys当前选中的菜单项 key 数组string[]-
style根节点样式CSSProperties-
subMenuCloseDelay用户鼠标离开子菜单后关闭延时,单位:秒number0.1
subMenuOpenDelay用户鼠标进入子菜单后开启延时,单位:秒number0
theme主题颜色light | darklight
triggerSubMenuActionSubMenu 展开/关闭的触发行为hover | clickhover
onClick点击 MenuItem 调用此函数function({ item, key, keyPath, domEvent })-
onDeselect取消选中时调用,仅在 multiple 生效function({ item, key, keyPath, selectedKeys, domEvent })-
onOpenChangeSubMenu 展开/关闭的回调function(openKeys: string[])-
onSelect被选中时调用function({ item, key, keyPath, selectedKeys, domEvent })-  

更多属性查看 rc-menu

Menu.Item

参数说明类型默认值版本
danger展示错误状态样式booleanfalse4.3.0
disabled是否禁用booleanfalse
icon菜单图标ReactNode-4.2.0
keyitem 的唯一标志string-
title设置收缩时展示的悬浮标题string-

注意:icon4.2.0 新增的属性,之前的版本请使用下面的方式定义图标。

  1. <Menu.Item>
  2. <PieChartOutlined />
  3. <span>Option 1</span>
  4. </Menu.Item>
  5. <Menu.SubMenu
  6. title={
  7. <>
  8. <PieChartOutlined />
  9. <span>Option 2</span>
  10. </>
  11. }
  12. >
  13. ...
  14. </Menu.SubMenu>

Menu.SubMenu

参数说明类型默认值版本
children子菜单的菜单项Array<MenuItem | SubMenu>-
disabled是否禁用booleanfalse
icon菜单图标ReactNode-4.2.0
key唯一标志string-
popupClassName子菜单样式,mode=”inline” 时无效string-
popupOffset子菜单偏移量,mode=”inline” 时无效[number, number]-
title子菜单项值ReactNode-
onTitleClick点击子菜单标题function({ key, domEvent })-

Menu.ItemGroup

参数说明类型默认值版本
children分组的菜单项MenuItem[]-
title分组标题ReactNode-

Menu.Divider

菜单项分割线,只用在弹出菜单内。