Nav 导航

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

安装方法

  1. 在命令行中执行以下命令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-
popupAlignTree 展开时候右侧子item的对齐方式可选值:'follow', 'outside'Enum'follow'
triggerTypePopupItem触发方式可选值:'click', 'hover'Enum-
className自定义classString-
iconOnly控制icon是否展示Boolean-
hasTooltip是否有ToolTIps(仅在iconOnly=true时生效)Booleanfalse
hasArrow是否显示右侧的箭头(仅在iconOnly=true时生效)Booleantrue

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支持

Nav 导航 - 图1

查看源码在线预览

  1. import { Nav, Select, Switch, Field } from "@icedesign/base";
  2. const { Item } = Nav;
  3. class App extends React.Component {
  4. field = new Field(this);
  5. render() {
  6. const { init, getValue } = this.field;
  7. return (
  8. <div>
  9. <div className="demo-ctl">
  10. <Switch
  11. checkedChildren="横"
  12. unCheckedChildren="竖"
  13. {...init("direction")}
  14. />
  15. <Select
  16. placeholder="active 位置"
  17. {...init("activeDirection", { initValue: "left" })}
  18. >
  19. <Option value="">无</Option>
  20. <Option value="top">top</Option>
  21. <Option value="bottom">bottom</Option>
  22. <Option value="left">left</Option>
  23. <Option value="right">right</Option>
  24. </Select>
  25. </div>
  26. <br />
  27. <Nav
  28. style={{ width: getValue("direction") ? "100%" : "240px" }}
  29. direction={getValue("direction") ? "hoz" : "ver"}
  30. activeDirection={getValue("activeDirection")}
  31. defaultSelectedKeys={["1"]}
  32. >
  33. <Item key="1" icon="service" hasSelectedIcon={false}>
  34. Value Added Service
  35. </Item>
  36. <Item key="2" icon="training">
  37. Training
  38. </Item>
  39. <Item key="3" icon="favorite">
  40. Favorite
  41. </Item>
  42. <Item key="4" icon="history">
  43. History
  44. </Item>
  45. <Item key="5" icon="attachment">
  46. Attachment
  47. </Item>
  48. <Item key="6" icon="electronics">
  49. Electronics
  50. </Item>
  51. </Nav>
  52. </div>
  53. );
  54. }
  55. }
  56. ReactDOM.render(<App />, mountNode);
  1. .demo-ctl {
  2. background-color: #f1f1f1;
  3. padding: 10.0px;
  4. color: #0a7ac3;
  5. border-left: 4.0px solid #0d599a;
  6. }
  7. .demo-ctl .next-switch {
  8. margin-right: 20px;
  9. }

基本

横向导航

Nav 导航 - 图2

查看源码在线预览

  1. import { Nav, Icon, Menu } from "@icedesign/base";
  2. const { Item, PopupItem } = Nav;
  3. ReactDOM.render(
  4. <Nav direction="hoz" activeDirection="bottom">
  5. <Item key="1" icon="service">
  6. First
  7. </Item>
  8. <Item key="2">Training</Item>
  9. <Item key="3" icon="favorite">
  10. Favorite
  11. </Item>
  12. <Item key="4">
  13. <a href="http://www.taobao.com" target="_blank">
  14. <Icon type="attachment" />Taobao
  15. </a>
  16. </Item>
  17. <PopupItem key="5" icon="history" label="History">
  18. <Menu>
  19. <Menu.Item key="51">Option 1</Menu.Item>
  20. <Menu.Item key="52">Option 3</Menu.Item>
  21. <Menu.Item disabled key="54">
  22. Option 2
  23. </Menu.Item>
  24. <Menu.Item key="53">Option 4</Menu.Item>
  25. </Menu>
  26. </PopupItem>
  27. <PopupItem key="6" icon="electronics" label="Sub Nav">
  28. <Menu>
  29. <Menu.Item key="61">Option 1</Menu.Item>
  30. <Menu.Item key="62">Option 3</Menu.Item>
  31. <Menu.Item key="63">Option 4</Menu.Item>
  32. <Menu.PopupItem key="64" label="popup">
  33. <Menu>
  34. <Menu.Item key="640">Option 1</Menu.Item>
  35. <Menu.Item key="641">Option 3</Menu.Item>
  36. <Menu.Item key="642">Option 4</Menu.Item>
  37. </Menu>
  38. </Menu.PopupItem>
  39. </Menu>
  40. </PopupItem>
  41. </Nav>,
  42. mountNode
  43. );

