react-router-breadcrumbs-hoc
Version:
small, flexible, higher order component for rendering breadcrumbs with react-router 4.x
2 lines (1 loc) • 2.97 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@babel/runtime/helpers/toConsumableArray"),require("react"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","@babel/runtime/helpers/toConsumableArray","react","react-router-dom"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["react-router-breadcrumbs-hoc"]={},t._toConsumableArray,t.React,t.ReactRouterDom)}(this,(function(t,e,r,n){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var a=o(e),c=o(r);function u(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r}var i={exact:!0},s="NO_BREADCRUMB",l=function(t){return t.replace(/^[\s_]+|[\s_]+$/g,"").replace(/[_\s]+/g," ").replace(/^[a-z]/,(function(t){return t.toUpperCase()}))},b=function(t){var e=t.breadcrumb,n=t.match,o=t.location,a=u(t,["breadcrumb","match","location"]),i=Object.assign({match:n,location:o,key:n.url},a);return Object.assign(Object.assign({},i),{breadcrumb:"string"==typeof e?r.createElement("span",{key:i.key},e):c.default.createElement(e,Object.assign({},i))})},f=function(t){var e,r=t.currentSection,o=t.disableDefaults,a=t.excludePaths,c=t.location,f=t.pathSection,p=t.routes;return a&&a.some((function(t){return n.matchPath(f,{path:t,exact:!0,strict:!1})}))?s:(p.some((function(t){var a=t.breadcrumb,p=t.matchOptions,m=t.path,d=u(t,["breadcrumb","matchOptions","path"]);if(!m)throw new Error("withBreadcrumbs: `path` must be provided in every route object");var h=n.matchPath(f,Object.assign(Object.assign({},p||i),{path:m}));return h&&null===a||!h&&p?(e=s,!0):!!h&&(!a&&o?(e=s,!0):(e=b(Object.assign({breadcrumb:a||l(r),match:h,location:c},d)),!0))})),e||(o?s:function(t){var e=t.currentSection,r=t.location,o=t.pathSection,a=n.matchPath(o,Object.assign(Object.assign({},i),{path:o}))||{url:"not-found"};return b({breadcrumb:l(e),match:a,location:r})}({pathSection:f,currentSection:"/"===f?"Home":r,location:c})))},p=function(t){var e=t.routes,r=t.location,n=t.options,o=void 0===n?{}:n,a=[];return r.pathname.split("?")[0].split("/").reduce((function(t,n,c){var u=n?"".concat(t,"/").concat(n):"/";if("/"===u&&0!==c)return"";var i=f(Object.assign({currentSection:n,location:r,pathSection:u,routes:e},o));return i!==s&&a.push(i),"/"===u?"":u}),""),a},m=function t(e){return e.reduce((function(e,r){return r.routes?e.concat([r].concat(a.default(t(r.routes)))):e.concat(r)}),[])};t.default=function(t,e){return function(r){return function(o){return c.default.createElement(r,Object.assign(Object.assign({},o),{breadcrumbs:p({options:e,routes:m(t||[]),location:n.useLocation()})}))}}},t.getBreadcrumbs=p,Object.defineProperty(t,"__esModule",{value:!0})}));