Skip to content

Commit 6d805c4

Browse files
HDaghashwendellhu95Laffery
authored
feat(module:breadcrumb): add nzRouteFn (#6313)
* feat(module:breadcrumb): add nzRouteFn * feat(module:breadcrumb): update docs * feat(module:breadcrumb): update docs * docs: add Chinese doc * Update breadcrumb.spec.ts to fix lint errors --------- Co-authored-by: wendellhu95 <wendellhu95@gmail.com> Co-authored-by: Laffery <49607541+Laffery@users.noreply.github.com>
1 parent 6d27073 commit 6d805c4

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

components/breadcrumb/breadcrumb.component.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class NzBreadCrumbComponent implements OnInit, OnDestroy, NzBreadcrumb {
6262
@Input() nzSeparator: string | TemplateRef<void> | null = '/';
6363
@Input() nzRouteLabel: string = 'breadcrumb';
6464
@Input() nzRouteLabelFn: (label: string) => string = label => label;
65+
@Input() nzRouteFn: (route: string) => string = route => route;
6566

6667
breadcrumbs: BreadcrumbOption[] = [];
6768
dir: Direction = 'ltr';
@@ -144,13 +145,13 @@ export class NzBreadCrumbComponent implements OnInit, OnDestroy, NzBreadcrumb {
144145
// Do not change nextUrl if routeUrl is falsy. This happens when it's a route lazy loading other modules.
145146
const nextUrl = routeUrl ? `${url}/${routeUrl}` : url;
146147
const breadcrumbLabel = this.nzRouteLabelFn(child.snapshot.data[this.nzRouteLabel]);
147-
148+
const shapedUrl = this.nzRouteFn(nextUrl);
148149
// If have data, go to generate a breadcrumb for it.
149150
if (routeUrl && breadcrumbLabel) {
150151
const breadcrumb: BreadcrumbOption = {
151152
label: breadcrumbLabel,
152153
params: child.snapshot.params,
153-
url: nextUrl
154+
url: shapedUrl
154155
};
155156
breadcrumbs.push(breadcrumb);
156157
}

components/breadcrumb/breadcrumb.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,26 @@ describe('breadcrumb', () => {
208208
});
209209
}));
210210

