Skip to content

Commit

Permalink
Feat(web-react): Make BreadcrumbsItem href optional #DS-957
Browse files Browse the repository at this point in the history
  • Loading branch information
crishpeen committed Oct 10, 2023
1 parent 8587f79 commit 31a5ef0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/web-react/src/components/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export const Breadcrumbs = <T extends ElementType = 'nav'>(props: SpiritBreadcru
items?.map((item, index) => (
<React.Fragment key={`BreadcrumbsItem_${item.title}`}>
{index === items.length - 2 && goBackTitle && (
<BreadcrumbsItem href={item.url} iconNameStart="chevron-left" isGoBackOnly>
<BreadcrumbsItem href={item.url || undefined} iconNameStart="chevron-left" isGoBackOnly>
{goBackTitle}
</BreadcrumbsItem>
)}
<BreadcrumbsItem
key={`BreadcrumbsItem_${item.title}`}
href={item.url}
href={item.url || undefined}
isCurrent={isLast(index, items?.length)}
>
{item.title}
Expand Down
20 changes: 12 additions & 8 deletions packages/web-react/src/components/Breadcrumbs/BreadcrumbsItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ const BreadcrumbsItem = (props: SpiritBreadcrumbsItemProps) => {
return (
<li {...transferProps} {...styleProps} className={classNames(classProps.item, styleProps.className)}>
{iconNameStart && <Icon name={iconNameStart} />}
<Link
href={href}
color={isCurrent ? 'secondary' : 'primary'}
isUnderlined={!isCurrent}
aria-current={isCurrent ? 'page' : undefined}
>
{children}
</Link>
{href ? (
<Link
href={href}
color={isCurrent ? 'secondary' : 'primary'}
isUnderlined={!isCurrent}
aria-current={isCurrent ? 'page' : undefined}
>
{children}
</Link>
) : (
children
)}
{!isCurrent && !restProps.isGoBackOnly && iconNameEnd && <Icon name={iconNameEnd} />}
</li>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/web-react/src/components/Breadcrumbs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Use the `BreadcrumbsItem` component for the ordered list as the component's chil
| Name | Type | Default | Required | Description |
| ------------------ | --------------- | --------------- | -------- | ----------------------------------------------------- |
| `children` | `ReactNode` ||| Custom content to override items rendering from array |
| `href` | `string` || | URL |
| `href` | `string` || | URL, if not set, the item is rendered as a plain text |
| `iconNameEnd` | `string` | `chevron-right` || Icon component on the end of the item wrapper |
| `iconNameStart` | `string` | - || Icon component on the start of the item |
| `isCurrent` | `boolean` | `false` || Whether is the item the current page |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ describe('BreadcrumbsItem', () => {

classNamePrefixProviderTest(BreadcrumbsItemGoBack, 'd-tablet-none');
});

describe('BreadcrumbsItem without a link', () => {
const BreadcrumbsItemWithoutLink = () => <BreadcrumbsItem isCurrent>test_title</BreadcrumbsItem>;

const dom = render(<BreadcrumbsItemWithoutLink />);
const listElement = dom.container.querySelector('li') as HTMLLIElement;

it('should render BreadcrumbsItem with go back title', () => {
expect(listElement).toHaveTextContent('test_title');
});

classNamePrefixProviderTest(BreadcrumbsItemWithoutLink, 'd-none');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { Breadcrumbs } from '../Breadcrumbs';

const BreadcrumbsDefault = () => {
const items = [
{
title: 'Root',
url: '#rootUrl',
},
{
title: 'Category',
url: '#categoryUrl',
},
{
title: 'Subcategory',
url: '#subcategoryUrl',
},
{
title: 'Current page',
},
];

return <Breadcrumbs items={items} goBackTitle="Back" />;
};

export default BreadcrumbsDefault;
4 changes: 4 additions & 0 deletions packages/web-react/src/components/Breadcrumbs/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import icons from '@lmc-eu/spirit-icons/dist/icons';
import DocsSection from '../../../../docs/DocsSections';
import { IconsProvider } from '../../../context';
import BreadcrumbsDefault from './BreadcrumbsDefault';
import BreadcrumbsCurrentNotLink from './BreadcrumbsCurrentNotLink';
import BreadcrumbsCustom from './BreadcrumbsCustom';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
Expand All @@ -19,6 +20,9 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<DocsSection title="Custom">
<BreadcrumbsCustom />
</DocsSection>
<DocsSection title="Current page is not a link">
<BreadcrumbsCurrentNotLink />
</DocsSection>
</IconsProvider>
</React.StrictMode>,
);
4 changes: 2 additions & 2 deletions packages/web-react/src/types/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { ChildrenProps, StyleProps, TransferProps } from './shared';

export type BreadcrumbsItem = {
title: string;
url: string;
url?: string;
};

export interface SpiritBreadcrumbsItemProps extends ChildrenProps {
href: string;
href?: string;
iconNameStart?: string;
iconNameEnd?: string;
isCurrent: boolean;
Expand Down

0 comments on commit 31a5ef0

Please sign in to comment.