Skip to content

Commit

Permalink
Add option to completely disable default breadcrumb generation
Browse files Browse the repository at this point in the history
  • Loading branch information
icd2k3 committed Mar 17, 2018
1 parent c64b102 commit 32e4248
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 30 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -130,11 +130,15 @@ const EditorBreadcrumb = ({ location: { state: { isNew } } }) => (

This package will attempt to create breadcrumbs for you based on the route section via [humanize-string](https://github.com/sindresorhus/humanize-string). For example `/users` will auotmatically create the breadcrumb `"Users"`. There are two ways to disable default breadcrumbs for a path:

**Option 1:** Pass `breadcrumb: null` in the routes config:
**Option 1:** Disable _all_ default breadcrumb generation by passing `disableDefaults: true` in the `options` object

`withBreadcrumbs(routes, { disableDefaults: true })`

**Option 2:** Disable _individual_ default breadcrumbs by passing `breadcrumb: null` in route config:

`{ path: '/a/b', breadcrumb: null }`

**Option 2:** Or, Pass an `excludePaths` array in the `options` object
**Option 3:** Disable _individual_ default breadcrumbs by passing an `excludePaths` array in the `options` object

`withBreadcrumbs(routes, { excludePaths: ['/', '/no-breadcrumb/for-this-route'] })`

Expand Down
8 changes: 4 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "react-router-breadcrumbs-hoc",
"version": "2.0.1",
"version": "2.1.0",
"description": "Just a tiny, flexible, higher order component for rendering breadcrumbs with react-router 4.x",
"repository": "icd2k3/react-router-breadcrumbs-hoc",
"keywords": [
Expand Down Expand Up @@ -35,7 +35,7 @@
"coveralls": "^3.0.0",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"eslint": "^4.18.2",
"eslint": "^4.19.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
Expand All @@ -46,9 +46,9 @@
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"rollup": "^0.56.5",
"rollup": "^0.57.1",
"rollup-plugin-babel": "^3.0.3",
"rollup-plugin-commonjs": "^9.0.0",
"rollup-plugin-commonjs": "^9.1.0",
"rollup-plugin-node-resolve": "^3.2.0"
},
"scripts": {
Expand Down
34 changes: 25 additions & 9 deletions src/index.js
Expand Up @@ -30,6 +30,7 @@ const getDefaultBreadcrumb = ({ pathSection, currentSection, location }) => {
// a user-provided breadcrumb OR a sensible default via `humanize-string`
const getBreadcrumb = ({
currentSection,
disableDefaults,
excludePaths,
location,
pathSection,
Expand Down Expand Up @@ -60,6 +61,14 @@ const getBreadcrumb = ({
}

if (match) {
// this covers the case where a user may be extending their react-router route
// config with breadcrumbs, but also does not want default breadcrumbs to be
// automatically generated (opt-in)
if (!userProvidedBreadcrumb && disableDefaults) {
breadcrumb = NO_BREADCRUMB;
return true;
}

breadcrumb = render({
// although we have a match, the user may be passing their react-router config object
// which we support. The route config object may not have a `breadcrumb` param specified.
Expand All @@ -73,14 +82,21 @@ const getBreadcrumb = ({
return false;
});

// if there are no breadcrumbs provided in the routes array we return a default breadcrumb instead
return breadcrumb
|| getDefaultBreadcrumb({
pathSection,
// include a "Home" breadcrumb by default (can be overrode or disabled in config)
currentSection: pathSection === '/' ? 'Home' : currentSection,
location,
});
if (breadcrumb) {
// user provided a breadcrumb prop, or we generated one via humanizeString above ~L75
return breadcrumb;
} else if (disableDefaults) {
// if there was no breadcrumb provided and user has disableDefaults turned on
return NO_BREADCRUMB;
}

// if the above conditionals don't fire, generate a default breadcrumb based on the path
return getDefaultBreadcrumb({
pathSection,
// include a "Home" breadcrumb by default (can be overrode or disabled in config)
currentSection: pathSection === '/' ? 'Home' : currentSection,
location,
});
};

export const getBreadcrumbs = ({ routes, location, options = {} }) => {
Expand All @@ -102,10 +118,10 @@ export const getBreadcrumbs = ({ routes, location, options = {} }) => {

const breadcrumb = getBreadcrumb({
currentSection,
excludePaths: options.excludePaths,
location,
pathSection,
routes,
...options,
});

// add the breadcrumb to the matches array
Expand Down
9 changes: 9 additions & 0 deletions src/index.test.js
Expand Up @@ -234,6 +234,15 @@ describe('react-router-breadcrumbs-hoc', () => {
expect(breadcrumbs).toBe('Two');
});
});

describe('disableDefaults', () => {
it('Should disable all default breadcrumb generation', () => {
const routes = [{ path: '/one', breadcrumb: 'One' }, { path: '/one/two' }];
const { breadcrumbs } = render({ pathname: '/one/two', routes, options: { disableDefaults: true } });

expect(breadcrumbs).toBe('One');
});
});
});

describe('Invalid route object', () => {
Expand Down
116 changes: 101 additions & 15 deletions yarn.lock
Expand Up @@ -71,6 +71,16 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"

"@types/acorn@^4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/acorn/-/acorn-4.0.3.tgz#d1f3e738dde52536f9aad3d3380d14e448820afd"
dependencies:
"@types/estree" "*"

"@types/estree@*", "@types/estree@0.0.38":
version "0.0.38"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.38.tgz#c1be40aa933723c608820a99a373a16d215a1ca2"

"@types/node@*":
version "9.4.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.6.tgz#d8176d864ee48753d053783e4e463aec86b8d82e"
Expand All @@ -83,6 +93,12 @@ abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"

acorn-dynamic-import@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-3.0.0.tgz#901ceee4c7faaef7e07ad2a47e890675da50a278"
dependencies:
acorn "^5.0.0"

acorn-globals@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-4.1.0.tgz#ab716025dbe17c54d3ef81d32ece2b2d99fe2538"
Expand All @@ -99,10 +115,14 @@ acorn@^3.0.4:
version "3.3.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"

acorn@^5.0.0, acorn@^5.3.0, acorn@^5.4.0:
acorn@^5.0.0, acorn@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.0.tgz#1abb587fbf051f94e3de20e6b26ef910b1828298"

acorn@^5.5.0, acorn@^5.5.3:
version "5.5.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"

ajv-keywords@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762"
Expand Down Expand Up @@ -1362,6 +1382,12 @@ dashdash@^1.12.0:
dependencies:
assert-plus "^1.0.0"

date-time@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/date-time/-/date-time-2.1.0.tgz#0286d1b4c769633b3ca13e1e62558d2dbdc2eba2"
dependencies:
time-zone "^1.0.0"

debug@^2.2.0, debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -1676,9 +1702,9 @@ eslint-visitor-keys@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#3f3180fb2e291017716acb4c9d6d5b5c34a6a81d"

eslint@^4.18.2:
version "4.18.2"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.18.2.tgz#0f81267ad1012e7d2051e186a9004cc2267b8d45"
eslint@^4.19.0:
version "4.19.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.19.0.tgz#9e900efb5506812ac374557034ef6f5c3642fc4c"
dependencies:
ajv "^5.3.0"
babel-code-frame "^6.22.0"
Expand All @@ -1689,7 +1715,7 @@ eslint@^4.18.2:
doctrine "^2.1.0"
eslint-scope "^3.7.1"
eslint-visitor-keys "^1.0.0"
espree "^3.5.2"
espree "^3.5.4"
esquery "^1.0.0"
esutils "^2.0.2"
file-entry-cache "^2.0.0"
Expand All @@ -1711,18 +1737,19 @@ eslint@^4.18.2:
path-is-inside "^1.0.2"
pluralize "^7.0.0"
progress "^2.0.0"
regexpp "^1.0.1"
require-uncached "^1.0.3"
semver "^5.3.0"
strip-ansi "^4.0.0"
strip-json-comments "~2.0.1"
table "4.0.2"
text-table "~0.2.0"

espree@^3.5.2:
version "3.5.3"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.3.tgz#931e0af64e7fbbed26b050a29daad1fc64799fa6"
espree@^3.5.4:
version "3.5.4"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.4.tgz#b0f447187c8a8bed944b815a660bddf5deb5d1a7"
dependencies:
acorn "^5.4.0"
acorn "^5.5.0"
acorn-jsx "^3.0.0"

esprima@^3.1.3:
Expand Down Expand Up @@ -2295,6 +2322,10 @@ invert-kv@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"

irregular-plurals@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/irregular-plurals/-/irregular-plurals-1.4.0.tgz#2ca9b033651111855412f16be5d77c62a458a766"

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
Expand Down Expand Up @@ -2425,6 +2456,12 @@ is-promise@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa"

is-reference@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.1.0.tgz#50e6ef3f64c361e2c53c0416cdc9420037f2685b"
dependencies:
"@types/estree" "0.0.38"

is-regex@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491"
Expand Down Expand Up @@ -2973,6 +3010,10 @@ load-json-file@^2.0.0:
pify "^2.0.0"
strip-bom "^3.0.0"

locate-character@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-2.0.5.tgz#f2d2614d49820ecb3c92d80d193b8db755f74c0f"

locate-path@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"
Expand Down Expand Up @@ -3373,6 +3414,10 @@ parse-json@^2.2.0:
dependencies:
error-ex "^1.2.0"

parse-ms@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-1.0.1.tgz#56346d4749d78f23430ca0c713850aef91aa361d"

parse5@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
Expand Down Expand Up @@ -3463,6 +3508,12 @@ pkg-dir@^2.0.0:
dependencies:
find-up "^2.1.0"

plur@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/plur/-/plur-2.1.2.tgz#7482452c1a0f508e3e344eaec312c91c29dc655a"
dependencies:
irregular-plurals "^1.0.0"

pluralize@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777"
Expand All @@ -3486,6 +3537,13 @@ pretty-format@^22.4.0:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

pretty-ms@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/pretty-ms/-/pretty-ms-3.1.0.tgz#e9cac9c76bf6ee52fe942dd9c6c4213153b12881"
dependencies:
parse-ms "^1.0.0"
plur "^2.1.2"

private@^0.1.6, private@^0.1.7:
version "0.1.8"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
Expand Down Expand Up @@ -3706,6 +3764,10 @@ regex-cache@^0.4.2:
dependencies:
is-equal-shallow "^0.1.3"

regexpp@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-1.0.1.tgz#d857c3a741dce075c2848dcb019a0a975b190d43"

regexpu-core@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-2.0.0.tgz#49d038837b8dcf8bfa5b9a42139938e6ea2ae240"
Expand Down Expand Up @@ -3818,6 +3880,10 @@ require-main-filename@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"

require-relative@^0.8.7:
version "0.8.7"
resolved "https://registry.yarnpkg.com/require-relative/-/require-relative-0.8.7.tgz#7999539fc9e047a37928fa196f8e1563dabd36de"

require-uncached@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/require-uncached/-/require-uncached-1.0.3.tgz#4e0d56d6c9662fd31e43011c4b95aa49955421d3"
Expand Down Expand Up @@ -3882,9 +3948,9 @@ rollup-plugin-babel@^3.0.3:
dependencies:
rollup-pluginutils "^1.5.0"

rollup-plugin-commonjs@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.0.0.tgz#9118e9041b67cdd2a6d8c59dda6349556dce3d0c"
rollup-plugin-commonjs@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.1.0.tgz#468341aab32499123ee9a04b22f51d9bf26fdd94"
dependencies:
estree-walker "^0.5.1"
magic-string "^0.22.4"
Expand Down Expand Up @@ -3913,9 +3979,21 @@ rollup-pluginutils@^2.0.1:
estree-walker "^0.3.0"
micromatch "^2.3.11"

rollup@^0.56.5:
version "0.56.5"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.56.5.tgz#40fe3cf0cd1659d469baad11f4d5b6336c14ce84"
rollup@^0.57.1:
version "0.57.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.57.1.tgz#0bb28be6151d253f67cf4a00fea48fb823c74027"
dependencies:
"@types/acorn" "^4.0.3"
acorn "^5.5.3"
acorn-dynamic-import "^3.0.0"
date-time "^2.1.0"
is-reference "^1.1.0"
locate-character "^2.0.5"
pretty-ms "^3.1.0"
require-relative "^0.8.7"
rollup-pluginutils "^2.0.1"
signal-exit "^3.0.2"
sourcemap-codec "^1.4.1"

rst-selector-parser@^2.2.3:
version "2.2.3"
Expand Down Expand Up @@ -4044,6 +4122,10 @@ source-map@^0.6.0, source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"

sourcemap-codec@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.1.tgz#c8fd92d91889e902a07aee392bdd2c5863958ba2"

spdx-correct@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.0.0.tgz#05a5b4d7153a195bc92c3c425b69f3b2a9524c82"
Expand Down Expand Up @@ -4228,6 +4310,10 @@ through@^2.3.6:
version "2.3.8"
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"

time-zone@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/time-zone/-/time-zone-1.0.0.tgz#99c5bf55958966af6d06d83bdf3800dc82faec5d"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down

0 comments on commit 32e4248

Please sign in to comment.