Navigation 导航(废弃)

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

注意

Navigation已经废弃, 会在1.0版本中去除, 建议切换到Nav。

代码示例

选中状态位置

设置选中状态的方向,建议横向导航设置为top或bottom,纵向设置为left或right

Navigation 导航(废弃) - 图1

查看源码在线预览

  1. import { Navigation, Select } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. class App extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. activeDirection: "top",
  8. type: "filling"
  9. };
  10. }
  11. changePosition(value) {
  12. this.setState({
  13. activeDirection: value,
  14. type: value === "top" || value === "bottom" ? "filling" : "normal"
  15. });
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <Select
  21. value={this.state.activeDirection}
  22. onChange={::this.changePosition}
  23. className="tab-select-demo"
  24. >
  25. <div value="left">left</div>
  26. <div value="top">top</div>
  27. <div value="bottom">bottom</div>
  28. <div value="right">right</div>
  29. </Select>
  30. <Navigation
  31. type={this.state.type}
  32. activeDirection={this.state.activeDirection}
  33. >
  34. <Item itemid="1" text="Value Added Service" icon="service" />
  35. <Item itemid="2" text="Training" icon="training" />
  36. <Item itemid="3" text="Favorite" icon="favorite" />
  37. <Item itemid="4" text="History" icon="history" />
  38. <Item itemid="5" text="Attachment" icon="attachment" />
  39. </Navigation>
  40. </div>
  41. );
  42. }
  43. }
  44. ReactDOM.render(<App />, mountNode);

基本

最简单的用法.

Navigation 导航(废弃) - 图2

查看源码在线预览

  1. import { Navigation, Menu } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onMouseEnter = () => {
  4. console.log("onMouseEnter");
  5. };
  6. let onMouseLeave = () => {
  7. console.log("onMouseLeave");
  8. };
  9. let onClick = (itemid, item) => {
  10. console.log("Click:" + item.props.text);
  11. };
  12. let onSelect = (itemid, item) => {
  13. console.log("Select:" + item.props.text);
  14. };
  15. class App extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. trigger: "hover"
  20. };
  21. }
  22. onToggleTrigger() {
  23. this.setState({
  24. trigger: this.state.trigger === "hover" ? "click" : "hover"
  25. });
  26. }
  27. render() {
  28. return (
  29. <div>
  30. <button
  31. id="changeTriggerType"
  32. onClick={this.onToggleTrigger.bind(this)}
  33. >
  34. 点击切换trigger方式
  35. </button>
  36. <Navigation
  37. onClick={onClick}
  38. onSelect={onSelect}
  39. onMouseEnter={onMouseEnter}
  40. onMouseLeave={onMouseLeave}
  41. trigger={this.state.trigger}
  42. >
  43. <Item itemid="1" text={<i>Value Added Service</i>}>
  44. <Menu>
  45. <Menu.Item key="1">Option 1</Menu.Item>
  46. <Menu.Item disabled key="2">
  47. Option 2
  48. </Menu.Item>
  49. <Menu.Item key="3">Option 3</Menu.Item>
  50. <Menu.Item key="4">Option 4</Menu.Item>
  51. </Menu>
  52. </Item>
  53. <Item itemid="2" text="Training">
  54. <Menu>
  55. <Menu.Item key="1">Option 1</Menu.Item>
  56. <Menu.Item disabled key="2">
  57. Option 2
  58. </Menu.Item>
  59. <Menu.Item key="3">Option 3</Menu.Item>
  60. <Menu.Item key="4">Option 4</Menu.Item>
  61. </Menu>
  62. </Item>
  63. <Item itemid="3" text="Favorite">
  64. <Menu>
  65. <Menu.Item key="1">Option 1</Menu.Item>
  66. <Menu.Item disabled key="2">
  67. Option 2
  68. </Menu.Item>
  69. <Menu.Item key="3">Option 3</Menu.Item>
  70. <Menu.Item key="4">Option 4</Menu.Item>
  71. </Menu>
  72. </Item>
  73. <Item itemid="4" text="History">
  74. <Menu>
  75. <Menu.Item key="1">Option 1</Menu.Item>
  76. <Menu.Item disabled key="2">
  77. Option 2
  78. </Menu.Item>
  79. <Menu.Item key="3">Option 3</Menu.Item>
  80. <Menu.Item key="4">Option 4</Menu.Item>
  81. </Menu>
  82. </Item>
  83. <Item itemid="5" text="Attachment">
  84. <Menu>
  85. <Menu.Item key="1">Option 1</Menu.Item>
  86. <Menu.Item disabled key="2">
  87. Option 2
  88. </Menu.Item>
  89. <Menu.Item key="3">Option 3</Menu.Item>
  90. <Menu.Item key="4">Option 4</Menu.Item>
  91. </Menu>
  92. </Item>
  93. <Item itemid="6" text="electronics">
  94. <Menu>
  95. <Menu.Item key="1">Option 1</Menu.Item>
  96. <Menu.Item disabled key="2">
  97. Option 2
  98. </Menu.Item>
  99. <Menu.Item key="3">Option 3</Menu.Item>
  100. <Menu.Item key="4">Option 4</Menu.Item>
  101. </Menu>
  102. </Item>
  103. </Navigation>
  104. </div>
  105. );
  106. }
  107. }
  108. ReactDOM.render(<App />, mountNode);

