@@ -31,6 +31,7 @@ type Location = ReturnType<typeof useLocation>;
31
31
export interface Options {
32
32
disableDefaults ?: boolean ;
33
33
excludePaths ?: string [ ] ;
34
+ defaultFormatter ?: ( str : string ) => string
34
35
}
35
36
36
37
export interface BreadcrumbMatch < ParamKey extends string = string > {
@@ -197,9 +198,9 @@ const NO_BREADCRUMB = Symbol('NO_BREADCRUMB');
197
198
* we used to use the humanize-string package, but it added a lot of bundle
198
199
* size and issues with compilation. This 4-liner seems to cover most cases.
199
200
*/
200
- const humanize = ( str : string ) : string => str
201
+ export const humanize = ( str : string ) : string => str
201
202
. replace ( / ^ [ \s _ ] + | [ \s _ ] + $ / g, '' )
202
- . replace ( / [ _ \s ] + / g, ' ' )
203
+ . replace ( / [ - _ \s ] + / g, ' ' )
203
204
. replace ( / ^ [ a - z ] / , ( m ) => m . toUpperCase ( ) ) ;
204
205
205
206
/**
@@ -242,10 +243,12 @@ const getDefaultBreadcrumb = ({
242
243
currentSection,
243
244
location,
244
245
pathSection,
246
+ defaultFormatter,
245
247
} : {
246
248
currentSection : string ;
247
249
location : Location ;
248
250
pathSection : string ;
251
+ defaultFormatter ?: ( str : string ) => string
249
252
} ) : BreadcrumbData => {
250
253
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
251
254
const match = matchPath (
@@ -257,7 +260,7 @@ const getDefaultBreadcrumb = ({
257
260
) ! ;
258
261
259
262
return render ( {
260
- breadcrumb : humanize ( currentSection ) ,
263
+ breadcrumb : defaultFormatter ? defaultFormatter ( currentSection ) : humanize ( currentSection ) ,
261
264
match,
262
265
location,
263
266
} ) ;
@@ -271,13 +274,15 @@ const getBreadcrumbMatch = ({
271
274
currentSection,
272
275
disableDefaults,
273
276
excludePaths,
277
+ defaultFormatter,
274
278
location,
275
279
pathSection,
276
280
branches,
277
281
} : {
278
282
currentSection : string ;
279
283
disableDefaults ?: boolean ;
280
284
excludePaths ?: string [ ] ;
285
+ defaultFormatter ?: ( str : string ) => string
281
286
location : Location ;
282
287
pathSection : string ;
283
288
branches : BreadcrumbsRouteBranch [ ] ;
@@ -342,7 +347,8 @@ const getBreadcrumbMatch = ({
342
347
// Although we have a match, the user may be passing their react-router config object
343
348
// which we support. The route config object may not have a `breadcrumb` param specified.
344
349
// 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 ) ) ,
346
352
match : { ...match , route } ,
347
353
location,
348
354
props,
@@ -368,6 +374,7 @@ const getBreadcrumbMatch = ({
368
374
// include a "Home" breadcrumb by default (can be overrode or disabled in config).
369
375
currentSection : pathSection === '/' ? 'Home' : currentSection ,
370
376
location,
377
+ defaultFormatter,
371
378
} ) ;
372
379
} ;
373
380
0 commit comments