Breadcrumb面包屑

显示当前页面在系统层级结构中的位置,并能向上返回。

何时使用

  • 当系统拥有超过两级以上的层级结构时;

  • 当需要告知用户『你在哪里』时;

  • 当需要向上导航的功能时。

代码演示

Breadcrumb 面包屑 - 图1

基本

最简单的用法。

  1. import { Breadcrumb } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <Breadcrumb>
  4. <Breadcrumb.Item>Home</Breadcrumb.Item>
  5. <Breadcrumb.Item><a href="">Application Center</a></Breadcrumb.Item>
  6. <Breadcrumb.Item><a href="">Application List</a></Breadcrumb.Item>
  7. <Breadcrumb.Item>An Application</Breadcrumb.Item>
  8. </Breadcrumb>,
  9. mountNode);

Breadcrumb 面包屑 - 图2

react-router

react-router@2 react-router@3 进行结合使用。

  1. import { Router, Route, Link, hashHistory } from 'react-router';
  2. import { Breadcrumb, Alert } from 'choerodon-ui';
  3. const Apps = () => (
  4. <ul className="app-list">
  5. <li>
  6. <Link to="/apps/1">Application1</Link>:<Link to="/apps/1/detail">Detail</Link>
  7. </li>
  8. <li>
  9. <Link to="/apps/2">Application2</Link>:<Link to="/apps/2/detail">Detail</Link>
  10. </li>
  11. </ul>
  12. );
  13. const Home = ({ routes, params, children }) => (
  14. <div className="demo">
  15. <div className="demo-nav">
  16. <Link to="/">Home</Link>
  17. <Link to="/apps">Application List</Link>
  18. </div>
  19. {children || 'Home Page'}
  20. <Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
  21. <Breadcrumb routes={routes} params={params} />
  22. </div>
  23. );
  24. ReactDOM.render(
  25. <Router history={hashHistory}>
  26. <Route name="home" breadcrumbName="Home" path="/" component={Home}>
  27. <Route name="apps" breadcrumbName="Application List" path="apps" component={Apps}>
  28. <Route name="app" breadcrumbName="Application:id" path=":id">
  29. <Route name="detail" breadcrumbName="Detail" path="detail" />
  30. </Route>
  31. </Route>
  32. </Route>
  33. </Router>,
  34. mountNode);
  1. .demo {
  2. margin: 16px;
  3. }
  4. .demo-nav {
  5. height: 30px;
  6. line-height: 30px;
  7. margin-bottom: 16px;
  8. background: #f8f8f8;
  9. }
  10. .demo-nav a {
  11. line-height: 30px;
  12. padding: 0 8px;
  13. }
  14. .app-list {
  15. margin-top: 16px;
  16. }

Breadcrumb 面包屑 - 图3

分隔符

使用 separator=">" 可以自定义分隔符。

  1. import { Breadcrumb } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <Breadcrumb separator=">">
  4. <Breadcrumb.Item>Home</Breadcrumb.Item>
  5. <Breadcrumb.Item href="">Application Center</Breadcrumb.Item>
  6. <Breadcrumb.Item href="">Application List</Breadcrumb.Item>
  7. <Breadcrumb.Item>An Application</Breadcrumb.Item>
  8. </Breadcrumb>,
  9. mountNode);

Breadcrumb 面包屑 - 图4

带有图标的

图标放在文字前面。

  1. import { Breadcrumb, Icon } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <Breadcrumb>
  4. <Breadcrumb.Item href="">
  5. <Icon type="home" />
  6. </Breadcrumb.Item>
  7. <Breadcrumb.Item href="">
  8. <Icon type="user" />
  9. <span>Application List</span>
  10. </Breadcrumb.Item>
  11. <Breadcrumb.Item>
  12. Application
  13. </Breadcrumb.Item>
  14. </Breadcrumb>,
  15. mountNode);

