Menu导航菜单

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

何时使用

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

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

代码演示

Menu 导航菜单 - 图1

顶部导航

水平的顶部导航菜单。

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

Menu 导航菜单 - 图2

内嵌菜单

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

  1. import { Menu, Icon } from 'antd';
  2. const SubMenu = Menu.SubMenu;
  3. const MenuItemGroup = Menu.ItemGroup;
  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
  18. key="sub1"
  19. title={
  20. <span>
  21. <Icon type="mail" />
  22. <span>Navigation One</span>
  23. </span>
  24. }
  25. >
  26. <MenuItemGroup key="g1" title="Item 1">
  27. <Menu.Item key="1">Option 1</Menu.Item>
  28. <Menu.Item key="2">Option 2</Menu.Item>
  29. </MenuItemGroup>
  30. <MenuItemGroup key="g2" title="Item 2">
  31. <Menu.Item key="3">Option 3</Menu.Item>
  32. <Menu.Item key="4">Option 4</Menu.Item>
  33. </MenuItemGroup>
  34. </SubMenu>
  35. <SubMenu
  36. key="sub2"
  37. title={
  38. <span>
  39. <Icon type="appstore" />
  40. <span>Navigation Two</span>
  41. </span>
  42. }
  43. >
  44. <Menu.Item key="5">Option 5</Menu.Item>
  45. <Menu.Item key="6">Option 6</Menu.Item>
  46. <SubMenu key="sub3" title="Submenu">
  47. <Menu.Item key="7">Option 7</Menu.Item>
  48. <Menu.Item key="8">Option 8</Menu.Item>
  49. </SubMenu>
  50. </SubMenu>
  51. <SubMenu
  52. key="sub4"
  53. title={
  54. <span>
  55. <Icon type="setting" />
  56. <span>Navigation Three</span>
  57. </span>
  58. }
  59. >
  60. <Menu.Item key="9">Option 9</Menu.Item>
  61. <Menu.Item key="10">Option 10</Menu.Item>
  62. <Menu.Item key="11">Option 11</Menu.Item>
  63. <Menu.Item key="12">Option 12</Menu.Item>
  64. </SubMenu>
  65. </Menu>
  66. );
  67. }
  68. }
  69. ReactDOM.render(<Sider />, mountNode);

Menu 导航菜单 - 图3

缩起内嵌菜单

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

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

  1. import { Menu, Icon, Button } from 'antd';
  2. const SubMenu = Menu.SubMenu;
  3. class App extends React.Component {
  4. state = {
  5. collapsed: false,
  6. };
  7. toggleCollapsed = () => {
  8. this.setState({
  9. collapsed: !this.state.collapsed,
  10. });
  11. };
  12. render() {
  13. return (
  14. <div style={{ width: 256 }}>
  15. <Button type="primary" onClick={this.toggleCollapsed} style={{ marginBottom: 16 }}>
  16. <Icon type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'} />
  17. </Button>
  18. <Menu
  19. defaultSelectedKeys={['1']}
  20. defaultOpenKeys={['sub1']}
  21. mode="inline"
  22. theme="dark"
  23. inlineCollapsed={this.state.collapsed}
  24. >
  25. <Menu.Item key="1">
  26. <Icon type="pie-chart" />
  27. <span>Option 1</span>
  28. </Menu.Item>
  29. <Menu.Item key="2">
  30. <Icon type="desktop" />
  31. <span>Option 2</span>
  32. </Menu.Item>
  33. <Menu.Item key="3">
  34. <Icon type="inbox" />
  35. <span>Option 3</span>
  36. </Menu.Item>
  37. <SubMenu
  38. key="sub1"
  39. title={
  40. <span>
  41. <Icon type="mail" />
  42. <span>Navigation One</span>
  43. </span>
  44. }
  45. >
  46. <Menu.Item key="5">Option 5</Menu.Item>
  47. <Menu.Item key="6">Option 6</Menu.Item>
  48. <Menu.Item key="7">Option 7</Menu.Item>
  49. <Menu.Item key="8">Option 8</Menu.Item>
  50. </SubMenu>
  51. <SubMenu
  52. key="sub2"
  53. title={
  54. <span>
  55. <Icon type="appstore" />
  56. <span>Navigation Two</span>
  57. </span>
  58. }
  59. >
  60. <Menu.Item key="9">Option 9</Menu.Item>
  61. <Menu.Item key="10">Option 10</Menu.Item>
  62. <SubMenu key="sub3" title="Submenu">
  63. <Menu.Item key="11">Option 11</Menu.Item>
  64. <Menu.Item key="12">Option 12</Menu.Item>
  65. </SubMenu>
  66. </SubMenu>
  67. </Menu>
  68. </div>
  69. );
  70. }
  71. }
  72. ReactDOM.render(<App />, mountNode);

