Nav 导航
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
开发指南
何时使用
分为顶部导航和侧边导航,顶部导航提供全局性的类目和功能,侧边导航提供多级结构来收纳和排列网站架构。
可以替代原来的 Navigation 组件使用
注意事项
icononly 只适用垂直方向。
Nav所有事件都继承自Menu,请参考Menu的API。
API
导航
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
type | 导航类型可选值:'normal'(正常)'primary'(主要)'secondary'(次要)'text'(文字)'line'(线形) | Enum | 'normal' |
direction | 导航方向可选值:'hoz'(水平)'ver'(垂直) | Enum | 'ver' |
activeDirection | 设置组件选中状态的active边方向可选值:null(无)'top'(上)'bottom'(下)'left'(左)'right'(右) | Enum | - |
popupAlign | Tree 展开时候右侧子item的对齐方式可选值:'follow', 'outside' | Enum | 'follow' |
triggerType | PopupItem触发方式可选值:'click', 'hover' | Enum | - |
className | 自定义class | String | - |
iconOnly | 控制icon是否展示 | Boolean | - |
hasTooltip | 是否有ToolTIps(仅在iconOnly=true时生效) | Boolean | false |
hasArrow | 是否显示右侧的箭头(仅在iconOnly=true时生效) | Boolean | true |
Nav.Group
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
Nav.Item
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
icon | 自定义图标,可以使用Icon的type, 也可以使用组件<Icon type="your type"/> | String/ReactNode | - |
Nav.PopupItem
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
icon | 自定义图标,可以使用Icon的type, 也可以使用组件<Icon type="your type"/> | String/ReactNode | - |
Nav.SubNav
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
icon | 自定义图标,可以使用Icon的type, 也可以使用组件<Icon type="your type"/> | String/ReactNode | - |
代码示例
direction水平方向只支持top/bottom,垂直方向只支持left/right。activeDirection=top,bottom,left,right等待Menu支持
查看源码在线预览
import { Nav, Select, Switch, Field } from "@icedesign/base";
const { Item } = Nav;
class App extends React.Component {
field = new Field(this);
render() {
const { init, getValue } = this.field;
return (
<div>
<div className="demo-ctl">
<Switch
checkedChildren="横"
unCheckedChildren="竖"
{...init("direction")}
/>
<Select
placeholder="active 位置"
{...init("activeDirection", { initValue: "left" })}
>
<Option value="">无</Option>
<Option value="top">top</Option>
<Option value="bottom">bottom</Option>
<Option value="left">left</Option>
<Option value="right">right</Option>
</Select>
</div>
<br />
<Nav
style={{ width: getValue("direction") ? "100%" : "240px" }}
direction={getValue("direction") ? "hoz" : "ver"}
activeDirection={getValue("activeDirection")}
defaultSelectedKeys={["1"]}
>
<Item key="1" icon="service" hasSelectedIcon={false}>
Value Added Service
</Item>
<Item key="2" icon="training">
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5" icon="attachment">
Attachment
</Item>
<Item key="6" icon="electronics">
Electronics
</Item>
</Nav>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.demo-ctl {
background-color: #f1f1f1;
padding: 10.0px;
color: #0a7ac3;
border-left: 4.0px solid #0d599a;
}
.demo-ctl .next-switch {
margin-right: 20px;
}
横向导航
查看源码在线预览
import { Nav, Icon, Menu } from "@icedesign/base";
const { Item, PopupItem } = Nav;
ReactDOM.render(
<Nav direction="hoz" activeDirection="bottom">
<Item key="1" icon="service">
First
</Item>
<Item key="2">Training</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4">
<a href="http://www.taobao.com" target="_blank">
<Icon type="attachment" />Taobao
</a>
</Item>
<PopupItem key="5" icon="history" label="History">
<Menu>
<Menu.Item key="51">Option 1</Menu.Item>
<Menu.Item key="52">Option 3</Menu.Item>
<Menu.Item disabled key="54">
Option 2
</Menu.Item>
<Menu.Item key="53">Option 4</Menu.Item>
</Menu>
</PopupItem>
<PopupItem key="6" icon="electronics" label="Sub Nav">
<Menu>
<Menu.Item key="61">Option 1</Menu.Item>
<Menu.Item key="62">Option 3</Menu.Item>
<Menu.Item key="63">Option 4</Menu.Item>
<Menu.PopupItem key="64" label="popup">
<Menu>
<Menu.Item key="640">Option 1</Menu.Item>
<Menu.Item key="641">Option 3</Menu.Item>
<Menu.Item key="642">Option 4</Menu.Item>
</Menu>
</Menu.PopupItem>
</Menu>
</PopupItem>
</Nav>,
mountNode
);
group
查看源码在线预览
import { Nav } from "@icedesign/base";
const { Item, Group } = Nav;
ReactDOM.render(
<Nav style={{ width: 240 }}>
<Group label="Website List">
<Item key="1" icon="service">
Value Added Service
</Item>
<Item key="2" icon="training" disabled>
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5" icon="attachment">
Attachment
</Item>
<Item key="6" icon="electronics">
Electronics
</Item>
</Group>
</Nav>,
mountNode
);
iconOnly切换图标
查看源码在线预览
import { Nav, Switch, Field } from "@icedesign/base";
const { Item, SubNav } = Nav;
class App extends React.Component {
field = new Field(this);
render() {
const { init, getValue } = this.field;
return (
<div>
<Switch
{...init("iconOnly", { initValue: true, valueName: "checked" })}
/>
<br />
<Nav
iconOnly={getValue("iconOnly")}
hasTooltip
style={{ display: "inline-block" }}
>
<Item key="1" icon="service">
Value Added Service
</Item>
<Item key="2" icon="training">
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5" icon="attachment">
Attachment
</Item>
<Item key="6" icon="electronics">
Electronics
</Item>
<SubNav key="sub4" label=" Nav Three">
<Item key="9" icon="favorite">
Option 9
</Item>
<Item key="10" icon="favorite">
Option 10
</Item>
<Item key="11" icon="favorite">
Option 11
</Item>
<Item key="12" icon="favorite">
Option 12
</Item>
</SubNav>
</Nav>
<br />
无箭头(hover最后一个item)
<br />
<Nav
iconOnly={getValue("iconOnly")}
hasArrow={false}
hasTooltip
triggerType="hover"
style={{ display: "inline-block" }}
>
<Item key="1" icon="service">
Value Added Service
</Item>
<Item key="2" icon="training">
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5" icon="attachment">
Attachment
</Item>
<SubNav mode="popup" key="sub4" icon="electronics" label="Nav Three">
<Item key="9" icon="favorite">
Option 9
</Item>
<Item key="10" icon="favorite">
Option 10
</Item>
<Item key="11" icon="favorite">
Option 11
</Item>
<Item key="12" icon="favorite">
Option 12
</Item>
</SubNav>
</Nav>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
自定义导航头尾
查看源码在线预览
import { Nav, Icon, Switch } from "@icedesign/base";
const { Item } = Nav;
class App extends React.Component {
state = { direction: "hoz" };
onChange(v) {
this.setState({
direction: v ? "hoz" : "ver"
});
}
render() {
return (
<div>
<Switch
checkedChildren="横"
unCheckedChildren="竖"
defaultChecked
onChange={this.onChange.bind(this)}
/>
<br />
<br />
<div>
<Nav
direction={this.state.direction}
type="primary"
header={
<img
style={{ margin: "0 10px" }}
src="http://c.sg.ali-lazada.com/lazada/lib/0.0.1/image/lsc-logo.png"
/>
}
footer={
<div style={{ margin: "0 10px", lineHeight: "44px" }}>
<a href="javascript:;"> Login in</a>
</div>
}
>
<Item key="1" icon="service">
Value Added Service
</Item>
<Item key="2" icon="training">
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5">
<a href="http://www.taobao.com" target="_blank">
<Icon type="attachment" />Link
</a>
</Item>
<Item key="6" icon="electronics">
Electronics
</Item>
</Nav>
</div>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
outside: Nav顶端对齐。follow: Item顶端对齐当SubNav的mode=popup的时候,popAlign控制弹出菜单对齐方式
查看源码在线预览
import { Nav, Icon } from "@icedesign/base";
const { Item, SubNav } = Nav;
ReactDOM.render(
<div>
<Nav style={{ width: 240 }} popupAlign="outside" defaultOpenKeys={["sub2"]}>
<SubNav
key="sub1"
mode="popup"
label={
<span>
<Icon type="service" size="small" />
<span> Nav One</span>
</span>
}
>
<Item key="1">Option 1</Item>
<Item key="2">Option 2</Item>
<Item key="3">Option 3</Item>
<Item key="4">Option 4</Item>
</SubNav>
<SubNav
key="sub2"
mode="popup"
label={
<span>
<Icon type="training" size="small" />
<span> Nav Two</span>
</span>
}
>
<Item key="5">Option 5</Item>
<Item key="6">Option 6</Item>
</SubNav>
<SubNav
key="sub3"
mode="popup"
label={
<span>
<Icon type="favorite" size="small" />
<span> Nav Three</span>
</span>
}
>
<Item key="9">Option 9</Item>
<Item key="10">Option 10</Item>
<Item key="11">Option 11</Item>
<Item key="12">Option 12</Item>
</SubNav>
<SubNav
key="sub4"
mode="popup"
label={
<span>
<Icon type="favorite" size="small" />
<span> Nav Three</span>
</span>
}
>
<Item key="21">Option 9</Item>
<Item key="22">Option 10</Item>
<Item key="23">Option 11</Item>
<Item key="24">Option 12</Item>
</SubNav>
</Nav>
</div>,
mountNode
);
单例、多例模式
查看源码在线预览
import { Nav, Icon, Switch } from "@icedesign/base";
const { Item, SubNav } = Nav;
class App extends React.Component {
state = {
openmode: "single"
};
onChange(v) {
this.setState({
openmode: v ? "multiple" : "single"
});
}
render() {
return (
<div>
<Switch
checkedChildren="多"
unCheckedChildren="单"
onChange={this.onChange.bind(this)}
/>
<br />
<br />
<Nav
style={{ width: 240 }}
direction="ver"
openMode={this.state.openmode}
>
<SubNav
key="sub1"
label={
<span>
<Icon type="service" size="small" />
<span>Nav One</span>
</span>
}
>
<Item key="1">Option 1</Item>
<Item key="2">Option 2</Item>
<Item key="3">Option 3</Item>
<Item key="4">Option 4</Item>
</SubNav>
<SubNav
key="sub2"
label={
<span>
<Icon type="training" size="small" />
<a href="javascript:;">Nav Two</a>
</span>
}
>
<Item key="5">Option 5</Item>
<Item key="6">Option 6</Item>
<SubNav key="sub3" label="Submenu">
<Item key="7">
<a href="javascript:;">Option 7</a>
</Item>
<Item key="8">Option 8</Item>
</SubNav>
</SubNav>
<SubNav
key="sub4"
label={
<span>
<Icon type="favorite" size="small" />
<span> Nav Three</span>
</span>
}
>
<Item key="9">Option 9</Item>
<Item key="10">Option 10</Item>
<Item key="11">Option 11</Item>
<Item key="12">Option 12</Item>
</SubNav>
<Item key="13">Option 13</Item>
</Nav>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
type控制类别切换
查看源码在线预览
import { Nav, Switch } from "@icedesign/base";
const { Item } = Nav;
function renderNav(type, direction) {
return (
<div className={`demo-${direction}`}>
<h2>{type}</h2>
<Nav type={type} direction={direction}>
<Item key="1" icon="service">
Value Added Service
</Item>
<Item key="2" icon="training">
Training
</Item>
<Item key="3" icon="favorite">
Favorite
</Item>
<Item key="4" icon="history">
History
</Item>
<Item key="5" icon="attachment">
Attachment
</Item>
<Item key="6" icon="electronics">
Electronics
</Item>
</Nav>
</div>
);
}
class App extends React.Component {
state = { direction: "hoz" };
onChange(v) {
this.setState({
direction: v ? "hoz" : "ver"
});
}
render() {
return (
<div>
<Switch
checkedChildren="横"
unCheckedChildren="竖"
defaultChecked
onChange={this.onChange.bind(this)}
/>
<br />
<div style={{ display: "inline-block" }}>
{renderNav("primary", this.state.direction)}
{renderNav("secondary", this.state.direction)}
{renderNav("normal", this.state.direction)}
{renderNav("line", this.state.direction)}
{renderNav("text", this.state.direction)}
</div>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.demo-hoz {
width: 100%;
}
.demo-ver {
width: 200px;
display: inline-block;
float: left;
}
.demo-ver .next-nav{
margin-left: 5px;
}