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({
  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" icon={<MailOutlined />}>
  18. Navigation One
  19. </Menu.Item>
  20. <Menu.Item key="app" disabled icon={<AppstoreOutlined />}>
  21. Navigation Two
  22. </Menu.Item>
  23. <SubMenu icon={<SettingOutlined />} title="Navigation Three - Submenu">
  24. <Menu.ItemGroup title="Item 1">
  25. <Menu.Item key="setting:1">Option 1</Menu.Item>
  26. <Menu.Item key="setting:2">Option 2</Menu.Item>
  27. </Menu.ItemGroup>
  28. <Menu.ItemGroup title="Item 2">
  29. <Menu.Item key="setting:3">Option 3</Menu.Item>
  30. <Menu.Item key="setting:4">Option 4</Menu.Item>
  31. </Menu.ItemGroup>
  32. </SubMenu>
  33. <Menu.Item key="alipay">
  34. <a href="https://ant.design" target="_blank" rel="noopener noreferrer">
  35. Navigation Four - Link
  36. </a>
  37. </Menu.Item>
  38. </Menu>
  39. );
  40. }
  41. }
  42. 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
  18. key="sub1"
  19. title={
  20. <span>
  21. <MailOutlined />
  22. <span>Navigation One</span>
  23. </span>
  24. }
  25. >
  26. <Menu.ItemGroup key="g1" title="Item 1">
  27. <Menu.Item key="1">Option 1</Menu.Item>
  28. <Menu.Item key="2">Option 2</Menu.Item>
  29. </Menu.ItemGroup>
  30. <Menu.ItemGroup key="g2" title="Item 2">
  31. <Menu.Item key="3">Option 3</Menu.Item>
  32. <Menu.Item key="4">Option 4</Menu.Item>
  33. </Menu.ItemGroup>
  34. </SubMenu>
  35. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  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. <SettingOutlined />
  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. );
  59. }
  60. }
  61. 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. class Sider extends React.Component {
  5. // submenu keys of first level
  6. rootSubmenuKeys = ['sub1', 'sub2', 'sub4'];
  7. state = {
  8. openKeys: ['sub1'],
  9. };
  10. onOpenChange = openKeys => {
  11. const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
  12. if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  13. this.setState({ openKeys });
  14. } else {
  15. this.setState({
  16. openKeys: latestOpenKey ? [latestOpenKey] : [],
  17. });
  18. }
  19. };
  20. render() {
  21. return (
  22. <Menu
  23. mode="inline"
  24. openKeys={this.state.openKeys}
  25. onOpenChange={this.onOpenChange}
  26. style={{ width: 256 }}
  27. >
  28. <SubMenu
  29. key="sub1"
  30. title={
  31. <span>
  32. <MailOutlined />
  33. <span>Navigation One</span>
  34. </span>
  35. }
  36. >
  37. <Menu.Item key="1">Option 1</Menu.Item>
  38. <Menu.Item key="2">Option 2</Menu.Item>
  39. <Menu.Item key="3">Option 3</Menu.Item>
  40. <Menu.Item key="4">Option 4</Menu.Item>
  41. </SubMenu>
  42. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  43. <Menu.Item key="5">Option 5</Menu.Item>
  44. <Menu.Item key="6">Option 6</Menu.Item>
  45. <SubMenu key="sub3" title="Submenu">
  46. <Menu.Item key="7">Option 7</Menu.Item>
  47. <Menu.Item key="8">Option 8</Menu.Item>
  48. </SubMenu>
  49. </SubMenu>
  50. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  51. <Menu.Item key="9">Option 9</Menu.Item>
  52. <Menu.Item key="10">Option 10</Menu.Item>
  53. <Menu.Item key="11">Option 11</Menu.Item>
  54. <Menu.Item key="12">Option 12</Menu.Item>
  55. </SubMenu>
  56. </Menu>
  57. );
  58. }
  59. }
  60. 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
  20. key="sub2"
  21. title={
  22. <span>
  23. <AppstoreOutlined />
  24. <span>Navigation Two</span>
  25. </span>
  26. }
  27. >
  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. mountNode,
  43. );

Menu导航菜单 - 图6

主题