填充背景

注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;

Navigation 导航(废弃) - 图3

查看源码在线预览

  1. import { Navigation, Menu } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onMouseEnter = () => {
  4. console.log("onMouseEnter");
  5. };
  6. let onMouseLeave = () => {
  7. console.log("onMouseLeave");
  8. };
  9. class CustomItem extends React.Component {
  10. render() {
  11. const { link } = this.props;
  12. return <Item content={link} />;
  13. }
  14. }
  15. ReactDOM.render(
  16. <Navigation
  17. onMouseEnter={onMouseEnter}
  18. onMouseLeave={onMouseLeave}
  19. type="filling"
  20. trigger="hover"
  21. activeDirection="left"
  22. morePositionKey="3"
  23. moreText="+ More"
  24. onSelect={id => console.log(id)}
  25. >
  26. <div>
  27. <Item text="TestInnerItem" />
  28. </div>
  29. <CustomItem link={<a href="#test">test</a>} />
  30. <Item itemid="2" text="Training" icon="training" title="Training Long Long">
  31. <Menu>
  32. <Menu.Item key="11">OptionOptionOptionOptionOption 1</Menu.Item>
  33. <Menu.Item disabled key="21">
  34. Option 2
  35. </Menu.Item>
  36. <Menu.Item key="31">Option 3</Menu.Item>
  37. <Menu.Item key="41">Option 4</Menu.Item>
  38. <Menu.SubMenu key="411" label="xxx" mode="popup" triggerType="hover">
  39. <Menu.Item key="xxx">Option2</Menu.Item>
  40. <Menu.Item key="xxx2">Option3</Menu.Item>
  41. </Menu.SubMenu>
  42. </Menu>
  43. </Item>
  44. <Item itemid="3" text="Favorite" icon="favorite" selected="true">
  45. <Menu>
  46. <Menu.SubMenu label="xxx" key="popup1">
  47. <Menu.Item disabled key="112">
  48. abc
  49. </Menu.Item>
  50. <Menu.Item key="222">abc</Menu.Item>
  51. </Menu.SubMenu>
  52. </Menu>
  53. </Item>
  54. <Item itemid="4" text="History" icon="history">
  55. <Menu>
  56. <Menu.Item key="1">Option 1</Menu.Item>
  57. <Menu.Item disabled key="2">
  58. Option 2
  59. </Menu.Item>
  60. <Menu.Item key="3">Option 3</Menu.Item>
  61. <Menu.Item key="4">Option 4</Menu.Item>
  62. </Menu>
  63. </Item>
  64. <Item itemid="5" text="Attachment" icon="attachment">
  65. <Menu>
  66. <Menu.Item key="1">Option 1</Menu.Item>
  67. <Menu.Item disabled key="2">
  68. Option 2
  69. </Menu.Item>
  70. <Menu.Item key="3">Option 3</Menu.Item>
  71. <Menu.Item key="4">Option 4</Menu.Item>
  72. </Menu>
  73. </Item>
  74. <Item itemid="6" text="electronics" icon="Electronics">
  75. <Menu>
  76. <Menu.Item key="1">Option 1</Menu.Item>
  77. <Menu.Item disabled key="2">
  78. Option 2
  79. </Menu.Item>
  80. <Menu.Item key="3">Option 3</Menu.Item>
  81. <Menu.Item key="4">Option 4</Menu.Item>
  82. </Menu>
  83. </Item>
  84. </Navigation>,
  85. mountNode
  86. );

