Skip to content

Commit ca9e2f8

Browse files
Keep static link behaviour on Breadcrumbs when no linkComponent passed
1 parent afa9673 commit ca9e2f8

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SciReactUI Changelog
1212
- Styles added to Navbar and Footer incorrectly remove built in styles.
1313

1414
### Changed
15-
- Breaking change Breadcrumbs component requires linkComponent prop for page routing.
15+
- Breadcrumbs component takes optional linkComponent prop for page routing.
1616

1717
[v0.1.0] - 2025-04-10
1818
---------------------

readme.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ A theme and component library to make websites at scientific installations simpl
88

99
Using
1010
-----
11-
1211
### Installing
1312

1413
Install as usual:
@@ -33,7 +32,19 @@ root.render(
3332

3433
There are currently two themes, `GenericTheme` or `DiamondTheme`, but you can - and should - adapt your own.
3534

36-
To use the Breadcrumbs component, use a route provider from your preferred library. For example, to use react-router's BrowserRouter, install react-router-dom:
35+
The Breadcrumbs supports either static links or the use of a routing library.
36+
To use static links, omit the linkComponent prop and Breadcrumbs will use a Link component with standard href attributes.
37+
38+
```js
39+
import { Breadcrumbs } from "@diamondlightsource/sci-react-ui";
40+
41+
function App() {
42+
return <Breadcrumbs path={window.location.pathname} />;
43+
}
44+
export default App;
45+
```
46+
47+
To use the Breadcrumbs component with your routing library, use a route provider from your preferred library. For example, to use react-router's BrowserRouter, install react-router-dom:
3748

3849
```sh
3950
npm i react-router-dom
@@ -57,7 +68,7 @@ root.render(
5768
)
5869
```
5970

60-
Then pass your library's corresponding Link component to Breadcrumbs, for example:
71+
Then pass your library's corresponding Link component to Breadcrumbs in the linkComponent prop, for example:
6172

6273
```js
6374
import { Link } from "react-router-dom";

src/components/navigation/Breadcrumbs.stories.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ export const Empty: Story = {
7575
},
7676
};
7777

78+
export const NoLinkComponent: Story = {
79+
args: {
80+
path: "/first/second/third/last/",
81+
},
82+
};
83+
84+
export const NoLinkComponentWithCustomPath: Story = {
85+
args: {
86+
path: [
87+
{ name: "first", href: "link" },
88+
{ name: "second", href: "other link" },
89+
{ name: "last", href: "/" },
90+
],
91+
},
92+
};
93+
7894
export const ColorChange: Story = {
7995
args: {
8096
path: ["first", "second", "third", "last"],

src/components/navigation/Breadcrumbs.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,27 @@ describe("getCrumbs", () => {
251251
).toStrictEqual(objectArrayCrumbs);
252252
});
253253
});
254+
255+
it("should render Link with href when linkComponent is not provided", () => {
256+
const { getByRole, getByText } = render(
257+
<Breadcrumbs path={defaultArrayObject} />,
258+
);
259+
260+
expect(getByRole("link", { name: crumbFirstTitle })).toHaveAttribute(
261+
"href",
262+
`/${crumbFirst}`,
263+
);
264+
expect(getByRole("link", { name: crumbSecondTitle })).toHaveAttribute(
265+
"href",
266+
`/${crumbSecond}`,
267+
);
268+
expect(getByText(crumbLastTitle)).toBeInTheDocument();
269+
});
270+
271+
it("should render home link with href when linkComponent is not provided", () => {
272+
const { getByTestId } = render(<Breadcrumbs path={defaultArrayObject} />);
273+
const homeIcon = getByTestId("HomeIcon");
274+
275+
expect(homeIcon).toBeInTheDocument();
276+
expect(homeIcon.parentElement).toHaveAttribute("href", "/");
277+
});

src/components/navigation/Breadcrumbs.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { CustomLink } from "types/links";
1515

1616
interface BreadcrumbsProps {
1717
path: string | string[] | CustomLink[];
18-
linkComponent: React.ElementType;
18+
linkComponent?: React.ElementType;
1919
rootProps?: PaperProps;
2020
muiBreadcrumbsProps?: Mui_BreadcrumbsProps;
2121
}
@@ -83,10 +83,19 @@ const Breadcrumbs = ({
8383
key={"crumb-0"}
8484
underline="hover"
8585
color="inherit"
86-
component={linkComponent}
87-
to="/"
86+
{...(linkComponent
87+
? {
88+
component: linkComponent,
89+
to: "/",
90+
}
91+
: {
92+
href: "/",
93+
})}
8894
>
89-
<HomeIcon sx={{ pt: 0.5, fontSize: "1.7em" }} />
95+
<HomeIcon
96+
data-testid="HomeIcon"
97+
sx={{ pt: 0.5, fontSize: "1.7em" }}
98+
/>
9099
</Mui_Link>
91100

92101
{crumbs.map((crumb, i, all) => {
@@ -97,8 +106,9 @@ const Breadcrumbs = ({
97106
sx={{ fontSize: "smaller" }}
98107
underline="hover"
99108
color="inherit"
100-
component={linkComponent}
101-
to={crumb.href}
109+
{...(linkComponent
110+
? { component: linkComponent, to: crumb.href }
111+
: { href: crumb.href })}
102112
>
103113
{crumb.name}
104114
</Mui_Link>

0 commit comments

Comments
 (0)