Menu 导航菜单 - 图4

只展开当前父级菜单

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

  1. import { Menu, Icon } from 'antd';
  2. const SubMenu = Menu.SubMenu;
  3. class Sider extends React.Component {
  4. // submenu keys of first level
  5. rootSubmenuKeys = ['sub1', 'sub2', 'sub4'];
  6. state = {
  7. openKeys: ['sub1'],
  8. };
  9. onOpenChange = openKeys => {
  10. const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
  11. if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  12. this.setState({ openKeys });
  13. } else {
  14. this.setState({
  15. openKeys: latestOpenKey ? [latestOpenKey] : [],
  16. });
  17. }
  18. };
  19. render() {
  20. return (
  21. <Menu
  22. mode="inline"
  23. openKeys={this.state.openKeys}
  24. onOpenChange={this.onOpenChange}
  25. style={{ width: 256 }}
  26. >
  27. <SubMenu
  28. key="sub1"
  29. title={
  30. <span>
  31. <Icon type="mail" />
  32. <span>Navigation One</span>
  33. </span>
  34. }
  35. >
  36. <Menu.Item key="1">Option 1</Menu.Item>
  37. <Menu.Item key="2">Option 2</Menu.Item>
  38. <Menu.Item key="3">Option 3</Menu.Item>
  39. <Menu.Item key="4">Option 4</Menu.Item>
  40. </SubMenu>
  41. <SubMenu
  42. key="sub2"
  43. title={
  44. <span>
  45. <Icon type="appstore" />
  46. <span>Navigation Two</span>
  47. </span>
  48. }
  49. >
  50. <Menu.Item key="5">Option 5</Menu.Item>
  51. <Menu.Item key="6">Option 6</Menu.Item>
  52. <SubMenu key="sub3" title="Submenu">
  53. <Menu.Item key="7">Option 7</Menu.Item>
  54. <Menu.Item key="8">Option 8</Menu.Item>
  55. </SubMenu>
  56. </SubMenu>
  57. <SubMenu
  58. key="sub4"
  59. title={
  60. <span>
  61. <Icon type="setting" />
  62. <span>Navigation Three</span>
  63. </span>
  64. }
  65. >
  66. <Menu.Item key="9">Option 9</Menu.Item>
  67. <Menu.Item key="10">Option 10</Menu.Item>
  68. <Menu.Item key="11">Option 11</Menu.Item>
  69. <Menu.Item key="12">Option 12</Menu.Item>
  70. </SubMenu>
  71. </Menu>
  72. );
  73. }
  74. }
  75. ReactDOM.render(<Sider />, mountNode);

Menu 导航菜单 - 图5

垂直菜单

