Menu导航菜单

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

何时使用

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

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

代码演示

Menu 导航菜单 - 图1

顶部导航

水平的顶部导航菜单。

  1. import { Menu, Icon } from 'choerodon-ui';
  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_outline" />
  19. Navigation One
  20. </Menu.Item>
  21. <Menu.Item key="app" disabled>
  22. <Icon type="apps" />
  23. Navigation Two
  24. </Menu.Item>
  25. <SubMenu
  26. title={
  27. <span>
  28. <Icon type="settings" />
  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
  44. href="https://choerodon.github.io/choerodon-ui/"
  45. target="_blank"
  46. rel="noopener noreferrer"
  47. >
  48. Navigation Four - Link
  49. </a>
  50. </Menu.Item>
  51. </Menu>
  52. );
  53. }
  54. }
  55. ReactDOM.render(<App />, mountNode);

Menu 导航菜单 - 图2

内嵌菜单

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

  1. import { Menu, Icon } from 'choerodon-ui';
  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_outline" />
  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="apps" />
  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="settings" />
  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 'choerodon-ui';
  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 ? 'folder_open' : 'folder'} />
  17. </Button>
  18. <Menu
  19. defaultSelectedKeys={['1']}
  20. defaultOpenKeys={['sub1']}
  21. mode="inline"
  22. inlineCollapsed={this.state.collapsed}
  23. >
  24. <Menu.Item key="1">
  25. <Icon type="pie_chart_outlined" />
  26. <span>Option 1</span>
  27. </Menu.Item>
  28. <Menu.Item key="2">
  29. <Icon type="desktop_windows" />
  30. <span>Option 2</span>
  31. </Menu.Item>
  32. <Menu.Item key="3">
  33. <Icon type="inbox" />
  34. <span>Option 3</span>
  35. </Menu.Item>
  36. <SubMenu
  37. key="sub1"
  38. title={
  39. <span>
  40. <Icon type="mail_outline" />
  41. <span>Navigation One</span>
  42. </span>
  43. }
  44. >
  45. <Menu.Item key="5">Option 5</Menu.Item>
  46. <Menu.Item key="6">Option 6</Menu.Item>
  47. <Menu.Item key="7">Option 7</Menu.Item>
  48. <Menu.Item key="8">Option 8</Menu.Item>
  49. </SubMenu>
  50. <SubMenu
  51. key="sub2"
  52. title={
  53. <span>
  54. <Icon type="apps" />
  55. <span>Navigation Two</span>
  56. </span>
  57. }
  58. >
  59. <Menu.Item key="9">Option 9</Menu.Item>
  60. <Menu.Item key="10">Option 10</Menu.Item>
  61. <SubMenu key="sub3" title="Submenu">
  62. <Menu.Item key="11">Option 11</Menu.Item>
  63. <Menu.Item key="12">Option 12</Menu.Item>
  64. </SubMenu>
  65. </SubMenu>
  66. </Menu>
  67. </div>
  68. );
  69. }
  70. }
  71. ReactDOM.render(<App />, mountNode);

Menu 导航菜单 - 图4

垂直菜单

子菜单是弹出的形式。

  1. import { Menu, Icon } from 'choerodon-ui';
  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_outline" />
  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="apps" />
  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="settings" />
  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 导航菜单 - 图5

主题

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

  1. import { Menu, Icon, Switch } from 'choerodon-ui';
  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 key="sub1" title={<span><Icon type="mail" /><span>Navigation One</span></span>}>
  39. <Menu.Item key="1">Option 1</Menu.Item>
  40. <Menu.Item key="2">Option 2</Menu.Item>
  41. <Menu.Item key="3">Option 3</Menu.Item>
  42. <Menu.Item key="4">Option 4</Menu.Item>
  43. </SubMenu>
  44. <SubMenu key="sub2" title={<span><Icon type="appstore" /><span>Navigtion Two</span></span>}>
  45. <Menu.Item key="5">Option 5</Menu.Item>
  46. <Menu.Item key="6">Option 6</Menu.Item>
  47. <SubMenu key="sub3" title="Submenu">
  48. <Menu.Item key="7">Option 7</Menu.Item>
  49. <Menu.Item key="8">Option 8</Menu.Item>
  50. </SubMenu>
  51. </SubMenu>
  52. <SubMenu key="sub4" title={<span><Icon type="setting" /><span>Navigation Three</span></span>}>
  53. <Menu.Item key="9">Option 9</Menu.Item>
  54. <Menu.Item key="10">Option 10</Menu.Item>
  55. <Menu.Item key="11">Option 11</Menu.Item>
  56. <Menu.Item key="12">Option 12</Menu.Item>
  57. </SubMenu>
  58. </Menu>
  59. </div>
  60. );
  61. }
  62. }
  63. ReactDOM.render(<Sider />, mountNode);