211+
it('should [nzRouteFn] work', fakeAsync(() => {
212+
TestBed.configureTestingModule({
213+
imports: [CommonModule, NzBreadCrumbModule, RouterTestingModule.withRoutes(customRouteLabelRoutes)],
214+
declarations: [NzBreadcrumbRouteWithCustomFnDemoComponent, NzBreadcrumbNullComponent]
215+
}).compileComponents();
216+
217+
fixture = TestBed.createComponent(NzBreadcrumbRouteWithCustomFnDemoComponent);
218+
breadcrumb = fixture.debugElement.query(By.directive(NzBreadCrumbComponent));
219+
220+
fixture.ngZone!.run(() => {
221+
router = TestBed.inject(Router);
222+
router.initialNavigation();
223+
224+
// Breadcrumb should conatin added params by nzRouteFn
225+
router.navigate(['one', 'two']);
226+
flushFixture(fixture);
227+
expect(breadcrumb.componentInstance.breadcrumbs[0].url).toContain('active=true');
228+
});
229+
}));
230+
211231
it('should route data breadcrumb navigate work', fakeAsync(() => {
212232
TestBed.configureTestingModule({
213233
imports: [CommonModule, NzBreadCrumbModule, RouterTestingModule.withRoutes(customRouteLabelRoutes)],
@@ -332,6 +352,16 @@ class NzBreadcrumbRouteLabelWithCustomFnDemoComponent {
332352
labelFn = (label: string): string => (label ? `${label} ${label}` : '');
333353
}
334354

355+
@Component({
356+
template: `
357+
<nz-breadcrumb [nzAutoGenerate]="true" [nzRouteLabel]="'customBreadcrumb'" [nzRouteFn]="routeFn"></nz-breadcrumb>
358+
<router-outlet></router-outlet>
359+
`
360+
})
361+
class NzBreadcrumbRouteWithCustomFnDemoComponent {
362+
routeFn = (route: string): string => `${route};active=true`;
363+
}
364+
335365
@Component({
336366
template: '<nz-breadcrumb [nzAutoGenerate]="true"></nz-breadcrumb>'
337367
})

components/breadcrumb/doc/index.en-US.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
2727
| `[nzSeparator]` | Custom separator | `string \| TemplateRef<void> \| null` | `'/'` |
2828
| `[nzAutoGenerate]` | Auto generate breadcrumb | `boolean` | `false` |
2929
| `[nzRouteLabel]` | Name of property that determines displayed text in routing config. It should be used when `nzAutoGenerate` is `true` | `string` | `'breadcrumb'` |
30-
| `[nzRouteLabelFn]` | Format breadcrumb item label text,normally used in international app to translate i18n key. It should be used when `nzAutoGenerate` is `true` | `(label:string) => string` | `label => label` |
30+
| `[nzRouteLabelFn]` | Format breadcrumb item label text, normally used in international app to translate i18n key. It should be used when `nzAutoGenerate` is `true` | `(label:string) => string` | `label => label` |
31+
| `[nzRouteFn]` | Format breadcrumb item route, normally used in international app to bind current params or query strings to avoid losing them while navigate using breadcrumb. It should be used when `nzAutoGenerate` is `true` | `(route:string) => route` | `route => route` |
3132

3233
Using `[nzAutoGenerate]` by configuring `data` like this:
3334

@@ -53,7 +54,7 @@ For lazy loading modules, you should write `data` in parent module like this:
5354
}
5455
```
5556

56-
use `nzRouteLabel` to custom `data` breadcrumb label:
57+
Use `nzRouteLabel` to custom `data` breadcrumb label:
5758

5859
```html
5960
<nz-breadcrumb [nzAutoGenerate]="true" [nzRouteLabel]="'customBreadcrumb'"></nz-breadcrumb>
@@ -69,7 +70,7 @@ use `nzRouteLabel` to custom `data` breadcrumb label:
6970
}
7071
```
7172

72-
use `nzRouteLabelFn` to format breadcrumb label in international application:
73+
Use `nzRouteLabelFn` to format breadcrumb label in international application:
7374

7475
```html
7576
<nz-breadcrumb [nzAutoGenerate]="true" [nzRouteLabel]="'breadcrumbI18nKey'" [nzRouteLabelFn]="translateFn"></nz-breadcrumb>
@@ -88,3 +89,27 @@ use `nzRouteLabelFn` to format breadcrumb label in international application:
8889
// In component
8990
translateFn = (key:string) => this.yourI18nService.translate(key);
9091
```
92+
93+
Use `nzRouteFn` to format or bind params and query strings to the route it self in international application:
94+
95+
```html
96+
<nz-breadcrumb [nzAutoGenerate]="true" [nzRouteLabel]="'breadcrumbI18nKey'" [nzRouteLabelFn]="translateFn" [nzRouteFn]="customRoute"></nz-breadcrumb>
97+
```
98+
99+
```ts
100+
// In component
101+
102+
bindCurrentParams(params, route) {
103+
let newRoute = route;
104+
for (const key in params) {
105+
if (params.hasOwnProperty(key)) {
106+
newRoute += `;${key}=${params[key]}`;
107+
}
108+
}
109+
return newRoute;
110+
}
111+
112+
const params = this.activatedRoute.snapshot.params;
113+
114+
customRoute = (route:string) => this.bindCurrentParams(params,route);
115+
```

components/breadcrumb/doc/index.zh-CN.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
2626
| --- | --- | --- | --- |
2727
| `[nzSeparator]` | 分隔符自定义 | `string \| TemplateRef<void> \| null` | `'/'` |
2828
| `[nzAutoGenerate]` | 自动生成 Breadcrumb | `boolean` | `false` |
29-
| `[nzRouteLabel]` | 自定义 route data 属性名称, `nzAutoGenerate``true` 时才生效 | `string` | `'breadcrumb'` |
30-
| `[nzRouteLabelFn]` | 格式化面包屑导航项的显示文字,通常用于在国际化应用中翻译键值, `nzAutoGenerate``true` 时才生效 | `(label:string) => string` | `label => label` |
29+
| `[nzRouteLabel]` | 自定义 route data 属性名称,`nzAutoGenerate``true` 时才生效 | `string` | `'breadcrumb'` |
30+
| `[nzRouteLabelFn]` | 格式化面包屑导航项的显示文字,通常用于在国际化应用中翻译键值, `nzAutoGenerate``true` 时才生效 | `(label:string) => string` | `label => label` |
31+
| `[nzRouteFn]` | 格式化面包屑路由格式,可用于为 URL 添加 query params,`nzAutoGenerate``true` 时才生效 | `(route:string) => route` | `route => route` |
3132

3233
使用 `[nzAutoGenerate]` 时,需要在路由类中定义 `data`:
3334

@@ -88,3 +89,28 @@ import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
8889
// In component
8990
translateFn = (key: string) => this.yourI18nService.translate(key);
9091
```
92+
93+
使用 `nzRouteFn` 来使用格式化 URL 或添加 query params:
94+
95+
96+
```html
97+
<nz-breadcrumb [nzAutoGenerate]="true" [nzRouteLabel]="'breadcrumbI18nKey'" [nzRouteLabelFn]="translateFn" [nzRouteFn]="customRoute"></nz-breadcrumb>
98+
```
99+
100+
```ts
101+
// In component
102+
103+
bindCurrentParams(params, route) {
104+
let newRoute = route;
105+
for (const key in params) {
106+
if (params.hasOwnProperty(key)) {
107+
newRoute += `;${key}=${params[key]}`;
108+
}
109+
}
110+
return newRoute;
111+
}
112+
113+
const params = this.activatedRoute.snapshot.params;
114+
115+
customRoute = (route:string) => this.bindCurrentParams(params,route);
116+
```

0 commit comments

Comments
 (0)