Skip to content

Commit 4fa09f5

Browse files
icd2k3jschrader-nr
andauthored
fix: support layout routes in v6 (#35)
Co-authored-by: Justin Schrader <[email protected]>
1 parent bc717c0 commit 4fa09f5

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-react-router-breadcrumbs",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A hook for displaying and setting breadcrumbs for react router",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",

src/index.test.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const components = {
6565
return <span>Class</span>;
6666
}
6767
},
68+
Layout: React.memo(({ children }) => <div>{children}</div>),
6869
};
6970

7071
const getHOC = () => {
@@ -129,15 +130,23 @@ components.Breadcrumbs.propTypes = {
129130
routes: PropTypes.arrayOf(
130131
PropTypes.oneOfType([
131132
PropTypes.shape({
132-
path: PropTypes.string.isRequired,
133+
path: PropTypes.string,
133134
breadcrumb: PropTypes.oneOfType([
134135
PropTypes.node,
135136
PropTypes.func,
136137
PropTypes.object,
137138
]),
138139
}),
139140
PropTypes.shape({
140-
index: PropTypes.bool.isRequired,
141+
index: PropTypes.bool,
142+
breadcrumb: PropTypes.oneOfType([
143+
PropTypes.node,
144+
PropTypes.func,
145+
PropTypes.object,
146+
]),
147+
}),
148+
PropTypes.shape({
149+
children: PropTypes.arrayOf(PropTypes.shape()).isRequired,
141150
breadcrumb: PropTypes.oneOfType([
142151
PropTypes.node,
143152
PropTypes.func,
@@ -174,6 +183,14 @@ components.BreadcrumbExtraPropsTest.propTypes = {
174183
bar: PropTypes.string.isRequired,
175184
};
176185

186+
components.Layout.propTypes = {
187+
children: PropTypes.node,
188+
};
189+
190+
components.Layout.defaultProps = {
191+
children: null,
192+
};
193+
177194
describe('use-react-router-breadcrumbs', () => {
178195
describe('Valid routes', () => {
179196
it('Should render breadcrumb components as expected', () => {
@@ -314,6 +331,26 @@ describe('use-react-router-breadcrumbs', () => {
314331
expect(breadcrumbs).toBe('Home / One / TwoCustom / ThreeCustom');
315332
});
316333

334+
it('Should allow layout routes', () => {
335+
const routes = [
336+
{
337+
element: <components.Layout />,
338+
children: [
339+
{
340+
path: 'about',
341+
breadcrumb: 'About',
342+
},
343+
],
344+
},
345+
{
346+
index: true,
347+
breadcrumb: 'Home',
348+
},
349+
];
350+
const { breadcrumbs } = render({ pathname: '/about', routes });
351+
expect(breadcrumbs).toBe('Home / About');
352+
});
353+
317354
it('Should use the breadcrumb provided by parent if the index route dose not provide one', () => {
318355
const routes = [
319356
{

src/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function flattenRoutes(
134134
parentPath = '',
135135
): BreadcrumbsRouteBranch[] {
136136
routes.forEach((route, index) => {
137-
if (typeof route.path !== 'string' && !route.index) {
137+
if (typeof route.path !== 'string' && !route.index && !route.children?.length) {
138138
throw new Error(
139139
'useBreadcrumbs: `path` or `index` must be provided in every route object',
140140
);

0 commit comments

Comments
 (0)