Skip to content

Commit

Permalink
fix: BreadcrumbItem when using to and element props (#2813)
Browse files Browse the repository at this point in the history
Small fix to support using router links as breadcrumbs

---------

Co-authored-by: Sondre Kvisli <[email protected]>
Co-authored-by: Tobias Høegh <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent cbbd9e5 commit e7eff2d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ showTabs: true
| `icon` | _(optional)_ Override icon displaying on the left side (Not recommended). Default: `chevron_left`. |
| `href` or `onClick` | _(optional)_ Set what happens when the user clicks on the item. |
| `skeleton` | _(optional)_ Applies loading skeleton. |

**NB:** When interactive, the item is an inherited [Button](/uilib/components/button/). You can therefore swap out the underlaying HTML element, by providing a new `element` or use other Button properties, such as `to` instead of `href`.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ const BreadcrumbItem = (localProps: BreadcrumbItemProps) => {
currentIcon = icon || determineSbankenIcon(variant, isSmallScreen)
}
const currentText = text || (variant === 'home' && homeText) || ''
const isInteractive = (href || onClick) && variant !== 'current'
const isInteractive =
(href || onClick || props.to) && variant !== 'current'
const style = { '--delay': String(itemNr) } as React.CSSProperties

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ describe('Breadcrumb', () => {
).toBeInTheDocument()
})

it('renders breadcrumbitem as a link if the to prop is given and element is a router link', () => {
const MockLink = React.forwardRef(
(props: { to: string; children: React.ReactNode }, ref) => (
<a
href={props.to}
ref={ref as React.RefObject<HTMLAnchorElement>}
>
{props.children}
</a>
)
)

render(<BreadcrumbItem to={'/url'} element={MockLink} text="Page" />)

expect(screen.getByRole('link')).toHaveAttribute('href', '/url')
})

it('fires onClick event', () => {
const onClick = jest.fn()
render(<BreadcrumbItem onClick={onClick} text="Page" />)
Expand Down

0 comments on commit e7eff2d

Please sign in to comment.