分组

group

Nav 导航 - 图3

查看源码在线预览

  1. import { Nav } from "@icedesign/base";
  2. const { Item, Group } = Nav;
  3. ReactDOM.render(
  4. <Nav style={{ width: 240 }}>
  5. <Group label="Website List">
  6. <Item key="1" icon="service">
  7. Value Added Service
  8. </Item>
  9. <Item key="2" icon="training" disabled>
  10. Training
  11. </Item>
  12. <Item key="3" icon="favorite">
  13. Favorite
  14. </Item>
  15. <Item key="4" icon="history">
  16. History
  17. </Item>
  18. <Item key="5" icon="attachment">
  19. Attachment
  20. </Item>
  21. <Item key="6" icon="electronics">
  22. Electronics
  23. </Item>
  24. </Group>
  25. </Nav>,
  26. mountNode
  27. );

图标

iconOnly切换图标

Nav 导航 - 图4

查看源码在线预览

  1. import { Nav, Switch, Field } from "@icedesign/base";
  2. const { Item, SubNav } = Nav;
  3. class App extends React.Component {
  4. field = new Field(this);
  5. render() {
  6. const { init, getValue } = this.field;
  7. return (
  8. <div>
  9. <Switch
  10. {...init("iconOnly", { initValue: true, valueName: "checked" })}
  11. />
  12. <br />
  13. <Nav
  14. iconOnly={getValue("iconOnly")}
  15. hasTooltip
  16. style={{ display: "inline-block" }}
  17. >
  18. <Item key="1" icon="service">
  19. Value Added Service
  20. </Item>
  21. <Item key="2" icon="training">
  22. Training
  23. </Item>
  24. <Item key="3" icon="favorite">
  25. Favorite
  26. </Item>
  27. <Item key="4" icon="history">
  28. History
  29. </Item>
  30. <Item key="5" icon="attachment">
  31. Attachment
  32. </Item>
  33. <Item key="6" icon="electronics">
  34. Electronics
  35. </Item>
  36. <SubNav key="sub4" label=" Nav Three">
  37. <Item key="9" icon="favorite">
  38. Option 9
  39. </Item>
  40. <Item key="10" icon="favorite">
  41. Option 10
  42. </Item>
  43. <Item key="11" icon="favorite">
  44. Option 11
  45. </Item>
  46. <Item key="12" icon="favorite">
  47. Option 12
  48. </Item>
  49. </SubNav>
  50. </Nav>
  51. <br />
  52. 无箭头(hover最后一个item)
  53. <br />
  54. <Nav
  55. iconOnly={getValue("iconOnly")}
  56. hasArrow={false}
  57. hasTooltip
  58. triggerType="hover"
  59. style={{ display: "inline-block" }}
  60. >
  61. <Item key="1" icon="service">
  62. Value Added Service
  63. </Item>
  64. <Item key="2" icon="training">
  65. Training
  66. </Item>
  67. <Item key="3" icon="favorite">
  68. Favorite
  69. </Item>
  70. <Item key="4" icon="history">
  71. History
  72. </Item>
  73. <Item key="5" icon="attachment">
  74. Attachment
  75. </Item>
  76. <SubNav mode="popup" key="sub4" icon="electronics" label="Nav Three">
  77. <Item key="9" icon="favorite">
  78. Option 9
  79. </Item>
  80. <Item key="10" icon="favorite">
  81. Option 10
  82. </Item>
  83. <Item key="11" icon="favorite">
  84. Option 11
  85. </Item>
  86. <Item key="12" icon="favorite">
  87. Option 12
  88. </Item>
  89. </SubNav>
  90. </Nav>
  91. </div>
  92. );
  93. }
  94. }
  95. ReactDOM.render(<App />, mountNode);

自定义头尾

自定义导航头尾

Nav 导航 - 图5

