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 authored and literat committed Oct 17, 2023
1 parent 4af0774 commit 6cf75ad
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 23 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,11 +30,11 @@ export const Breadcrumbs = <T extends ElementType = 'nav'>(props: SpiritBreadcru
items?.map((item, index) => (
<Fragment key={`BreadcrumbsItem_${item.title}`}>
{index === items.length - 2 && goBackTitle && (
<BreadcrumbsItem href={item.url} isGoBackOnly>
<BreadcrumbsItem href={item.url || undefined} isGoBackOnly>
{goBackTitle}
</BreadcrumbsItem>
)}
<BreadcrumbsItem href={item.url} isCurrent={isLast(index, items?.length)}>
<BreadcrumbsItem href={item.url || undefined} isCurrent={isLast(index, items?.length)}>
{item.title}
</BreadcrumbsItem>
</Fragment>
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 @@ export const BreadcrumbsItem = (props: SpiritBreadcrumbsItemProps) => {
return (
<li {...transferProps} {...styleProps} className={classNames(classProps.item, styleProps.className)}>
{restProps.isGoBackOnly && iconNameStart && <Icon name={iconNameStart} />}
<Link
href={href}
color={isCurrent ? 'secondary' : 'primary'}
isUnderlined={!isCurrent}
aria-current={isCurrent ? 'page' : undefined}
>
{children}
</Link>
{!href && isCurrent ? (
children
) : (
<Link
href={href}
color={isCurrent ? 'secondary' : 'primary'}
isUnderlined={!isCurrent}
aria-current={isCurrent ? 'page' : undefined}
>
{children}
</Link>
)}
{!isCurrent && !restProps.isGoBackOnly && iconNameEnd && <Icon name={iconNameEnd} />}
</li>
);
Expand Down
22 changes: 12 additions & 10 deletions packages/web-react/src/components/Breadcrumbs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ Use the `BreadcrumbsItem` component for the ordered list as the component's chil

### API

| Name | Type | Default | Required | Description |
| ------------------ | --------------- | --------------- | -------- | ------------------------------------------- |
| `children` | `ReactNode` ||| Children node |
| `href` | `string` ||| URL |
| `iconNameEnd` | `string` | `chevron-right` || Icon name at the end of the item |
| `iconNameStart` | `string` | `chevron-left` || Icon name at the start of the item |
| `isCurrent` | `boolean` | `false` || Whether is the item the current page |
| `isGoBackOnly` | `boolean` | `false` || Whether should be displayed in go back mode |
| `UNSAFE_className` | `string` ||| Wrapper custom class name |
| `UNSAFE_style` | `CSSProperties` ||| Wrapper custom style |
| Name | Type | Default | Required | Description |
| ------------------ | --------------- | --------------- | -------- | ----------------------------------------------------- |
| `children` | `ReactNode` ||| Children node |
| `href` | `string` ||\* | URL, if not set, the item is rendered as a plain text |
| `iconNameEnd` | `string` | `chevron-right` || Icon name at the end of the item |
| `iconNameStart` | `string` | `chevron-left` || Icon name at the start of the item |
| `isCurrent` | `boolean` | `false` || Whether is the item the current page |
| `isGoBackOnly` | `boolean` | `false` || Whether should be displayed in go back mode |
| `UNSAFE_className` | `string` ||| Wrapper custom class name |
| `UNSAFE_style` | `CSSProperties` ||| Wrapper custom style |

(\*) Optional only for the current page.

### Dealing with long titles

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,27 @@ describe('BreadcrumbsItem', () => {
expect(lastElement.tagName).not.toBe('svg');
});
});

describe('BreadcrumbsItem without a link', () => {
it('should render BreadcrumbsItem as a plain text', () => {
const dom = render(<BreadcrumbsItem isCurrent>test_title</BreadcrumbsItem>);
const listElement = dom.container.querySelector('li') as HTMLLIElement;
const anchorElement = dom.container.querySelector('a') as HTMLAnchorElement;

expect(listElement).toHaveTextContent('test_title');
expect(anchorElement).toBeNull();
});

it('should render BreadcrumbsItem as a Link', () => {
const dom = render(
<BreadcrumbsItem href="/" isCurrent>
test_title
</BreadcrumbsItem>,
);
const anchorElement = dom.container.querySelector('a') as HTMLAnchorElement;

expect(anchorElement).toHaveTextContent('test_title');
expect(anchorElement).toBeDefined();
});
});
});
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;
6 changes: 5 additions & 1 deletion packages/web-react/src/components/Breadcrumbs/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import ReactDOM from 'react-dom/client';
import icons from '@lmc-eu/spirit-icons/dist/icons';
import DocsSection from '../../../../docs/DocsSections';
import { IconsProvider } from '../../../context';
import BreadcrumbsDefault from './BreadcrumbsDefault';
import BreadcrumbsCurrentWithoutLink from './BreadcrumbsCurrentWithoutLink';
import BreadcrumbsCustom from './BreadcrumbsCustom';
import BreadcrumbsDefault from './BreadcrumbsDefault';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
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">
<BreadcrumbsCurrentWithoutLink />
</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,13 +3,13 @@ import { ChildrenProps, StyleProps, TransferProps } from './shared';

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

export type BreadcrumbsItems = BreadcrumbsItem[];

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

0 comments on commit 6cf75ad

Please sign in to comment.