Navigation 导航(废弃)
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
注意
Navigation已经废弃, 会在1.0版本中去除, 建议切换到Nav。
代码示例
设置选中状态的方向,建议横向导航设置为top或bottom,纵向设置为left或right
查看源码在线预览
import { Navigation, Select } from "@icedesign/base";
const { Item } = Navigation;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
activeDirection: "top",
type: "filling"
};
}
changePosition(value) {
this.setState({
activeDirection: value,
type: value === "top" || value === "bottom" ? "filling" : "normal"
});
}
render() {
return (
<div>
<Select
value={this.state.activeDirection}
onChange={::this.changePosition}
className="tab-select-demo"
>
<div value="left">left</div>
<div value="top">top</div>
<div value="bottom">bottom</div>
<div value="right">right</div>
</Select>
<Navigation
type={this.state.type}
activeDirection={this.state.activeDirection}
>
<Item itemid="1" text="Value Added Service" icon="service" />
<Item itemid="2" text="Training" icon="training" />
<Item itemid="3" text="Favorite" icon="favorite" />
<Item itemid="4" text="History" icon="history" />
<Item itemid="5" text="Attachment" icon="attachment" />
</Navigation>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
最简单的用法.
查看源码在线预览
import { Navigation, Menu } from "@icedesign/base";
const { Item } = Navigation;
let onMouseEnter = () => {
console.log("onMouseEnter");
};
let onMouseLeave = () => {
console.log("onMouseLeave");
};
let onClick = (itemid, item) => {
console.log("Click:" + item.props.text);
};
let onSelect = (itemid, item) => {
console.log("Select:" + item.props.text);
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
trigger: "hover"
};
}
onToggleTrigger() {
this.setState({
trigger: this.state.trigger === "hover" ? "click" : "hover"
});
}
render() {
return (
<div>
<button
id="changeTriggerType"
onClick={this.onToggleTrigger.bind(this)}
>
点击切换trigger方式
</button>
<Navigation
onClick={onClick}
onSelect={onSelect}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
trigger={this.state.trigger}
>
<Item itemid="1" text={<i>Value Added Service</i>}>
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="2" text="Training">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="3" text="Favorite">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="4" text="History">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="5" text="Attachment">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="6" text="electronics">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
</Navigation>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;
查看源码在线预览
import { Navigation, Menu } from "@icedesign/base";
const { Item } = Navigation;
let onMouseEnter = () => {
console.log("onMouseEnter");
};
let onMouseLeave = () => {
console.log("onMouseLeave");
};
class CustomItem extends React.Component {
render() {
const { link } = this.props;
return <Item content={link} />;
}
}
ReactDOM.render(
<Navigation
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
type="filling"
trigger="hover"
activeDirection="left"
morePositionKey="3"
moreText="+ More"
onSelect={id => console.log(id)}
>
<div>
<Item text="TestInnerItem" />
</div>
<CustomItem link={<a href="#test">test</a>} />
<Item itemid="2" text="Training" icon="training" title="Training Long Long">
<Menu>
<Menu.Item key="11">OptionOptionOptionOptionOption 1</Menu.Item>
<Menu.Item disabled key="21">
Option 2
</Menu.Item>
<Menu.Item key="31">Option 3</Menu.Item>
<Menu.Item key="41">Option 4</Menu.Item>
<Menu.SubMenu key="411" label="xxx" mode="popup" triggerType="hover">
<Menu.Item key="xxx">Option2</Menu.Item>
<Menu.Item key="xxx2">Option3</Menu.Item>
</Menu.SubMenu>
</Menu>
</Item>
<Item itemid="3" text="Favorite" icon="favorite" selected="true">
<Menu>
<Menu.SubMenu label="xxx" key="popup1">
<Menu.Item disabled key="112">
abc
</Menu.Item>
<Menu.Item key="222">abc</Menu.Item>
</Menu.SubMenu>
</Menu>
</Item>
<Item itemid="4" text="History" icon="history">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="5" text="Attachment" icon="attachment">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
<Item itemid="6" text="electronics" icon="Electronics">
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item disabled key="2">
Option 2
</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
</Menu>
</Item>
</Navigation>,
mountNode
);
最简单的用法。
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
let onClick = (itemid, item) => {
console.log("Click:" + item.props.icon);
};
let style = {
padding: "20px 0",
display: "block",
cursor: "pointer"
};
class Content extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
type: "icononly"
};
}
changeType() {
this.setState({
type: this.state.type === "icononly" ? "tree" : "icononly"
});
}
render() {
return (
<div>
<a style={style} id="spread" onClick={this.changeType.bind(this)}>
点我试试
</a>
<Navigation
type={this.state.type}
onClick={onClick}
style={{ maxWidth: "240px" }}
>
<Item
itemid="1"
text="Value Added Service"
icon="service"
link="#icon"
opened={true}
>
<Navigation>
<Item itemid="1-1-1" text="Apps2" icon="service" opened={true}>
<Navigation>
<Item itemid="1-1-1-1" text="Apps2" icon="service" />
<Item itemid="1-1-1-2" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="1-1-2" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="2" text="Training" icon="training" />
<Item itemid="3" text="Favorite" icon="favorite" />
<Item itemid="4" text="History" icon="history" />
<Item itemid="5" text="Attachment" icon="attachment" />
<Item itemid="6" text="Electronics" icon="electronics" />
</Navigation>
</div>
);
}
}
ReactDOM.render(<Content />, mountNode);
注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
ReactDOM.render(
<Navigation type="line" activeDirection="left">
<Item
itemid="1"
key="1"
text="Value Added Service"
icon="service"
container={<span />}
/>
<Item itemid="2" text="Training" icon="training" />
<Item itemid="3" text="Favorite" icon="favorite" />
<Item itemid="4" text="History" icon="history" />
<Item itemid="5" text="Attachment" icon="attachment" />
<Item itemid="6" text="Electronics" icon="electronics" />
</Navigation>,
mountNode
);
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
ReactDOM.render(
<Navigation
type="normal"
title="Website List"
style={{ maxWidth: "240px" }}
activeDirection="left"
>
<Item itemid="1" text="Value Added Service" icon="service" />
<Item
itemid="2"
text="Training"
icon="training"
link="http://taobao.com"
target="_blank"
selectedStyle={false}
/>
<Item itemid="3" text="Favorite" icon="favorite" />
<Item itemid="4" text="History" icon="history" />
<Item itemid="5" text="Attachment" icon="attachment" />
<Item itemid="6" text="Electronics" icon="electronics" />
</Navigation>,
mountNode
);
注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
let onMouseEnter = () => {
console.log("onMouseEnter");
};
let onMouseLeave = () => {
console.log("onMouseLeave");
};
let menus = [
{ key: "1", text: "下单", icon: "service" },
{
key: "2",
text: "购物车",
icon: "service",
children: [
{
key: "2-1",
text: "下单2",
icon: "service",
children: [{ key: "2-1-1", text: "下单", icon: "service" }]
}
]
},
{
key: "3",
text: "订单",
icon: "service",
children: (
<Navigation>
<Item text="子订单" />
</Navigation>
)
}
];
class App extends React.Component {
constructor() {
super();
this.state = {
current: []
};
}
onChange(e) {
let value = e.target.value;
this.setState({
current: value.split(",")
});
}
render() {
let render = menus => {
return menus.map((m, i) => (
<Item
key={m.key || i}
opened={this.state.current.indexOf(m.key) > -1}
text={m.text}
icon={m.icon}
>
{"length" in (m.children || {}) ? (
<Navigation>{render(m.children)}</Navigation>
) : (
m.children
)}
</Item>
));
};
let renderItem = render(menus);
console.log(renderItem);
return (
<div>
<div style={{ padding: "10px" }}>
<label>输入切换key/itemid(多个用半角,隔开):</label>
<input
id="myInput"
value={this.state.value}
onChange={this.onChange.bind(this)}
/>
</div>
<Navigation
type="tree"
trigger="hover"
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
{renderItem}
</Navigation>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;
查看源码在线预览
import { Navigation, Menu } from "@icedesign/base";
const { Item } = Navigation;
let onMouseEnter = () => {
console.log("onMouseEnter");
};
let onMouseLeave = () => {
console.log("onMouseLeave");
};
let menus = [
{ key: "1", text: "Value", icon: "service" },
{ key: "2", text: "Value", icon: "service" },
{
key: "3",
text: "Value",
icon: "service",
children: (
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item key="2">Option 2</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
<Menu.Item key="4">Option 4</Menu.Item>
<Menu.SubMenu key="41" label="xxx" mode="popup" triggerType="hover">
<Menu.Item key="5">Option 5</Menu.Item>
</Menu.SubMenu>
</Menu>
)
}
];
class App extends React.Component {
constructor() {
super();
this.state = {
current: null
};
}
onChange(e) {
let value = e.target.value;
this.setState({
current: value.trim()
});
}
render() {
let renderItem = menus.map(m => {
return (
<Item
key={m.key}
text={m.text}
icon={m.icon}
selected={m.key === this.state.current}
>
{m.children}
</Item>
);
});
return (
<div>
<div style={{ padding: "10px" }}>
<label>输入切换key/itemid:</label>
<input
id="selectInput"
value={this.state.value}
onChange={this.onChange.bind(this)}
/>
</div>
<Navigation
type="filling"
trigger="hover"
selectedKey={this.state.current}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
{renderItem}
</Navigation>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
let timer, result, onSelect;
onSelect = (id, item, nav) => {
//模拟异步
if (item.props.children) {
return clearTimeout(timer);
}
if (result) {
return nav.setContent(result);
}
nav.setContent(<div>请稍后</div>);
timer = setTimeout(() => {
nav.setContent((result = <div>加载完毕</div>));
}, 1000);
};
ReactDOM.render(
<Navigation
type="slip"
onSelect={onSelect}
style={{ maxWidth: "240px" }}
title="Web Sites"
container={<div className="custom-container" />}
>
<Item
itemid="1"
text="Value Added Service"
icon="service"
link="http://www.taobao.com"
target="_blank"
/>
<Item key="2" text="Training" icon="training">
<i>Training</i>
</Item>
<Item itemid="3" text="Favorite" icon="favorite">
<div>Favorite</div>
</Item>
<Item itemid="4" text="History" icon="history">
<div>History</div>
</Item>
<Item itemid="5" text="Attachment" icon="attachment">
<div>Attachment</div>
</Item>
<Item itemid="6" text="Electronics" icon="electronics">
<div>Electronics</div>
</Item>
</Navigation>,
mountNode
);
注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;
查看源码在线预览
import { Navigation, Menu } from "@icedesign/base";
const { Item } = Navigation;
let onMouseEnter = () => {
console.log("onMouseEnter");
};
let onMouseLeave = () => {
console.log("onMouseLeave");
};
ReactDOM.render(
<Navigation
type="text"
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
trigger="hover"
cententAlign="left"
activeDirection="bottom"
morePositionKey="5"
moreText="+ More"
>
<Item itemid="1" text="淘宝,淘你喜欢" icon="folder" />
<Item itemid="2" text="天猫,正品选择" icon="account" />
<Item itemid="3" text="菜单一" icon="cart" menuAlign="left" />
<Item
link="http://www.baidu.com"
target="_blank"
itemid="4"
text="菜单一"
icon="cart"
menuAlign="left"
/>
<Item itemid="5" text="菜单一" icon="cart" menuAlign="left" />
<Item itemid="6" text="菜单一" icon="cart" menuAlign="left" />
</Navigation>,
mountNode
);
最简单的用法。
查看源码在线预览
import { Navigation } from "@icedesign/base";
const { Item } = Navigation;
class App extends React.Component {
constructor() {
super();
this.state = {
selectedKey: "3"
};
}
onSelect(id, itemid) {
this.setState({
selectedKey: id
});
}
onUnFold(id, item, tree, e) {
console.log("UnFold");
}
render() {
return (
<Navigation
onSelect={this.onSelect.bind(this)}
selectedKey={this.state.selectedKey}
onUnFold={this.onUnFold.bind(this)}
style={{ maxWidth: "240px" }}
type="tree"
title="Web Sites"
accordion={true}
activeDirection="left"
branchPadding="20"
>
<Item
itemid="1"
text="Value Added Service"
icon="service"
opened={false}
hasLeaf={false}
>
<Navigation>
<Item itemid="1-1" text="Apps-One" icon="service" opened={true}>
<Navigation>
<Item itemid="1-1-1" text="Apps-One-One" icon="service" />
<Item itemid="1-1-2" text="Apps-one-Two" icon="service" />
</Navigation>
</Item>
<Item itemid="1-2" text="Apps-Two" icon="service">
<Navigation>
<Item itemid="1-2-1" text="Apps2" icon="service" />
<Item itemid="1-2-2" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="1-3" text="Apps-Three" icon="service" />
</Navigation>
</Item>
<Item itemid="2" text="Training" icon="training">
<Navigation>
<Item itemid="2-1" text="Apps1" icon="service" opened={false}>
<Navigation>
<Item itemid="2-1-1" text="Apps2" icon="service" />
<Item itemid="2-1-2" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="2-2" text="Apps2" icon="service">
<Navigation>
<Item itemid="2-2-1" text="Apps2" icon="service" />
<Item itemid="2-2-2" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="2-3" text="Apps3" icon="service" />
</Navigation>
</Item>
<Item itemid="3" text="Favorite" icon="favorite" />
<Item itemid="4" text="History" icon="history" />
<Item
itemid="5"
text="有链接"
icon="attachment"
link="https://www.taobao.com"
target="_blank"
/>
<Item itemid="6" text="Electronics" icon="electronics" />
</Navigation>
);
}
}
ReactDOM.render(<App />, mountNode);