Breadcrumb 面包屑 - 图5

其它路由

react-router@4,或其他路由进行结合使用。

  1. import { HashRouter as Router, Route, Switch, Link, withRouter } from 'react-router-dom';
  2. import { Breadcrumb, Alert } from 'choerodon-ui';
  3. const Apps = () => (
  4. <ul className="app-list">
  5. <li>
  6. <Link to="/apps/1">Application1</Link>:<Link to="/apps/1/detail">Detail</Link>
  7. </li>
  8. <li>
  9. <Link to="/apps/2">Application2</Link>:<Link to="/apps/2/detail">Detail</Link>
  10. </li>
  11. </ul>
  12. );
  13. const breadcrumbNameMap = {
  14. '/apps': 'Application List',
  15. '/apps/1': 'Application1',
  16. '/apps/2': 'Application2',
  17. '/apps/1/detail': 'Detail',
  18. '/apps/2/detail': 'Detail',
  19. };
  20. const Home = withRouter((props) => {
  21. const { location } = props;
  22. const pathSnippets = location.pathname.split('/').filter(i => i);
  23. const extraBreadcrumbItems = pathSnippets.map((_, index) => {
  24. const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
  25. return (
  26. <Breadcrumb.Item key={url}>
  27. <Link to={url}>
  28. {breadcrumbNameMap[url]}
  29. </Link>
  30. </Breadcrumb.Item>
  31. );
  32. });
  33. const breadcrumbItems = [(
  34. <Breadcrumb.Item key="home">
  35. <Link to="/">Home</Link>
  36. </Breadcrumb.Item>
  37. )].concat(extraBreadcrumbItems);
  38. return (
  39. <div className="demo">
  40. <div className="demo-nav">
  41. <Link to="/">Home</Link>
  42. <Link to="/apps">Application List</Link>
  43. </div>
  44. <Switch>
  45. <Route path="/apps" component={Apps} />
  46. <Route render={() => <span>Home Page</span>} />
  47. </Switch>
  48. <Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
  49. <Breadcrumb>
  50. {breadcrumbItems}
  51. </Breadcrumb>
  52. </div>
  53. );
  54. });
  55. ReactDOM.render(
  56. <Router>
  57. <Home />
  58. </Router>,
  59. mountNode);
  1. .demo {
  2. margin: 16px;
  3. }
  4. .demo-nav {
  5. height: 30px;
  6. line-height: 30px;
  7. margin-bottom: 16px;
  8. background: #f8f8f8;
  9. }
  10. .demo-nav a {
  11. line-height: 30px;
  12. padding: 0 8px;
  13. }
  14. .app-list {
  15. margin-top: 16px;
  16. }

API

参数说明类型可选值默认值
itemRender自定义链接函数,和 react-router 配置使用(route, params, routes, paths) => ReactNode-
params路由的参数object-
routesrouter 的路由栈信息object[]-
separator分隔符自定义string|ReactNode'/'

2.0 之后,linkRendernameRender 被移除,请使用 itemRender 来代替。

和 browserHistory 配合

和 react-router 一起使用时,默认生成的 url 路径是带有 # 的,如果和 browserHistory 一起使用的话,你可以使用 itemRender 属性定义面包屑链接。

  1. import { Link } from 'react-router';
  2. const routes = [{
  3. path: 'index',
  4. breadcrumbName: '首页'
  5. }, {
  6. path: 'first',
  7. breadcrumbName: '一级面包屑'
  8. }, {
  9. path: 'second',
  10. breadcrumbName: '当前页面'
  11. }];
  12. function itemRender(route, params, routes, paths) {
  13. const last = routes.indexOf(route) === routes.length - 1;
  14. return last ? <span>{route.breadcrumbName}</span> : <Link to={paths.join('/')}>{route.breadcrumbName}</Link>;
  15. }
  16. return <Breadcrumb itemRender={itemRender} routes={routes}/>;