Menu 导航菜单
为页面和功能提供导航的菜单列表。
何时使用#
导航菜单是一个网站的灵魂,用户依赖导航在各个页面中进行跳转。一般分为顶部导航和侧边导航,顶部导航提供全局性的类目和功能,侧边导航提供多级结构来收纳和排列网站架构。
更多布局和导航的范例可以参考:常用布局。
<Menu>
<MenuItem>菜单项</MenuItem>
<SubMenu title="子菜单">
<MenuItem>子菜单项</MenuItem>
</SubMenu>
</Menu>
API#
参数 |
说明 |
类型 |
默认值 |
theme |
主题颜色 |
enum: light dark |
'light' |
mode |
菜单类型 |
enum: vertical horizontal inline |
vertical |
selectedKeys |
当前选中的菜单项 key 数组 |
|
|
defaultSelectedKeys |
初始选中的菜单项 key 数组 |
|
|
openKeys |
当前展开的菜单项 key 数组 |
|
|
defaultOpenKeys |
初始展开的菜单项 key 数组 |
|
|
onSelect |
被选中时调用,参数 {item, key, selectedKeys} 对象 |
function |
无 |
onDeselect |
取消选中时调用,参数 {item, key, selectedKeys} 对象,仅在 multiple 生效 |
function |
无 |
onClick |
点击 menuitem 调用此函数,参数为 {item, key} |
function |
无 |
style |
根节点样式 |
object |
|
参数 |
说明 |
类型 |
默认值 |
disabled |
是否禁用 |
Boolean |
false |
key |
item 的唯一标志 |
String |
|
参数 |
说明 |
类型 |
默认值 |
title |
子菜单项值 |
String or React.Element |
|
children |
子菜单的菜单项 |
(MenuItem or SubMenu)[] |
|
参数 |
说明 |
类型 |
默认值 |
title |
分组标题 |
String or React.Element |
|
children |
分组的菜单项 |
MenuItem[] |
|
代码演示
import { Menu, Icon } from 'antd';
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
const App = React.createClass({
getInitialState() {
return {
current: 'mail'
};
},
handleClick(e) {
console.log('click ', e);
this.setState({
current: e.key
});
},
render() {
return (
<Menu onClick={this.handleClick}
selectedKeys={[this.state.current]}
theme={this.state.theme}
mode="horizontal">
<Menu.Item key="mail">
<Icon type="mail" />导航一
</Menu.Item>
<Menu.Item key="app" disabled>
<Icon type="appstore" />导航二
</Menu.Item>
<SubMenu title={<span><Icon type="setting" />导航 - 子菜单</span>}>
<MenuItemGroup title="分组1">
<Menu.Item key="setting:1">选项1</Menu.Item>
<Menu.Item key="setting:2">选项2</Menu.Item>
</MenuItemGroup>
<MenuItemGroup title="分组2">
<Menu.Item key="setting:3">选项3</Menu.Item>
<Menu.Item key="setting:4">选项4</Menu.Item>
</MenuItemGroup>
</SubMenu>
<Menu.Item key="alipay">
<a href="http://www.alipay.com/" target="_blank">导航四 - 链接</a>
</Menu.Item>
</Menu>
);
}
});
ReactDOM.render(<App />, mountNode);
import { Menu, Icon } from 'antd';
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
const Sider = React.createClass({
getInitialState() {
return {
current: '1'
};
},
handleClick(e) {
console.log('click ', e);
this.setState({
current: e.key
});
},
render() {
return <Menu onClick={this.handleClick}
style={{width:240}}
defaultOpenKeys={['sub1']}
selectedKeys={[this.state.current]}
mode="inline">
<SubMenu key="sub1" title={<span><Icon type="mail" /><span>导航一</span></span>}>
<MenuItemGroup title="分组1">
<Menu.Item key="1">选项1</Menu.Item>
<Menu.Item key="2">选项2</Menu.Item>
</MenuItemGroup>
<MenuItemGroup title="分组2">
<Menu.Item key="3"><a href="#">选项3</a></Menu.Item>
<Menu.Item key="4">选项4</Menu.Item>
</MenuItemGroup>
</SubMenu>
<SubMenu key="sub2" title={<span><Icon type="appstore" /><span>导航二</span></span>}>
<Menu.Item key="5">选项5</Menu.Item>
<Menu.Item key="6">选项6</Menu.Item>
<SubMenu key="sub3" title="三级导航">
<Menu.Item key="7">选项7</Menu.Item>
<Menu.Item key="8">选项8</Menu.Item>
</SubMenu>
</SubMenu>
<SubMenu key="sub4" title={<span><Icon type="setting" /><span>导航三</span></span>}>
<Menu.Item key="9">选项9</Menu.Item>
<Menu.Item key="10">选项10</Menu.Item>
<Menu.Item key="11">选项11</Menu.Item>
<Menu.Item key="12">选项12</Menu.Item>
</SubMenu>
</Menu>;
}
});
ReactDOM.render(<Sider />, mountNode);
import { Menu, Icon } from 'antd';
const SubMenu = Menu.SubMenu;
const Sider = React.createClass({
getInitialState() {
return {
current: '1',
openKeys: []
};
},
handleClick(e) {
console.log('click ', e);
this.setState({
current: e.key,
openKeys: e.keyPath.slice(1)
});
},
onToggle(info) {
this.setState({
openKeys: info.open ? info.keyPath : info.keyPath.slice(1)
});
},
render() {
return <Menu onClick={this.handleClick}
style={{width:240}}
openKeys={this.state.openKeys}
onOpen={this.onToggle}
onClose={this.onToggle}
selectedKeys={[this.state.current]}
mode="inline">
<SubMenu key="sub1" title={<span><Icon type="mail" /><span>导航一</span></span>}>
<Menu.Item key="1">选项1</Menu.Item>
<Menu.Item key="2">选项2</Menu.Item>
<Menu.Item key="3">选项3</Menu.Item>
<Menu.Item key="4">选项4</Menu.Item>
</SubMenu>
<SubMenu key="sub2" title={<span><Icon type="appstore" /><span>导航二</span></span>}>
<Menu.Item key="5">选项5</Menu.Item>
<Menu.Item key="6">选项6</Menu.Item>
<SubMenu key="sub3" title="三级导航">
<Menu.Item key="7">选项7</Menu.Item>
<Menu.Item key="8">选项8</Menu.Item>
</SubMenu>
</SubMenu>
<SubMenu key="sub4" title={<span><Icon type="setting" /><span>导航三</span></span>}>
<Menu.Item key="9">选项9</Menu.Item>
<Menu.Item key="10">选项10</Menu.Item>
<Menu.Item key="11">选项11</Menu.Item>
<Menu.Item key="12">选项12</Menu.Item>
</SubMenu>
</Menu>;
}
});
ReactDOM.render(<Sider />, mountNode);
import { Menu, Icon } from 'antd';
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
function handleClick(e) {
console.log('click', e);
}
ReactDOM.render(<Menu onClick={handleClick} style={{width:240}} mode="vertical">
<SubMenu key="sub1" title={<span><Icon type="mail" /><span>导航一</span></span>}>
<MenuItemGroup title="分组1">
<Menu.Item key="1">选项1</Menu.Item>
<Menu.Item key="2">选项2</Menu.Item>
</MenuItemGroup>
<MenuItemGroup title="分组2">
<Menu.Item key="3">选项3</Menu.Item>
<Menu.Item key="4">选项4</Menu.Item>
</MenuItemGroup>
</SubMenu>
<SubMenu key="sub2" title={<span><Icon type="appstore" /><span>导航二</span></span>}>
<Menu.Item key="5">选项5</Menu.Item>
<Menu.Item key="6">选项6</Menu.Item>
<SubMenu key="sub3" title="三级导航">
<Menu.Item key="7">选项7</Menu.Item>
<Menu.Item key="8">选项8</Menu.Item>
</SubMenu>
</SubMenu>
<SubMenu key="sub4" title={<span><icon type="setting" /><span>导航三</span></span>}>
<Menu.Item key="9">选项9</Menu.Item>
<Menu.Item key="10">选项10</Menu.Item>
<Menu.Item key="11">选项11</Menu.Item>
<Menu.Item key="12">选项12</Menu.Item>
</SubMenu>
</Menu>
, mountNode);
import { Menu, Icon, Switch } from 'antd';
const SubMenu = Menu.SubMenu;
const Sider = React.createClass({
getInitialState() {
return {
theme: 'light'
};
},
changeTheme(value) {
this.setState({
theme: value ? 'dark' : 'light'
});
},
handleClick(e) {
console.log('click ', e);
this.setState({
current: e.key
});
},
render() {
return <div>
<Switch onChange={this.changeTheme} checkedChildren="暗" unCheckedChildren="亮" />
<br />
<br />
<Menu theme={this.state.theme}
onClick={this.handleClick}
style={{width:240}}
defaultOpenKeys={['sub1']}
selectedKeys={[this.state.current]}
mode="inline">
<SubMenu key="sub1" title={<span><Icon type="mail" /><span>导航一</span></span>}>
<Menu.Item key="1">选项1</Menu.Item>
<Menu.Item key="2">选项2</Menu.Item>
<Menu.Item key="3">选项3</Menu.Item>
<Menu.Item key="4">选项4</Menu.Item>
</SubMenu>
<SubMenu key="sub2" title={<span><Icon type="appstore" /><span>导航二</span></span>}>
<Menu.Item key="5">选项5</Menu.Item>
<Menu.Item key="6">选项6</Menu.Item>
<SubMenu key="sub3" title="三级导航">
<Menu.Item key="7">选项7</Menu.Item>
<Menu.Item key="8">选项8</Menu.Item>
</SubMenu>
</SubMenu>
<SubMenu key="sub4" title={<span><Icon type="setting" /><span>导航三</span></span>}>
<Menu.Item key="9">选项9</Menu.Item>
<Menu.Item key="10">选项10</Menu.Item>
<Menu.Item key="11">选项11</Menu.Item>
<Menu.Item key="12">选项12</Menu.Item>
</SubMenu>
</Menu>
</div>;
}
});
ReactDOM.render(<Sider />, mountNode);