图标型

最简单的用法。

Navigation 导航(废弃) - 图4

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onClick = (itemid, item) => {
  4. console.log("Click:" + item.props.icon);
  5. };
  6. let style = {
  7. padding: "20px 0",
  8. display: "block",
  9. cursor: "pointer"
  10. };
  11. class Content extends React.Component {
  12. constructor(props, context) {
  13. super(props, context);
  14. this.state = {
  15. type: "icononly"
  16. };
  17. }
  18. changeType() {
  19. this.setState({
  20. type: this.state.type === "icononly" ? "tree" : "icononly"
  21. });
  22. }
  23. render() {
  24. return (
  25. <div>
  26. <a style={style} id="spread" onClick={this.changeType.bind(this)}>
  27. 点我试试
  28. </a>
  29. <Navigation
  30. type={this.state.type}
  31. onClick={onClick}
  32. style={{ maxWidth: "240px" }}
  33. >
  34. <Item
  35. itemid="1"
  36. text="Value Added Service"
  37. icon="service"
  38. link="#icon"
  39. opened={true}
  40. >
  41. <Navigation>
  42. <Item itemid="1-1-1" text="Apps2" icon="service" opened={true}>
  43. <Navigation>
  44. <Item itemid="1-1-1-1" text="Apps2" icon="service" />
  45. <Item itemid="1-1-1-2" text="Apps3" icon="service" />
  46. </Navigation>
  47. </Item>
  48. <Item itemid="1-1-2" text="Apps3" icon="service" />
  49. </Navigation>
  50. </Item>
  51. <Item itemid="2" text="Training" icon="training" />
  52. <Item itemid="3" text="Favorite" icon="favorite" />
  53. <Item itemid="4" text="History" icon="history" />
  54. <Item itemid="5" text="Attachment" icon="attachment" />
  55. <Item itemid="6" text="Electronics" icon="electronics" />
  56. </Navigation>
  57. </div>
  58. );
  59. }
  60. }
  61. ReactDOM.render(<Content />, mountNode);

横向

注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;

Navigation 导航(废弃) - 图5

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. ReactDOM.render(
  4. <Navigation type="line" activeDirection="left">
  5. <Item
  6. itemid="1"
  7. key="1"
  8. text="Value Added Service"
  9. icon="service"
  10. container={<span />}
  11. />
  12. <Item itemid="2" text="Training" icon="training" />
  13. <Item itemid="3" text="Favorite" icon="favorite" />
  14. <Item itemid="4" text="History" icon="history" />
  15. <Item itemid="5" text="Attachment" icon="attachment" />
  16. <Item itemid="6" text="Electronics" icon="electronics" />
  17. </Navigation>,
  18. mountNode
  19. );

Normal

