@@ -31,6 +31,7 @@ type Location = ReturnType<typeof useLocation>;
3131export interface Options {
3232 disableDefaults ?: boolean ;
3333 excludePaths ?: string [ ] ;
34+ defaultFormatter ?: ( str : string ) => string
3435}
3536
3637export interface BreadcrumbMatch < ParamKey extends string = string > {
@@ -197,9 +198,9 @@ const NO_BREADCRUMB = Symbol('NO_BREADCRUMB');
197198 * we used to use the humanize-string package, but it added a lot of bundle
198199 * size and issues with compilation. This 4-liner seems to cover most cases.
199200 */
200- const humanize = ( str : string ) : string => str
201+ export const humanize = ( str : string ) : string => str
201202 . replace ( / ^ [ \s _ ] + | [ \s _ ] + $ / g, '' )
202- . replace ( / [ _ \s ] + / g, ' ' )
203+ . replace ( / [ - _ \s ] + / g, ' ' )
203204 . replace ( / ^ [ a - z ] / , ( m ) => m . toUpperCase ( ) ) ;
204205
205206/**
@@ -242,10 +243,12 @@ const getDefaultBreadcrumb = ({
242243 currentSection,
243244 location,
244245 pathSection,
246+ defaultFormatter,
245247} : {
246248 currentSection : string ;
247249 location : Location ;
248250 pathSection : string ;
251+ defaultFormatter ?: ( str : string ) => string
249252} ) : BreadcrumbData => {
250253 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
251254 const match = matchPath (
@@ -257,7 +260,7 @@ const getDefaultBreadcrumb = ({
257260 ) ! ;
258261
259262 return render ( {
260- breadcrumb : humanize ( currentSection ) ,
263+ breadcrumb : defaultFormatter ? defaultFormatter ( currentSection ) : humanize ( currentSection ) ,
261264 match,
262265 location,
263266 } ) ;
@@ -271,13 +274,15 @@ const getBreadcrumbMatch = ({
271274 currentSection,
272275 disableDefaults,
273276 excludePaths,
277+ defaultFormatter,
274278 location,
275279 pathSection,
276280 branches,
277281} : {
278282 currentSection : string ;
279283 disableDefaults ?: boolean ;
280284 excludePaths ?: string [ ] ;
285+ defaultFormatter ?: ( str : string ) => string
281286 location : Location ;
282287 pathSection : string ;
283288 branches : BreadcrumbsRouteBranch [ ] ;
@@ -342,7 +347,8 @@ const getBreadcrumbMatch = ({
342347 // Although we have a match, the user may be passing their react-router config object
343348 // which we support. The route config object may not have a `breadcrumb` param specified.
344349 // If this is the case, we should provide a default via `humanize`.
345- breadcrumb : userProvidedBreadcrumb || humanize ( currentSection ) ,
350+ breadcrumb : userProvidedBreadcrumb
351+ || ( defaultFormatter ? defaultFormatter ( currentSection ) : humanize ( currentSection ) ) ,
346352 match : { ...match , route } ,
347353 location,
348354 props,
@@ -368,6 +374,7 @@ const getBreadcrumbMatch = ({
368374 // include a "Home" breadcrumb by default (can be overrode or disabled in config).
369375 currentSection : pathSection === '/' ? 'Home' : currentSection ,
370376 location,
377+ defaultFormatter,
371378 } ) ;
372379} ;
373380
0 commit comments