内建了两套主题 light|dark,默认 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" danger>
  44. Option 4
  45. </Menu.Item>
  46. </SubMenu>
  47. <SubMenu key="sub2" icon={<AppstoreOutlined />} title="Navigation Two">
  48. <Menu.Item key="5">Option 5</Menu.Item>
  49. <Menu.Item key="6">Option 6</Menu.Item>
  50. <SubMenu key="sub3" title="Submenu">
  51. <Menu.Item key="7">Option 7</Menu.Item>
  52. <Menu.Item key="8">Option 8</Menu.Item>
  53. </SubMenu>
  54. </SubMenu>
  55. <SubMenu key="sub4" icon={<SettingOutlined />} title="Navigation Three">
  56. <Menu.Item key="9">Option 9</Menu.Item>
  57. <Menu.Item key="10">Option 10</Menu.Item>
  58. <Menu.Item key="11">Option 11</Menu.Item>
  59. <Menu.Item key="12">Option 12</Menu.Item>
  60. </SubMenu>
  61. </Menu>
  62. </>
  63. );
  64. }
  65. }
  66. 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. class Sider extends React.Component {
  11. state = {
  12. mode: 'inline',
  13. theme: 'light',
  14. };
  15. changeMode = value => {
  16. this.setState({
  17. mode: value ? 'vertical' : 'inline',
  18. });
  19. };
  20. changeTheme = value => {
  21. this.setState({
  22. theme: value ? 'dark' : 'light',
  23. });
  24. };
  25. render() {
  26. return (
  27. <>
  28. <Switch onChange={this.changeMode} /> Change Mode
  29. <Divider type="vertical" />
  30. <Switch onChange={this.changeTheme} /> Change Style
  31. <br />
  32. <br />
  33. <Menu
  34. style={{ width: 256 }}
  35. defaultSelectedKeys={['1']}
  36. defaultOpenKeys={['sub1']}
  37. mode={this.state.mode}
  38. theme={this.state.theme}
  39. >
  40. <Menu.Item key="1" icon={<MailOutlined />}>
  41. Navigation One
  42. </Menu.Item>
  43. <Menu.Item key="2" icon={<CalendarOutlined />}>
  44. Navigation Two
  45. </Menu.Item>
  46. <SubMenu key="sub1" icon={<AppstoreOutlined />} title="Navigation Two">
  47. <Menu.Item key="3">Option 3</Menu.Item>
  48. <Menu.Item key="4">Option 4</Menu.Item>
  49. <SubMenu key="sub1-2" title="Submenu">
  50. <Menu.Item key="5">Option 5</Menu.Item>
  51. <Menu.Item key="6">Option 6</Menu.Item>
  52. </SubMenu>
  53. </SubMenu>
  54. <SubMenu key="sub2" icon={<SettingOutlined />} title="Navigation Three">
  55. <Menu.Item key="7">Option 7</Menu.Item>
  56. <Menu.Item key="8">Option 8</Menu.Item>
  57. <Menu.Item key="9">Option 9</Menu.Item>
  58. <Menu.Item key="10">Option 10</Menu.Item>
  59. </SubMenu>
  60. <Menu.Item key="link" icon={<LinkOutlined />}>
  61. <a href="https://ant.design" target="_blank" rel="noopener noreferrer">
  62. Ant Design
  63. </a>
  64. </Menu.Item>
  65. </Menu>
  66. </>
  67. );
  68. }
  69. }
  70. 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菜单类型,现在支持垂直、水平、和内嵌模式三种vertical | horizontal | inlinevertical
multiple是否允许多选booleanfalse
openKeys当前展开的 SubMenu 菜单项 key 数组string[]
selectable是否允许选中booleantrue
selectedKeys当前选中的菜单项 key 数组string[]
style根节点样式CSSProperties
subMenuCloseDelay用户鼠标离开子菜单后关闭延时,单位:秒number0.1
subMenuOpenDelay用户鼠标进入子菜单后开启延时,单位:秒number0
theme主题颜色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
icon菜单图标ReactNode4.2.0
danger展示错误状态样式booleanfalse4.3.0

注意: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

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

Menu.ItemGroup

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

Menu.Divider

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