子菜单是弹出的形式。

  1. import { Menu, Icon } from 'antd';
  2. const SubMenu = Menu.SubMenu;
  3. const MenuItemGroup = Menu.ItemGroup;
  4. function handleClick(e) {
  5. console.log('click', e);
  6. }
  7. ReactDOM.render(
  8. <Menu onClick={handleClick} style={{ width: 256 }} mode="vertical">
  9. <SubMenu
  10. key="sub1"
  11. title={
  12. <span>
  13. <Icon type="mail" />
  14. <span>Navigation One</span>
  15. </span>
  16. }
  17. >
  18. <MenuItemGroup title="Item 1">
  19. <Menu.Item key="1">Option 1</Menu.Item>
  20. <Menu.Item key="2">Option 2</Menu.Item>
  21. </MenuItemGroup>
  22. <MenuItemGroup title="Iteom 2">
  23. <Menu.Item key="3">Option 3</Menu.Item>
  24. <Menu.Item key="4">Option 4</Menu.Item>
  25. </MenuItemGroup>
  26. </SubMenu>
  27. <SubMenu
  28. key="sub2"
  29. title={
  30. <span>
  31. <Icon type="appstore" />
  32. <span>Navigation Two</span>
  33. </span>
  34. }
  35. >
  36. <Menu.Item key="5">Option 5</Menu.Item>
  37. <Menu.Item key="6">Option 6</Menu.Item>
  38. <SubMenu key="sub3" title="Submenu">
  39. <Menu.Item key="7">Option 7</Menu.Item>
  40. <Menu.Item key="8">Option 8</Menu.Item>
  41. </SubMenu>
  42. </SubMenu>
  43. <SubMenu
  44. key="sub4"
  45. title={
  46. <span>
  47. <Icon type="setting" />
  48. <span>Navigation Three</span>
  49. </span>
  50. }
  51. >
  52. <Menu.Item key="9">Option 9</Menu.Item>
  53. <Menu.Item key="10">Option 10</Menu.Item>
  54. <Menu.Item key="11">Option 11</Menu.Item>
  55. <Menu.Item key="12">Option 12</Menu.Item>
  56. </SubMenu>
  57. </Menu>,
  58. mountNode,
  59. );

Menu 导航菜单 - 图6

主题

内建了两套主题 light|dark,默认 light

  1. import { Menu, Icon, Switch } from 'antd';
  2. const SubMenu = Menu.SubMenu;
  3. class Sider extends React.Component {
  4. state = {
  5. theme: 'dark',
  6. current: '1',
  7. };
  8. changeTheme = value => {
  9. this.setState({
  10. theme: value ? 'dark' : 'light',
  11. });
  12. };
  13. handleClick = e => {
  14. console.log('click ', e);
  15. this.setState({
  16. current: e.key,
  17. });
  18. };
  19. render() {
  20. return (
  21. <div>
  22. <Switch
  23. checked={this.state.theme === 'dark'}
  24. onChange={this.changeTheme}
  25. checkedChildren="Dark"
  26. unCheckedChildren="Light"
  27. />
  28. <br />
  29. <br />
  30. <Menu
  31. theme={this.state.theme}
  32. onClick={this.handleClick}
  33. style={{ width: 256 }}
  34. defaultOpenKeys={['sub1']}
  35. selectedKeys={[this.state.current]}
  36. mode="inline"
  37. >
  38. <SubMenu
  39. key="sub1"
  40. title={
  41. <span>
  42. <Icon type="mail" />
  43. <span>Navigation One</span>
  44. </span>
  45. }
  46. >
  47. <Menu.Item key="1">Option 1</Menu.Item>
  48. <Menu.Item key="2">Option 2</Menu.Item>
  49. <Menu.Item key="3">Option 3</Menu.Item>
  50. <Menu.Item key="4">Option 4</Menu.Item>
  51. </SubMenu>
  52. <SubMenu
  53. key="sub2"
  54. title={
  55. <span>
  56. <Icon type="appstore" />
  57. <span>Navigtion Two</span>
  58. </span>
  59. }
  60. >
  61. <Menu.Item key="5">Option 5</Menu.Item>
  62. <Menu.Item key="6">Option 6</Menu.Item>
  63. <SubMenu key="sub3" title="Submenu">
  64. <Menu.Item key="7">Option 7</Menu.Item>
  65. <Menu.Item key="8">Option 8</Menu.Item>
  66. </SubMenu>
  67. </SubMenu>
  68. <SubMenu
  69. key="sub4"
  70. title={
  71. <span>
  72. <Icon type="setting" />
  73. <span>Navigation Three</span>
  74. </span>
  75. }
  76. >
  77. <Menu.Item key="9">Option 9</Menu.Item>
  78. <Menu.Item key="10">Option 10</Menu.Item>
  79. <Menu.Item key="11">Option 11</Menu.Item>
  80. <Menu.Item key="12">Option 12</Menu.Item>
  81. </SubMenu>
  82. </Menu>
  83. </div>
  84. );
  85. }
  86. }
  87. ReactDOM.render(<Sider />, mountNode);

Menu 导航菜单 - 图7

切换菜单类型

