Menu 导航菜单

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

何时使用

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

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

代码演示

顶部导航

水平的顶部导航菜单。

Menu导航菜单 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. const MenuItemGroup = Menu.ItemGroup;
  6. class App extends React.Component {
  7. state = {
  8. current: 'mail',
  9. };
  10. handleClick = e => {
  11. console.log('click ', e);
  12. this.setState({
  13. current: e.key,
  14. });
  15. };
  16. render() {
  17. return (
  18. <Menu onClick={this.handleClick} selectedKeys={[this.state.current]} mode="horizontal">
  19. <Menu.Item key="mail">
  20. <Icon type="mail_outline" />
  21. Navigation One
  22. </Menu.Item>
  23. <Menu.Item key="app" disabled>
  24. <Icon type="apps" />
  25. Navigation Two
  26. </Menu.Item>

内嵌菜单

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

Menu导航菜单 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. const MenuItemGroup = Menu.ItemGroup;
  6. class Sider extends React.Component {
  7. handleClick = e => {
  8. console.log('click ', e);
  9. };
  10. render() {
  11. return (
  12. <Menu
  13. onClick={this.handleClick}
  14. style={{ width: 256 }}
  15. defaultSelectedKeys={['1']}
  16. defaultOpenKeys={['sub1']}
  17. mode="inline"
  18. >
  19. <SubMenu
  20. key="sub1"
  21. title={
  22. <span>
  23. <Icon type="mail_outline" />
  24. <span>Navigation One</span>
  25. </span>
  26. }
  27. >
  28. <MenuItemGroup key="g1" title="Item 1">
  29. <Menu.Item key="1">Option 1</Menu.Item>
  30. <Menu.Item key="2">Option 2</Menu.Item>
  31. </MenuItemGroup>
  32. <MenuItemGroup key="g2" title="Item 2">
  33. <Menu.Item key="3">Option 3</Menu.Item>
  34. <Menu.Item key="4">Option 4</Menu.Item>
  35. </MenuItemGroup>
  36. </SubMenu>
  37. <SubMenu
  38. key="sub2"
  39. title={
  40. <span>
  41. <Icon type="apps" />
  42. <span>Navigation Two</span>

缩起内嵌菜单

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

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

Menu导航菜单 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon, Button } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. class App extends React.Component {
  6. state = {
  7. collapsed: false,
  8. };
  9. toggleCollapsed = () => {
  10. this.setState({
  11. collapsed: !this.state.collapsed,
  12. });
  13. };
  14. render() {
  15. return (
  16. <div style={{ width: 256 }}>
  17. <Button type="primary" onClick={this.toggleCollapsed} style={{ marginBottom: 16 }}>
  18. <Icon type={this.state.collapsed ? 'folder_open' : 'folder'} />
  19. </Button>
  20. <Menu
  21. defaultSelectedKeys={['1']}
  22. defaultOpenKeys={['sub1']}
  23. mode="inline"
  24. inlineCollapsed={this.state.collapsed}
  25. >
  26. <Menu.Item key="1">
  27. <Icon type="pie_chart_outlined" />
  28. <span>Option 1</span>
  29. </Menu.Item>
  30. <Menu.Item key="2">
  31. <Icon type="desktop_windows" />
  32. <span>Option 2</span>
  33. </Menu.Item>
  34. <Menu.Item key="3">
  35. <Icon type="inbox" />
  36. <span>Option 3</span>
  37. </Menu.Item>
  38. <SubMenu
  39. key="sub1"
  40. title={
  41. <span>
  42. <Icon type="mail_outline" />
  43. <span>Navigation One</span>
  44. </span>

垂直菜单

子菜单是弹出的形式。

Menu导航菜单 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. const MenuItemGroup = Menu.ItemGroup;
  6. function handleClick(e) {
  7. console.log('click', e);
  8. }
  9. ReactDOM.render(
  10. <Menu onClick={handleClick} style={{ width: 256 }} mode="vertical">
  11. <SubMenu
  12. key="sub1"
  13. title={
  14. <span>
  15. <Icon type="mail_outline" />
  16. <span>Navigation One</span>
  17. </span>
  18. }
  19. >
  20. <MenuItemGroup title="Item 1">
  21. <Menu.Item key="1">Option 1</Menu.Item>
  22. <Menu.Item key="2">Option 2</Menu.Item>
  23. </MenuItemGroup>
  24. <MenuItemGroup title="Iteom 2">
  25. <Menu.Item key="3">Option 3</Menu.Item>
  26. <Menu.Item key="4">Option 4</Menu.Item>
  27. </MenuItemGroup>
  28. </SubMenu>
  29. <SubMenu
  30. key="sub2"

主题

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

Menu导航菜单 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon, Switch } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. class Sider extends React.Component {
  6. state = {
  7. theme: 'dark',
  8. current: '1',
  9. }
  10. changeTheme = (value) => {
  11. this.setState({
  12. theme: value ? 'dark' : 'light',
  13. });
  14. }
  15. handleClick = (e) => {
  16. console.log('click ', e);
  17. this.setState({
  18. current: e.key,
  19. });
  20. }
  21. render() {
  22. return (
  23. <div>
  24. <Switch
  25. checked={this.state.theme === 'dark'}
  26. onChange={this.changeTheme}
  27. checkedChildren="Dark"
  28. unCheckedChildren="Light"
  29. />
  30. <br />
  31. <br />
  32. <Menu
  33. theme={this.state.theme}
  34. onClick={this.handleClick}
  35. style={{ width: 256 }}
  36. defaultOpenKeys={['sub1']}
  37. selectedKeys={[this.state.current]}
  38. mode="inline"

切换菜单类型

展示动态切换模式。

Menu导航菜单 - 图6

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon, Switch } from 'choerodon-ui';
  4. const { SubMenu } = Menu;
  5. class Sider extends React.Component {
  6. state = {
  7. mode: 'inline',
  8. };
  9. changeMode = value => {
  10. this.setState({
  11. mode: value ? 'vertical' : 'inline',
  12. });
  13. };
  14. render() {
  15. return (
  16. <div>
  17. <Switch onChange={this.changeMode} /> Change Mode
  18. <span className="c7n-divider" style={{ margin: '0 1em' }} />
  19. <br />
  20. <br />
  21. <Menu
  22. style={{ width: 256 }}
  23. defaultSelectedKeys={['1']}
  24. defaultOpenKeys={['sub1']}
  25. mode={this.state.mode}
  26. >
  27. <Menu.Item key="1">
  28. <Icon type="mail_outline" />
  29. Navigation One
  30. </Menu.Item>
  31. <Menu.Item key="2">
  32. <Icon type="perm_contact_calendar" />
  33. Navigation Two
  34. </Menu.Item>
  35. <SubMenu
  36. key="sub1"
  37. title={
  38. <span>
  39. <Icon type="apps" />

只展开当前父级菜单

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

Menu导航菜单 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Menu, Icon } from 'choerodon-ui';
  4. const SubMenu = Menu.SubMenu;
  5. class Sider extends React.Component {
  6. // submenu keys of first level
  7. rootSubmenuKeys = ['sub1', 'sub2', 'sub4'];
  8. state = {
  9. openKeys: ['sub1'],
  10. };
  11. onOpenChange = openKeys => {
  12. const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1);
  13. if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
  14. this.setState({ openKeys });
  15. } else {
  16. this.setState({
  17. openKeys: latestOpenKey ? [latestOpenKey] : [],
  18. });
  19. }
  20. };
  21. render() {
  22. return (
  23. <Menu
  24. mode="inline"
  25. openKeys={this.state.openKeys}
  26. onOpenChange={this.onOpenChange}
  27. style={{ width: 256 }}
  28. >
  29. <SubMenu
  30. key="sub1"
  31. title={
  32. <span>
  33. <Icon type="mail_outline" />
  34. <span>Navigation One</span>
  35. </span>
  36. }

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

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