Breadcrumb面包屑

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

何时使用

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

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

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

代码演示

Breadcrumb 面包屑 - 图1

基本

最简单的用法。

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

Breadcrumb 面包屑 - 图2

react-router

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

  1. import { Router, Route, Link, hashHistory } from 'react-router';
  2. import { Breadcrumb, Alert } from 'antd';
  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,
  35. );
  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 'antd';
  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,
  10. );

Breadcrumb 面包屑 - 图4

带有图标的

图标放在文字前面。

  1. import { Breadcrumb, Icon } from 'antd';
  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>Application</Breadcrumb.Item>
  12. </Breadcrumb>,
  13. mountNode,
  14. );

Breadcrumb 面包屑 - 图5

其它路由

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

  1. import { HashRouter as Router, Route, Switch, Link, withRouter } from 'react-router-dom';
  2. import { Breadcrumb, Alert } from 'antd';
  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}>{breadcrumbNameMap[url]}</Link>
  28. </Breadcrumb.Item>
  29. );
  30. });
  31. const breadcrumbItems = [
  32. <Breadcrumb.Item key="home">
  33. <Link to="/">Home</Link>
  34. </Breadcrumb.Item>,
  35. ].concat(extraBreadcrumbItems);
  36. return (
  37. <div className="demo">
  38. <div className="demo-nav">
  39. <Link to="/">Home</Link>
  40. <Link to="/apps">Application List</Link>
  41. </div>
  42. <Switch>
  43. <Route path="/apps" component={Apps} />
  44. <Route render={() => <span>Home Page</span>} />
  45. </Switch>
  46. <Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
  47. <Breadcrumb>{breadcrumbItems}</Breadcrumb>
  48. </div>
  49. );
  50. });
  51. ReactDOM.render(
  52. <Router>
  53. <Home />
  54. </Router>,
  55. mountNode,
  56. );
  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 面包屑 - 图6

带下拉菜单的面包屑

面包屑支持下拉菜单。

  1. import { Breadcrumb, Menu } from 'antd';
  2. const menu = (
  3. <Menu>
  4. <Menu.Item>
  5. <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
  6. General
  7. </a>
  8. </Menu.Item>
  9. <Menu.Item>
  10. <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">
  11. Layout
  12. </a>
  13. </Menu.Item>
  14. <Menu.Item>
  15. <a target="_blank" rel="noopener noreferrer" href="http://www.tmall.com/">
  16. Navigation
  17. </a>
  18. </Menu.Item>
  19. </Menu>
  20. );
  21. ReactDOM.render(
  22. <div>
  23. <Breadcrumb>
  24. <Breadcrumb.Item>Ant Design</Breadcrumb.Item>
  25. <Breadcrumb.Item>
  26. <a href="">Component</a>
  27. </Breadcrumb.Item>
  28. <Breadcrumb.Item overlay={menu}>
  29. <a href="">General</a>
  30. </Breadcrumb.Item>
  31. <Breadcrumb.Item>Button</Breadcrumb.Item>
  32. </Breadcrumb>
  33. </div>,
  34. mountNode,
  35. );

API

Breadcrumb

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

Breadcrumb.Item

参数参数类型默认值
href链接的目的地string-
separator自定义的分隔符string|ReactNode'/'
overlay下来菜单的内容Menu | () => Menu-
onClick单击事件(e:MouseEventHandler)=>void-

routes

  1. interface Route {
  2. path: string;
  3. breadcrumbName: string;
  4. children: Array<{
  5. path: string;
  6. breadcrumbName: string;
  7. }>;
  8. }

和 browserHistory 配合

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

  1. import { Link } from 'react-router';
  2. const routes = [
  3. {
  4. path: 'index',
  5. breadcrumbName: 'home',
  6. },
  7. {
  8. path: 'first',
  9. breadcrumbName: 'first',
  10. children: [
  11. {
  12. path: '/general',
  13. breadcrumbName: 'General',
  14. },
  15. {
  16. path: '/layout',
  17. breadcrumbName: 'Layout',
  18. },
  19. {
  20. path: '/navigation',
  21. breadcrumbName: 'Navigation',
  22. },
  23. ],
  24. },
  25. {
  26. path: 'second',
  27. breadcrumbName: 'second',
  28. },
  29. ];
  30. function itemRender(route, params, routes, paths) {
  31. const last = routes.indexOf(route) === routes.length - 1;
  32. return last ? (
  33. <span>{route.breadcrumbName}</span>
  34. ) : (
  35. <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
  36. );
  37. }
  38. return <Breadcrumb itemRender={itemRender} routes={routes} />;