-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathBreadcrumb.js
103 lines (85 loc) · 2.24 KB
/
Breadcrumb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { Component } from 'react';
import { Route, Link } from 'react-router-dom';
import { Breadcrumb, BreadcrumbItem } from 'reactstrap';
import PropTypes from 'prop-types';
import classNames from 'classnames';
let routes;
const getPaths = (pathname) => {
const paths = ['/'];
if (pathname === '/') return paths;
pathname.split('/').reduce((prev, curr) => {
const currPath = `${prev}/${curr}`;
paths.push(currPath);
return currPath;
});
return paths;
};
const findRouteName = (url) => {
const aroute = routes.find(route => route.path === url);
if (aroute && aroute.name) {
return aroute.name;
}
return null;
};
const BreadcrumbsItem = ({ match }) => {
const routeName = findRouteName(match.url);
if (routeName) {
return (
match.isExact ?
<BreadcrumbItem active>{routeName}</BreadcrumbItem>
:
<BreadcrumbItem>
<Link to={match.url || ''}>
{routeName}
</Link>
</BreadcrumbItem>
);
}
return null;
};
BreadcrumbsItem.propTypes = {
match: PropTypes.shape({
url: PropTypes.string
})
};
const Breadcrumbs = (args) => {
const paths = getPaths(args.location.pathname);
const items = paths.map((path, i) => <Route key={i.toString()} path={path} component={BreadcrumbsItem} />);
return (
<Breadcrumb>
{items}
</Breadcrumb>
);
};
const propTypes = {
children: PropTypes.node,
className: PropTypes.string,
appRoutes: PropTypes.any,
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string])
};
const defaultProps = {
tag: 'div',
className: '',
appRoutes: [{ path: '/', exact: true, name: 'Home', component: null }]
};
class AppBreadcrumb extends Component {
constructor(props) {
super(props);
this.state = { routes: props.appRoutes };
routes = this.state.routes;
}
render() {
const { className, tag: Tag, ...attributes } = this.props;
delete attributes.children
delete attributes.appRoutes
const classes = classNames(className);
return (
<Tag className={classes}>
<Route path="/:path" component={Breadcrumbs} {...attributes} />
</Tag>
);
}
}
AppBreadcrumb.propTypes = propTypes;
AppBreadcrumb.defaultProps = defaultProps;
export default AppBreadcrumb;