Navigation 导航(废弃) - 图6

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. ReactDOM.render(
  4. <Navigation
  5. type="normal"
  6. title="Website List"
  7. style={{ maxWidth: "240px" }}
  8. activeDirection="left"
  9. >
  10. <Item itemid="1" text="Value Added Service" icon="service" />
  11. <Item
  12. itemid="2"
  13. text="Training"
  14. icon="training"
  15. link="http://taobao.com"
  16. target="_blank"
  17. selectedStyle={false}
  18. />
  19. <Item itemid="3" text="Favorite" icon="favorite" />
  20. <Item itemid="4" text="History" icon="history" />
  21. <Item itemid="5" text="Attachment" icon="attachment" />
  22. <Item itemid="6" text="Electronics" icon="electronics" />
  23. </Navigation>,
  24. mountNode
  25. );

横向

注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;

Navigation 导航(废弃) - 图7

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onMouseEnter = () => {
  4. console.log("onMouseEnter");
  5. };
  6. let onMouseLeave = () => {
  7. console.log("onMouseLeave");
  8. };
  9. let menus = [
  10. { key: "1", text: "下单", icon: "service" },
  11. {
  12. key: "2",
  13. text: "购物车",
  14. icon: "service",
  15. children: [
  16. {
  17. key: "2-1",
  18. text: "下单2",
  19. icon: "service",
  20. children: [{ key: "2-1-1", text: "下单", icon: "service" }]
  21. }
  22. ]
  23. },
  24. {
  25. key: "3",
  26. text: "订单",
  27. icon: "service",
  28. children: (
  29. <Navigation>
  30. <Item text="子订单" />
  31. </Navigation>
  32. )
  33. }
  34. ];
  35. class App extends React.Component {
  36. constructor() {
  37. super();
  38. this.state = {
  39. current: []
  40. };
  41. }
  42. onChange(e) {
  43. let value = e.target.value;
  44. this.setState({
  45. current: value.split(",")
  46. });
  47. }
  48. render() {
  49. let render = menus => {
  50. return menus.map((m, i) => (
  51. <Item
  52. key={m.key || i}
  53. opened={this.state.current.indexOf(m.key) > -1}
  54. text={m.text}
  55. icon={m.icon}
  56. >
  57. {"length" in (m.children || {}) ? (
  58. <Navigation>{render(m.children)}</Navigation>
  59. ) : (
  60. m.children
  61. )}
  62. </Item>
  63. ));
  64. };
  65. let renderItem = render(menus);
  66. console.log(renderItem);
  67. return (
  68. <div>
  69. <div style={{ padding: "10px" }}>
  70. <label>输入切换key/itemid(多个用半角,隔开):</label>
  71. <input
  72. id="myInput"
  73. value={this.state.value}
  74. onChange={this.onChange.bind(this)}
  75. />
  76. </div>
  77. <Navigation
  78. type="tree"
  79. trigger="hover"
  80. onMouseEnter={onMouseEnter}
  81. onMouseLeave={onMouseLeave}
  82. >
  83. {renderItem}
  84. </Navigation>
  85. </div>
  86. );
  87. }
  88. }
  89. ReactDOM.render(<App />, mountNode);

横向

注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;

Navigation 导航(废弃) - 图8

