Skip to content

Commit dcbe4fd

Browse files
icd2k3jschrader-nr
andauthored
feat: add pass through props functionality and other cleanup items (#21)
Co-authored-by: Justin Schrader <[email protected]>
1 parent 7716b3d commit dcbe4fd

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -275,24 +275,28 @@ Now, `example.com/users/create` will display `create-breadcrumb` as expected, be
275275
## API
276276

277277
```js
278-
Route = {
278+
BreadcrumbsRoute = {
279279
path: String
280280
breadcrumb?: React.ComponentType | React.ElementType | string | null
281-
matchOptions?: { // see: https://reacttraining.com/react-router/web/api/matchPath
281+
// see: https://reacttraining.com/react-router/web/api/matchPath
282+
matchOptions?: {
282283
exact?: boolean
283284
strict?: boolean
284285
sensitive?: boolean
285286
}
286-
routes?: BreadcrumbsRoute[] // optional nested routes (for react-router config compatibility)
287+
// optional nested routes (for react-router config compatibility)
288+
routes?: BreadcrumbsRoute[],
289+
// optional props to be passed through directly to the breadcrumb component
290+
props?: { [x: string]: unknown };
287291
}
288292

289293
Options = {
290-
currentSection?: string
294+
// disable all default generation of breadcrumbs
291295
disableDefaults?: boolean
296+
// exclude certain paths fom generating breadcrumbs
292297
excludePaths?: string[]
293-
pathSection?: string
294298
}
295299

296300
// if routes are not passed, default breadcrumbs will be returned
297-
useBreadcrumbs(routes?: Route[], options?: Options): Array<React.node>
301+
useBreadcrumbs(routes?: BreadcrumbsRoute[], options?: Options): Array<React.node>
298302
```

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": "1.0.5",
3+
"version": "2.0.0",
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

+22-15
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ import React from 'react';
44
import PropTypes from 'prop-types';
55
import { mount } from 'enzyme';
66
import { MemoryRouter as Router } from 'react-router';
7-
import withBreadcrumbs, { getBreadcrumbs } from './index.tsx';
7+
import useBreadcrumbs, { getBreadcrumbs } from './index.tsx';
88

99
// imports to test compiled builds
10-
import withBreadcrumbsCompiledES, {
10+
import useBreadcrumbsCompiledES, {
1111
getBreadcrumbs as getBreadcrumbsCompiledES,
1212
} from '../dist/es/index';
13-
import withBreadcrumbsCompiledUMD, {
13+
import useBreadcrumbsCompiledUMD, {
1414
getBreadcrumbs as getBreadcrumbsCompiledUMD,
1515
} from '../dist/umd/index';
16-
import withBreadcrumbsCompiledCJS, {
16+
import useBreadcrumbsCompiledCJS, {
1717
getBreadcrumbs as getBreadcrumbsCompiledCJS,
1818
} from '../dist/cjs/index';
1919

2020
const components = {
21-
Breadcrumbs: ({ useBreadcrumbs, options, routes, ...forwardedProps }) => {
22-
const breadcrumbs = useBreadcrumbs(routes, options);
21+
Breadcrumbs: ({
22+
useBreadcrumbs: useBreadcrumbsHook,
23+
options,
24+
routes,
25+
...forwardedProps
26+
}) => {
27+
const breadcrumbs = useBreadcrumbsHook(routes, options);
2328

2429
return (
2530
<h1>
@@ -65,13 +70,13 @@ const components = {
6570
const getHOC = () => {
6671
switch (process.env.TEST_BUILD) {
6772
case 'cjs':
68-
return withBreadcrumbsCompiledCJS;
73+
return useBreadcrumbsCompiledCJS;
6974
case 'umd':
70-
return withBreadcrumbsCompiledUMD;
75+
return useBreadcrumbsCompiledUMD;
7176
case 'es':
72-
return withBreadcrumbsCompiledES;
77+
return useBreadcrumbsCompiledES;
7378
default:
74-
return withBreadcrumbs;
79+
return useBreadcrumbs;
7580
}
7681
};
7782

@@ -89,12 +94,12 @@ const getMethod = () => {
8994
};
9095

9196
const render = ({ options, pathname, routes, state, props }) => {
92-
const useBreadcrumbs = getHOC();
97+
const useBreadcrumbsHook = getHOC();
9398
const { Breadcrumbs } = components;
9499
const wrapper = mount(
95100
<Router initialIndex={0} initialEntries={[{ pathname, state }]}>
96101
<Breadcrumbs
97-
useBreadcrumbs={useBreadcrumbs}
102+
useBreadcrumbs={useBreadcrumbsHook}
98103
options={options}
99104
routes={routes}
100105
{...(props || {})}
@@ -348,8 +353,10 @@ describe('use-react-router-breadcrumbs', () => {
348353
{
349354
path: '/one',
350355
breadcrumb: components.BreadcrumbExtraPropsTest,
351-
foo: 'Pass through',
352-
bar: ' props',
356+
props: {
357+
foo: 'Pass through',
358+
bar: ' props',
359+
},
353360
},
354361
];
355362
const { breadcrumbs } = render({ pathname: '/one', routes });
@@ -418,7 +425,7 @@ describe('use-react-router-breadcrumbs', () => {
418425
location: { pathname: '/1' },
419426
})
420427
).toThrow(
421-
'withBreadcrumbs: `path` must be provided in every route object'
428+
'useBreadcrumbs: `path` must be provided in every route object'
422429
);
423430
});
424431
});

src/index.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ import { matchPath, useLocation } from 'react-router';
2424
type Location = ReturnType<typeof useLocation>;
2525

2626
export interface Options {
27-
currentSection?: string;
2827
disableDefaults?: boolean;
2928
excludePaths?: string[];
30-
pathSection?: string;
3129
}
3230

3331
export interface MatchOptions {
@@ -41,6 +39,7 @@ export interface BreadcrumbsRoute {
4139
breadcrumb?: React.ComponentType | React.ElementType | string | null;
4240
matchOptions?: MatchOptions;
4341
routes?: BreadcrumbsRoute[];
42+
props?: { [x: string]: unknown };
4443
}
4544

4645
export interface BreadcrumbData {
@@ -72,13 +71,19 @@ const render = ({
7271
breadcrumb: Breadcrumb,
7372
match,
7473
location,
75-
...rest
74+
props,
7675
}: {
7776
breadcrumb: React.ComponentType | string;
7877
match: { url: string };
7978
location: Location;
79+
props?: { [x: string]: unknown };
8080
}): BreadcrumbData => {
81-
const componentProps = { match, location, key: match.url, ...rest };
81+
const componentProps = {
82+
match,
83+
location,
84+
key: match.url,
85+
...(props || {}),
86+
};
8287

8388
return {
8489
...componentProps,
@@ -156,7 +161,7 @@ const getBreadcrumbMatch = ({
156161
({ breadcrumb: userProvidedBreadcrumb, matchOptions, path, ...rest }) => {
157162
if (!path) {
158163
throw new Error(
159-
'withBreadcrumbs: `path` must be provided in every route object'
164+
'useBreadcrumbs: `path` must be provided in every route object'
160165
);
161166
}
162167

0 commit comments

Comments
 (0)