Skip to content

Commit

Permalink
feat(bread-crumbs): adds ability to set crumb labels
Browse files Browse the repository at this point in the history
Allows ability to se the label for the crumbs
  • Loading branch information
anguspiv committed Nov 15, 2022
1 parent 0176377 commit 07aaf3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/components/molecules/BreadCrumbs/BreadCrumbs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ const toTitleCase = (str) =>
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');

export function BreadCrumbs({ location, className }) {
export function BreadCrumbs({ labels, location, className }) {
const { pathname } = location;
const segments = pathname.split('/').filter(Boolean);

const crumbs = segments.map((segment, index) => {
const isLast = index === segments.length - 1;
const label = toTitleCase(segment);
const label = labels[segment] || toTitleCase(segment);
const path = `/${segments.slice(0, index + 1).join('/')}`;

return {
Expand Down Expand Up @@ -89,6 +89,7 @@ BreadCrumbs.propTypes = {
pathname: PropTypes.string,
search: PropTypes.string,
}),
labels: PropTypes.shape({}),
};

BreadCrumbs.defaultProps = {
Expand All @@ -97,6 +98,7 @@ BreadCrumbs.defaultProps = {
pathname: '',
search: '',
},
labels: {},
};

export default BreadCrumbs;
17 changes: 13 additions & 4 deletions src/components/molecules/BreadCrumbs/BreadCrumbs.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import { BreadCrumbs } from './BreadCrumbs';

describe('<BreadCrumbs />', () => {
it('should render breadcrumb links', () => {
expect.assertions(4);
expect.assertions(5);

const location = {
pathname: '/somewhere/else-where',
pathname: '/somewhere/else-where/and-again/foo',
};

render(<BreadCrumbs location={location} className="test-class" />);
const labels = {
'and-again': 'And Again or Not',
};

render(<BreadCrumbs location={location} className="test-class" labels={labels} />);

expect(screen.getByRole('link', { name: 'Somewhere' })).toHaveAttribute('href', '/somewhere');

expect(screen.getByText('Else Where')).toBeInTheDocument();
expect(screen.getByText('Foo')).toBeInTheDocument();

expect(screen.getByRole('link', { name: 'And Again or Not' })).toHaveAttribute(
'href',
'/somewhere/else-where/and-again',
);

expect(screen.getByRole('link', { name: 'Home' })).toHaveAttribute('href', '/');

Expand Down

0 comments on commit 07aaf3d

Please sign in to comment.