查看源码在线预览

  1. import { Navigation, Menu } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onMouseEnter = () => {
  4. console.log("onMouseEnter");
  5. };
  6. let onMouseLeave = () => {
  7. console.log("onMouseLeave");
  8. };
  9. let menus = [
  10. { key: "1", text: "Value", icon: "service" },
  11. { key: "2", text: "Value", icon: "service" },
  12. {
  13. key: "3",
  14. text: "Value",
  15. icon: "service",
  16. children: (
  17. <Menu>
  18. <Menu.Item key="1">Option 1</Menu.Item>
  19. <Menu.Item key="2">Option 2</Menu.Item>
  20. <Menu.Item key="3">Option 3</Menu.Item>
  21. <Menu.Item key="4">Option 4</Menu.Item>
  22. <Menu.SubMenu key="41" label="xxx" mode="popup" triggerType="hover">
  23. <Menu.Item key="5">Option 5</Menu.Item>
  24. </Menu.SubMenu>
  25. </Menu>
  26. )
  27. }
  28. ];
  29. class App extends React.Component {
  30. constructor() {
  31. super();
  32. this.state = {
  33. current: null
  34. };
  35. }
  36. onChange(e) {
  37. let value = e.target.value;
  38. this.setState({
  39. current: value.trim()
  40. });
  41. }
  42. render() {
  43. let renderItem = menus.map(m => {
  44. return (
  45. <Item
  46. key={m.key}
  47. text={m.text}
  48. icon={m.icon}
  49. selected={m.key === this.state.current}
  50. >
  51. {m.children}
  52. </Item>
  53. );
  54. });
  55. return (
  56. <div>
  57. <div style={{ padding: "10px" }}>
  58. <label>输入切换key/itemid:</label>
  59. <input
  60. id="selectInput"
  61. value={this.state.value}
  62. onChange={this.onChange.bind(this)}
  63. />
  64. </div>
  65. <Navigation
  66. type="filling"
  67. trigger="hover"
  68. selectedKey={this.state.current}
  69. onMouseEnter={onMouseEnter}
  70. onMouseLeave={onMouseLeave}
  71. >
  72. {renderItem}
  73. </Navigation>
  74. </div>
  75. );
  76. }
  77. }
  78. ReactDOM.render(<App />, mountNode);

Slip

Navigation 导航(废弃) - 图9

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let timer, result, onSelect;
  4. onSelect = (id, item, nav) => {
  5. //模拟异步
  6. if (item.props.children) {
  7. return clearTimeout(timer);
  8. }
  9. if (result) {
  10. return nav.setContent(result);
  11. }
  12. nav.setContent(<div>请稍后</div>);
  13. timer = setTimeout(() => {
  14. nav.setContent((result = <div>加载完毕</div>));
  15. }, 1000);
  16. };
  17. ReactDOM.render(
  18. <Navigation
  19. type="slip"
  20. onSelect={onSelect}
  21. style={{ maxWidth: "240px" }}
  22. title="Web Sites"
  23. container={<div className="custom-container" />}
  24. >
  25. <Item
  26. itemid="1"
  27. text="Value Added Service"
  28. icon="service"
  29. link="http://www.taobao.com"
  30. target="_blank"
  31. />
  32. <Item key="2" text="Training" icon="training">
  33. <i>Training</i>
  34. </Item>
  35. <Item itemid="3" text="Favorite" icon="favorite">
  36. <div>Favorite</div>
  37. </Item>
  38. <Item itemid="4" text="History" icon="history">
  39. <div>History</div>
  40. </Item>
  41. <Item itemid="5" text="Attachment" icon="attachment">
  42. <div>Attachment</div>
  43. </Item>
  44. <Item itemid="6" text="Electronics" icon="electronics">
  45. <div>Electronics</div>
  46. </Item>
  47. </Navigation>,
  48. mountNode
  49. );

Text

注意:onMouseLeave、onMouseEnter,如果子元素有注册以上事件,注意兼容;

Navigation 导航(废弃) - 图10

查看源码在线预览

  1. import { Navigation, Menu } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. let onMouseEnter = () => {
  4. console.log("onMouseEnter");
  5. };
  6. let onMouseLeave = () => {
  7. console.log("onMouseLeave");
  8. };
  9. ReactDOM.render(
  10. <Navigation
  11. type="text"
  12. onMouseEnter={onMouseEnter}
  13. onMouseLeave={onMouseLeave}
  14. trigger="hover"
  15. cententAlign="left"
  16. activeDirection="bottom"
  17. morePositionKey="5"
  18. moreText="+ More"
  19. >
  20. <Item itemid="1" text="淘宝,淘你喜欢" icon="folder" />
  21. <Item itemid="2" text="天猫,正品选择" icon="account" />
  22. <Item itemid="3" text="菜单一" icon="cart" menuAlign="left" />
  23. <Item
  24. link="http://www.baidu.com"
  25. target="_blank"
  26. itemid="4"
  27. text="菜单一"
  28. icon="cart"
  29. menuAlign="left"
  30. />
  31. <Item itemid="5" text="菜单一" icon="cart" menuAlign="left" />
  32. <Item itemid="6" text="菜单一" icon="cart" menuAlign="left" />
  33. </Navigation>,
  34. mountNode
  35. );