查看源码在线预览

  1. import { Nav, Icon, Switch } from "@icedesign/base";
  2. const { Item } = Nav;
  3. class App extends React.Component {
  4. state = { direction: "hoz" };
  5. onChange(v) {
  6. this.setState({
  7. direction: v ? "hoz" : "ver"
  8. });
  9. }
  10. render() {
  11. return (
  12. <div>
  13. <Switch
  14. checkedChildren="横"
  15. unCheckedChildren="竖"
  16. defaultChecked
  17. onChange={this.onChange.bind(this)}
  18. />
  19. <br />
  20. <br />
  21. <div>
  22. <Nav
  23. direction={this.state.direction}
  24. type="primary"
  25. header={
  26. <img
  27. style={{ margin: "0 10px" }}
  28. src="http://c.sg.ali-lazada.com/lazada/lib/0.0.1/image/lsc-logo.png"
  29. />
  30. }
  31. footer={
  32. <div style={{ margin: "0 10px", lineHeight: "44px" }}>
  33. <a href="javascript:;"> Login in</a>
  34. </div>
  35. }
  36. >
  37. <Item key="1" icon="service">
  38. Value Added Service
  39. </Item>
  40. <Item key="2" icon="training">
  41. Training
  42. </Item>
  43. <Item key="3" icon="favorite">
  44. Favorite
  45. </Item>
  46. <Item key="4" icon="history">
  47. History
  48. </Item>
  49. <Item key="5">
  50. <a href="http://www.taobao.com" target="_blank">
  51. <Icon type="attachment" />Link
  52. </a>
  53. </Item>
  54. <Item key="6" icon="electronics">
  55. Electronics
  56. </Item>
  57. </Nav>
  58. </div>
  59. </div>
  60. );
  61. }
  62. }
  63. ReactDOM.render(<App />, mountNode);

弹出

outside: Nav顶端对齐。follow: Item顶端对齐当SubNav的mode=popup的时候,popAlign控制弹出菜单对齐方式

Nav 导航 - 图6

查看源码在线预览

  1. import { Nav, Icon } from "@icedesign/base";
  2. const { Item, SubNav } = Nav;
  3. ReactDOM.render(
  4. <div>
  5. <Nav style={{ width: 240 }} popupAlign="outside" defaultOpenKeys={["sub2"]}>
  6. <SubNav
  7. key="sub1"
  8. mode="popup"
  9. label={
  10. <span>
  11. <Icon type="service" size="small" />
  12. <span> Nav One</span>
  13. </span>
  14. }
  15. >
  16. <Item key="1">Option 1</Item>
  17. <Item key="2">Option 2</Item>
  18. <Item key="3">Option 3</Item>
  19. <Item key="4">Option 4</Item>
  20. </SubNav>
  21. <SubNav
  22. key="sub2"
  23. mode="popup"
  24. label={
  25. <span>
  26. <Icon type="training" size="small" />
  27. <span> Nav Two</span>
  28. </span>
  29. }
  30. >
  31. <Item key="5">Option 5</Item>
  32. <Item key="6">Option 6</Item>
  33. </SubNav>
  34. <SubNav
  35. key="sub3"
  36. mode="popup"
  37. label={
  38. <span>
  39. <Icon type="favorite" size="small" />
  40. <span> Nav Three</span>
  41. </span>
  42. }
  43. >
  44. <Item key="9">Option 9</Item>
  45. <Item key="10">Option 10</Item>
  46. <Item key="11">Option 11</Item>
  47. <Item key="12">Option 12</Item>
  48. </SubNav>
  49. <SubNav
  50. key="sub4"
  51. mode="popup"
  52. label={
  53. <span>
  54. <Icon type="favorite" size="small" />
  55. <span> Nav Three</span>
  56. </span>
  57. }
  58. >
  59. <Item key="21">Option 9</Item>
  60. <Item key="22">Option 10</Item>
  61. <Item key="23">Option 11</Item>
  62. <Item key="24">Option 12</Item>
  63. </SubNav>
  64. </Nav>
  65. </div>,
  66. mountNode
  67. );

子菜单

单例、多例模式

Nav 导航 - 图7

