UNPKG

1.94 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import { Breadcrumb, BreadcrumbItem } from 'reactstrap';
4
5const Breadcrumbs = ({ crumbs, active, emptyState, children, linkTag: LinkTag, homeUrl, ...rest }) => {
6 const renderBreadCrumb = (crumb) => {
7 // default breadcrumbitem render
8 let breadCrumbItemChildren = <span>{emptyState}</span>;
9 // render static links
10 if (crumb.name && crumb.url) {
11 breadCrumbItemChildren = (
12 <LinkTag aria-label={crumb.name} href={crumb.url}>
13 {crumb.name}
14 </LinkTag>
15 );
16 }
17 return <BreadcrumbItem key={crumb.name + crumb.url}>{breadCrumbItemChildren}</BreadcrumbItem>;
18 };
19
20 return (
21 <Breadcrumb {...rest}>
22 <BreadcrumbItem>
23 <LinkTag aria-label="Home" href={homeUrl}>
24 Home
25 </LinkTag>
26 </BreadcrumbItem>
27 {crumbs && crumbs.length > 0 && crumbs.map((crumb) => renderBreadCrumb(crumb))}
28 {children}
29 <BreadcrumbItem active>{active || emptyState}</BreadcrumbItem>
30 </Breadcrumb>
31 );
32};
33
34Breadcrumbs.propTypes = {
35 /** The name of the active page (the page the user is currently on). */
36 active: PropTypes.string.isRequired,
37 /** The ancestor pages. Objects in array contain `name` (String) and `url` (String) properties. */
38 crumbs: PropTypes.arrayOf(
39 PropTypes.shape({
40 name: PropTypes.string,
41 url: PropTypes.string,
42 })
43 ),
44 /** Custom link tag for the links. */
45 linkTag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
46 /** The value to display when the active page or an ancestor does not have a value. */
47 emptyState: PropTypes.string,
48 /** The children must be a reactstrap `BreadcrumbItem` components. */
49 children: PropTypes.node,
50 /** Url for the Home route. */
51 homeUrl: PropTypes.string,
52};
53
54Breadcrumbs.defaultProps = {
55 emptyState: '…',
56 homeUrl: '/public/apps/dashboard',
57 linkTag: 'a',
58};
59
60export default Breadcrumbs;