-
Notifications
You must be signed in to change notification settings - Fork 125
/
Breadcrumbs.jsx
64 lines (55 loc) · 1.37 KB
/
Breadcrumbs.jsx
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
import PropTypes from 'prop-types';
import React from 'react';
import { Link } from 'react-router';
import upperFirst from 'lodash/upperFirst';
import lowerCase from 'lodash/lowerCase';
const acronymMapping = {
poa: 'POA',
va: 'VA',
};
const formatSegment = segment => {
const words = segment.split('-');
const acronymsFixed = words.map(word => {
if (acronymMapping[word]) {
return acronymMapping[word];
}
return lowerCase(word);
});
return upperFirst(acronymsFixed.join(' '));
};
const Breadcrumbs = ({ pathname }) => {
const pathSegments = pathname.split('/').filter(Boolean);
let pathAccumulator = '';
const breadcrumbs = pathSegments.map((segment, index) => {
if (!index) {
return {
link: '/',
label: 'Home',
};
}
if (index === pathSegments.length - 1) {
return {
link: null,
label: formatSegment(segment),
};
}
pathAccumulator += `/${segment}`;
return {
link: pathAccumulator,
label: formatSegment(segment),
};
});
return (
<va-breadcrumbs uswds="false" home-veterans-affairs={false}>
{breadcrumbs.map(({ link, label }) => (
<li key={label}>
<Link to={link}>{label}</Link>
</li>
))}
</va-breadcrumbs>
);
};
Breadcrumbs.propTypes = {
pathname: PropTypes.string.isRequired,
};
export default Breadcrumbs;