查看源码在线预览

  1. import { Nav, Icon, Switch } from "@icedesign/base";
  2. const { Item, SubNav } = Nav;
  3. class App extends React.Component {
  4. state = {
  5. openmode: "single"
  6. };
  7. onChange(v) {
  8. this.setState({
  9. openmode: v ? "multiple" : "single"
  10. });
  11. }
  12. render() {
  13. return (
  14. <div>
  15. <Switch
  16. checkedChildren="多"
  17. unCheckedChildren="单"
  18. onChange={this.onChange.bind(this)}
  19. />
  20. <br />
  21. <br />
  22. <Nav
  23. style={{ width: 240 }}
  24. direction="ver"
  25. openMode={this.state.openmode}
  26. >
  27. <SubNav
  28. key="sub1"
  29. label={
  30. <span>
  31. <Icon type="service" size="small" />
  32. <span>Nav One</span>
  33. </span>
  34. }
  35. >
  36. <Item key="1">Option 1</Item>
  37. <Item key="2">Option 2</Item>
  38. <Item key="3">Option 3</Item>
  39. <Item key="4">Option 4</Item>
  40. </SubNav>
  41. <SubNav
  42. key="sub2"
  43. label={
  44. <span>
  45. <Icon type="training" size="small" />
  46. <a href="javascript:;">Nav Two</a>
  47. </span>
  48. }
  49. >
  50. <Item key="5">Option 5</Item>
  51. <Item key="6">Option 6</Item>
  52. <SubNav key="sub3" label="Submenu">
  53. <Item key="7">
  54. <a href="javascript:;">Option 7</a>
  55. </Item>
  56. <Item key="8">Option 8</Item>
  57. </SubNav>
  58. </SubNav>
  59. <SubNav
  60. key="sub4"
  61. label={
  62. <span>
  63. <Icon type="favorite" size="small" />
  64. <span> Nav Three</span>
  65. </span>
  66. }
  67. >
  68. <Item key="9">Option 9</Item>
  69. <Item key="10">Option 10</Item>
  70. <Item key="11">Option 11</Item>
  71. <Item key="12">Option 12</Item>
  72. </SubNav>
  73. <Item key="13">Option 13</Item>
  74. </Nav>
  75. </div>
  76. );
  77. }
  78. }
  79. ReactDOM.render(<App />, mountNode);

类别

type控制类别切换

Nav 导航 - 图8

查看源码在线预览

  1. import { Nav, Switch } from "@icedesign/base";
  2. const { Item } = Nav;
  3. function renderNav(type, direction) {
  4. return (
  5. <div className={`demo-${direction}`}>
  6. <h2>{type}</h2>
  7. <Nav type={type} direction={direction}>
  8. <Item key="1" icon="service">
  9. Value Added Service
  10. </Item>
  11. <Item key="2" icon="training">
  12. Training
  13. </Item>
  14. <Item key="3" icon="favorite">
  15. Favorite
  16. </Item>
  17. <Item key="4" icon="history">
  18. History
  19. </Item>
  20. <Item key="5" icon="attachment">
  21. Attachment
  22. </Item>
  23. <Item key="6" icon="electronics">
  24. Electronics
  25. </Item>
  26. </Nav>
  27. </div>
  28. );
  29. }
  30. class App extends React.Component {
  31. state = { direction: "hoz" };
  32. onChange(v) {
  33. this.setState({
  34. direction: v ? "hoz" : "ver"
  35. });
  36. }
  37. render() {
  38. return (
  39. <div>
  40. <Switch
  41. checkedChildren="横"
  42. unCheckedChildren="竖"
  43. defaultChecked
  44. onChange={this.onChange.bind(this)}
  45. />
  46. <br />
  47. <div style={{ display: "inline-block" }}>
  48. {renderNav("primary", this.state.direction)}
  49. {renderNav("secondary", this.state.direction)}
  50. {renderNav("normal", this.state.direction)}
  51. {renderNav("line", this.state.direction)}
  52. {renderNav("text", this.state.direction)}
  53. </div>
  54. </div>
  55. );
  56. }
  57. }
  58. ReactDOM.render(<App />, mountNode);
  1. .demo-hoz {
  2. width: 100%;
  3. }
  4. .demo-ver {
  5. width: 200px;
  6. display: inline-block;
  7. float: left;
  8. }
  9. .demo-ver .next-nav{
  10. margin-left: 5px;
  11. }

相关区块

Nav 导航 - 图9

暂无相关区块