树型

最简单的用法。

Navigation 导航(废弃) - 图11

查看源码在线预览

  1. import { Navigation } from "@icedesign/base";
  2. const { Item } = Navigation;
  3. class App extends React.Component {
  4. constructor() {
  5. super();
  6. this.state = {
  7. selectedKey: "3"
  8. };
  9. }
  10. onSelect(id, itemid) {
  11. this.setState({
  12. selectedKey: id
  13. });
  14. }
  15. onUnFold(id, item, tree, e) {
  16. console.log("UnFold");
  17. }
  18. render() {
  19. return (
  20. <Navigation
  21. onSelect={this.onSelect.bind(this)}
  22. selectedKey={this.state.selectedKey}
  23. onUnFold={this.onUnFold.bind(this)}
  24. style={{ maxWidth: "240px" }}
  25. type="tree"
  26. title="Web Sites"
  27. accordion={true}
  28. activeDirection="left"
  29. branchPadding="20"
  30. >
  31. <Item
  32. itemid="1"
  33. text="Value Added Service"
  34. icon="service"
  35. opened={false}
  36. hasLeaf={false}
  37. >
  38. <Navigation>
  39. <Item itemid="1-1" text="Apps-One" icon="service" opened={true}>
  40. <Navigation>
  41. <Item itemid="1-1-1" text="Apps-One-One" icon="service" />
  42. <Item itemid="1-1-2" text="Apps-one-Two" icon="service" />
  43. </Navigation>
  44. </Item>
  45. <Item itemid="1-2" text="Apps-Two" icon="service">
  46. <Navigation>
  47. <Item itemid="1-2-1" text="Apps2" icon="service" />
  48. <Item itemid="1-2-2" text="Apps3" icon="service" />
  49. </Navigation>
  50. </Item>
  51. <Item itemid="1-3" text="Apps-Three" icon="service" />
  52. </Navigation>
  53. </Item>
  54. <Item itemid="2" text="Training" icon="training">
  55. <Navigation>
  56. <Item itemid="2-1" text="Apps1" icon="service" opened={false}>
  57. <Navigation>
  58. <Item itemid="2-1-1" text="Apps2" icon="service" />
  59. <Item itemid="2-1-2" text="Apps3" icon="service" />
  60. </Navigation>
  61. </Item>
  62. <Item itemid="2-2" text="Apps2" icon="service">
  63. <Navigation>
  64. <Item itemid="2-2-1" text="Apps2" icon="service" />
  65. <Item itemid="2-2-2" text="Apps3" icon="service" />
  66. </Navigation>
  67. </Item>
  68. <Item itemid="2-3" text="Apps3" icon="service" />
  69. </Navigation>
  70. </Item>
  71. <Item itemid="3" text="Favorite" icon="favorite" />
  72. <Item itemid="4" text="History" icon="history" />
  73. <Item
  74. itemid="5"
  75. text="有链接"
  76. icon="attachment"
  77. link="https://www.taobao.com"
  78. target="_blank"
  79. />
  80. <Item itemid="6" text="Electronics" icon="electronics" />
  81. </Navigation>
  82. );
  83. }
  84. }
  85. ReactDOM.render(<App />, mountNode);

相关区块

Navigation 导航(废弃) - 图12

暂无相关区块