Skip to content

Commit cf78c15

Browse files
author
Justin Schrader
authored
Merge pull request #23 from mrchief/patch-1
Pass location object
2 parents 48660d5 + 984b482 commit cf78c15

File tree

6 files changed

+97
-9
lines changed

6 files changed

+97
-9
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,20 @@ Breadcrumb = {
9797
breadcrumb: Component
9898
}
9999

100+
// react-router's location object: https://reacttraining.com/react-router/web/api/location
101+
Location = {
102+
key: String
103+
pathname: String
104+
search: String
105+
hash: String
106+
state: Object
107+
}
108+
100109
withBreadcrumbs(routes: Array<Route>): HigherOrderComponent
101110

102111
// you shouldn't ever really have to use `getBreadcrumbs`, but it's
103112
// exported for convenience if you don't want to use the HOC
104-
getBreadcrumbs({ routes: Array<Route>, pathname: String }): Array<Breadcrumb>
113+
getBreadcrumbs({ routes: Array<Route>, location: Location }): Array<Breadcrumb>
105114
```
106115

107116
## Order Matters!
@@ -129,3 +138,27 @@ To get the right breadcrumbs, simply change the order:
129138
```
130139

131140
Now, `/users/create` will match the create breadcrumb first, and all others will fall through to `/:id`.
141+
142+
## Dynamic Breadcrumbs
143+
React Router's [location](https://reacttraining.com/react-router/web/api/location) object let's you pass `state` property. Using the `state` allows one to update the Breadcrumb to display dynamic info at runtime. Consider this example:
144+
145+
```jsx
146+
// Breadcrumb.js
147+
const Breadcrumb = ({location: { state: { isNew } } }) => {
148+
return <span>{isNew ? 'Add New' : 'Update'}</span> // lets one dynamically update Breadcrumb based on some state info
149+
}
150+
151+
// routes
152+
{
153+
pathname: '/editor',
154+
breadcrumb: Breadcrumb
155+
...
156+
}
157+
158+
// Component
159+
// Oversimplified example showing usage of state props
160+
<Link to={{pathname: '/editor'}}>Edit</Link>} // Upon navigation, breadcrumb will display: Update
161+
<Link to={{pathname: '/editor', state: {isNew: true}}}>Add</Link>} // Upon navigation, breadcrumb will display: Add New
162+
```
163+
164+
Now based on what you pass in the `state` prop, the Breadcrumb will display dynamic values at runtime!

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-router-breadcrumbs-hoc",
3-
"version": "1.1.2",
3+
"version": "2.0.0",
44
"description": "Just a tiny, flexible, higher order component for rendering breadcrumbs with react-router 4.x",
55
"repository": "icd2k3/react-router-breadcrumbs-hoc",
66
"keywords": [
@@ -30,6 +30,7 @@
3030
"babel-preset-react": "^6.24.1",
3131
"babel-preset-stage-1": "^6.24.1",
3232
"coveralls": "^3.0.0",
33+
"cross-env": "^5.1.3",
3334
"enzyme": "^3.2.0",
3435
"enzyme-adapter-react-16": "^1.1.0",
3536
"enzyme-to-json": "^3.3.0",
@@ -52,7 +53,7 @@
5253
"scripts": {
5354
"prepublishOnly": "npm run build",
5455
"build": "rollup -c",
55-
"test": "NODE_ENV=test jest",
56+
"test": "cross-env NODE_ENV=test jest",
5657
"travis": "jest && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
5758
"lint": "eslint ./src/**"
5859
}

src/__snapshots__/index.test.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
274274
},
275275
Object {
276276
"breadcrumb": <breadcrumb
277+
location={
278+
Object {
279+
"hash": "",
280+
"pathname": "/1/2/3/4",
281+
"search": "",
282+
}
283+
}
277284
match={
278285
Object {
279286
"isExact": true,
@@ -293,6 +300,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
293300
},
294301
Object {
295302
"breadcrumb": <BreadcrumbMatchTest
303+
location={
304+
Object {
305+
"hash": "",
306+
"pathname": "/1/2/3/4",
307+
"search": "",
308+
}
309+
}
296310
match={
297311
Object {
298312
"isExact": true,
@@ -316,6 +330,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
316330
},
317331
Object {
318332
"breadcrumb": <BreadcrumbNavLinkTest
333+
location={
334+
Object {
335+
"hash": "",
336+
"pathname": "/1/2/3/4",
337+
"search": "",
338+
}
339+
}
319340
match={
320341
Object {
321342
"isExact": true,
@@ -391,6 +412,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
391412
key="/1/22"
392413
>
393414
<breadcrumb
415+
location={
416+
Object {
417+
"hash": "",
418+
"pathname": "/1/2/3/4",
419+
"search": "",
420+
}
421+
}
394422
match={
395423
Object {
396424
"isExact": true,
@@ -409,6 +437,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
409437
key="/1/2/:number3"
410438
>
411439
<BreadcrumbMatchTest
440+
location={
441+
Object {
442+
"hash": "",
443+
"pathname": "/1/2/3/4",
444+
"search": "",
445+
}
446+
}
412447
match={
413448
Object {
414449
"isExact": true,
@@ -429,6 +464,13 @@ exports[`react-router-breadcrumbs-hoc Valid routes Should render breadcrumb comp
429464
key="/1/2/:number/44"
430465
>
431466
<BreadcrumbNavLinkTest
467+
location={
468+
Object {
469+
"hash": "",
470+
"pathname": "/1/2/3/4",
471+
"search": "",
472+
}
473+
}
432474
match={
433475
Object {
434476
"isExact": true,

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ const DEFAULT_MATCH_OPTIONS = { exact: true };
55

66
// if user is passing a function (component) as a breadcrumb, make sure we
77
// pass the match object into it. Else just return the string.
8-
const renderer = ({ breadcrumb, match }) => {
8+
const renderer = ({ breadcrumb, match, location }) => {
99
if (typeof breadcrumb === 'function') {
10-
return createElement(breadcrumb, { match });
10+
return createElement(breadcrumb, { match, location });
1111
}
1212
return breadcrumb;
1313
};
1414

15-
export const getBreadcrumbs = ({ routes, pathname }) => {
15+
export const getBreadcrumbs = ({ routes, location }) => {
1616
const matches = [];
17+
const { pathname } = location;
1718

1819
pathname
1920
// remove trailing slash "/" from pathname (avoids multiple of the same match)
@@ -42,7 +43,7 @@ export const getBreadcrumbs = ({ routes, pathname }) => {
4243
// and match object to add to the `matches` array
4344
if (match) {
4445
breadcrumbMatch = {
45-
breadcrumb: renderer({ breadcrumb, match }),
46+
breadcrumb: renderer({ breadcrumb, match, location }),
4647
path,
4748
match,
4849
};
@@ -67,7 +68,7 @@ export const withBreadcrumbs = routes => Component => withRouter(props =>
6768
createElement(Component, {
6869
...props,
6970
breadcrumbs: getBreadcrumbs({
70-
pathname: props.location.pathname,
7171
routes,
72+
location: props.location,
7273
}),
7374
}));

src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('react-router-breadcrumbs-hoc', () => {
131131

132132
describe('Invalid route object', () => {
133133
it('Should error if `path` is not provided', () => {
134-
expect(() => getBreadcrumbs({ routes: [{ breadcrumb: 'Yo' }], pathname: '/1' }))
134+
expect(() => getBreadcrumbs({ routes: [{ breadcrumb: 'Yo' }], location: { pathname: '/1' } }))
135135
.toThrow('withBreadcrumbs: `path` must be provided in every route object');
136136
});
137137
});

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,13 @@ coveralls@^3.0.0:
13471347
minimist "^1.2.0"
13481348
request "^2.79.0"
13491349

1350+
cross-env@^5.1.3:
1351+
version "5.1.3"
1352+
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.1.3.tgz#f8ae18faac87692b0a8b4d2f7000d4ec3a85dfd7"
1353+
dependencies:
1354+
cross-spawn "^5.1.0"
1355+
is-windows "^1.0.0"
1356+
13501357
cross-spawn@^5.0.1, cross-spawn@^5.1.0:
13511358
version "5.1.0"
13521359
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -2557,6 +2564,10 @@ is-utf8@^0.2.0:
25572564
version "0.2.1"
25582565
resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72"
25592566

2567+
is-windows@^1.0.0:
2568+
version "1.0.2"
2569+
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
2570+
25602571
isarray@0.0.1:
25612572
version "0.0.1"
25622573
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

0 commit comments

Comments
 (0)