Menu 导航菜单 - 图6

切换菜单类型

展示动态切换模式。

  1. import { Menu, Icon, Switch } from 'choerodon-ui';
  2. const { SubMenu } = Menu;
  3. class Sider extends React.Component {
  4. state = {
  5. mode: 'inline',
  6. };
  7. changeMode = value => {
  8. this.setState({
  9. mode: value ? 'vertical' : 'inline',
  10. });
  11. };
  12. render() {
  13. return (
  14. <div>
  15. <Switch onChange={this.changeMode} /> Change Mode
  16. <span className="c7n-divider" style={{ margin: '0 1em' }} />
  17. <br />
  18. <br />
  19. <Menu
  20. style={{ width: 256 }}
  21. defaultSelectedKeys={['1']}
  22. defaultOpenKeys={['sub1']}
  23. mode={this.state.mode}
  24. >
  25. <Menu.Item key="1">
  26. <Icon type="mail_outline" />
  27. Navigation One
  28. </Menu.Item>
  29. <Menu.Item key="2">
  30. <Icon type="perm_contact_calendar" />
  31. Navigation Two
  32. </Menu.Item>
  33. <SubMenu
  34. key="sub1"
  35. title={
  36. <span>
  37. <Icon type="apps" />
  38. <span>Navigation Three</span>
  39. </span>
  40. }
  41. >
  42. <Menu.Item key="3">Option 3</Menu.Item>
  43. <Menu.Item key="4">Option 4</Menu.Item>
  44. <SubMenu key="sub1-2" title="Submenu">
  45. <Menu.Item key="5">Option 5</Menu.Item>
  46. <Menu.Item key="6">Option 6</Menu.Item>
  47. </SubMenu>
  48. </SubMenu>
  49. <SubMenu
  50. key="sub2"
  51. title={
  52. <span>
  53. <Icon type="settings" />
  54. <span>Navigation Four</span>
  55. </span>
  56. }
  57. >
  58. <Menu.Item key="7">Option 7</Menu.Item>
  59. <Menu.Item key="8">Option 8</Menu.Item>
  60. <Menu.Item key="9">Option 9</Menu.Item>
  61. <Menu.Item key="10">Option 10</Menu.Item>
  62. </SubMenu>
  63. </Menu>
  64. </div>
  65. );
  66. }
  67. }
  68. ReactDOM.render(<Sider />, mountNode);

Menu 导航菜单 - 图7

只展开当前父级菜单

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

  1. import { Menu, Icon } from 'choerodon-ui';
  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_outline" />
  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="apps" />
  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="settings" />
  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);

API

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

Menu

参数说明类型默认值
defaultOpenKeys初始展开的 SubMenu 菜单项 key 数组
defaultSelectedKeys初始选中的菜单项 key 数组string[]
forceSubMenuRender在子菜单展示之前就渲染进 DOMbooleanfalse
inlineCollapsedinline 时菜单是否收起状态boolean-
inlineIndentinline 模式的菜单缩进宽度number24
mode菜单类型,现在支持垂直、水平、和内嵌模式三种string: vertical vertical-right 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 })-
onDeselect取消选中时调用,仅在 multiple 生效function({ item, key, selectedKeys })-
onOpenChangeSubMenu 展开/关闭的回调function(openKeys: string[])noop
onSelect被选中时调用function({ item, key, selectedKeys })

More options in rc-menu

Menu.Item

参数说明类型默认值
disabled是否禁用booleanfalse
keyitem 的唯一标志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

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