展示动态切换模式。

  1. import { Menu, Icon, Switch } from 'antd';
  2. const { SubMenu } = Menu;
  3. class Sider extends React.Component {
  4. state = {
  5. mode: 'inline',
  6. theme: 'light',
  7. };
  8. changeMode = value => {
  9. this.setState({
  10. mode: value ? 'vertical' : 'inline',
  11. });
  12. };
  13. changeTheme = value => {
  14. this.setState({
  15. theme: value ? 'dark' : 'light',
  16. });
  17. };
  18. render() {
  19. return (
  20. <div>
  21. <Switch onChange={this.changeMode} /> Change Mode
  22. <span className="ant-divider" style={{ margin: '0 1em' }} />
  23. <Switch onChange={this.changeTheme} /> Change Theme
  24. <br />
  25. <br />
  26. <Menu
  27. style={{ width: 256 }}
  28. defaultSelectedKeys={['1']}
  29. defaultOpenKeys={['sub1']}
  30. mode={this.state.mode}
  31. theme={this.state.theme}
  32. >
  33. <Menu.Item key="1">
  34. <Icon type="mail" />
  35. Navigation One
  36. </Menu.Item>
  37. <Menu.Item key="2">
  38. <Icon type="calendar" />
  39. Navigation Two
  40. </Menu.Item>
  41. <SubMenu
  42. key="sub1"
  43. title={
  44. <span>
  45. <Icon type="appstore" />
  46. <span>Navigation Three</span>
  47. </span>
  48. }
  49. >
  50. <Menu.Item key="3">Option 3</Menu.Item>
  51. <Menu.Item key="4">Option 4</Menu.Item>
  52. <SubMenu key="sub1-2" title="Submenu">
  53. <Menu.Item key="5">Option 5</Menu.Item>
  54. <Menu.Item key="6">Option 6</Menu.Item>
  55. </SubMenu>
  56. </SubMenu>
  57. <SubMenu
  58. key="sub2"
  59. title={
  60. <span>
  61. <Icon type="setting" />
  62. <span>Navigation Four</span>
  63. </span>
  64. }
  65. >
  66. <Menu.Item key="7">Option 7</Menu.Item>
  67. <Menu.Item key="8">Option 8</Menu.Item>
  68. <Menu.Item key="9">Option 9</Menu.Item>
  69. <Menu.Item key="10">Option 10</Menu.Item>
  70. </SubMenu>
  71. </Menu>
  72. </div>
  73. );
  74. }
  75. }
  76. ReactDOM.render(<Sider />, 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[]
forceSubMenuRender在子菜单展示之前就渲染进 DOMbooleanfalse
inlineCollapsedinline 时菜单是否收起状态boolean-
inlineIndentinline 模式的菜单缩进宽度number24
mode菜单类型,现在支持垂直、水平、和内嵌模式三种string: vertical horizontal inlinevertical
multiple是否允许多选booleanfalse
openKeys当前展开的 SubMenu 菜单项 key 数组string[]
selectable是否允许选中booleantrue
selectedKeys当前选中的菜单项 key 数组string[]
style根节点样式object
subMenuCloseDelay用户鼠标离开子菜单后关闭延时,单位:秒number0.1
subMenuOpenDelay用户鼠标进入子菜单后开启延时,单位:秒number0
theme主题颜色string: light darklight
onClick点击 MenuItem 调用此函数function({ item, key, keyPath, domEvent })-
onDeselect取消选中时调用,仅在 multiple 生效function({ item, key, keyPath, selectedKeys, domEvent })-
onOpenChangeSubMenu 展开/关闭的回调function(openKeys: string[])noop
onSelect被选中时调用function({ item, key, keyPath, selectedKeys, domEvent })
overflowedIndicator自定义 Menu 折叠时的图标ReactNode-

More options in rc-menu

Menu.Item

参数说明类型默认值
disabled是否禁用booleanfalse
keyitem 的唯一标志string
title设置收缩时展示的悬浮标题string

Menu.SubMenu

参数说明类型默认值
children子菜单的菜单项Array<MenuItem|SubMenu>
disabled是否禁用booleanfalse
key唯一标志string
title子菜单项值string|ReactNode
onTitleClick点击子菜单标题function({ key, domEvent })

Menu.ItemGroup

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

Menu.Divider

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