Skip to content

Commit 24b4e83

Browse files
authored
fix(module:cascader): fix zoneless NG0100 issue (#9504)
1 parent 1592187 commit 24b4e83

28 files changed

+97
-279
lines changed

components/cascader/cascader.component.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
137137
[mirrorSync]="true"
138138
[disabled]="nzDisabled"
139139
[autofocus]="nzAutoFocus"
140-
[focusTrigger]="menuVisible"
140+
[focusTrigger]="menuVisible()"
141141
/>
142142
</div>
143143
</div>
@@ -150,7 +150,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
150150
[mirrorSync]="false"
151151
[disabled]="nzDisabled"
152152
[autofocus]="nzAutoFocus"
153-
[focusTrigger]="menuVisible"
153+
[focusTrigger]="menuVisible()"
154154
/>
155155
156156
@if (showLabelRender) {
@@ -175,7 +175,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
175175
@if (nzShowArrow) {
176176
<span class="ant-select-arrow" [class.ant-select-arrow-loading]="isLoading">
177177
@if (!isLoading) {
178-
<nz-icon [nzType]="$any(nzSuffixIcon)" [class.ant-cascader-picker-arrow-expand]="menuVisible" />
178+
<nz-icon [nzType]="$any(nzSuffixIcon)" [class.ant-cascader-picker-arrow-expand]="menuVisible()" />
179179
} @else {
180180
<nz-icon nzType="loading" />
181181
}
@@ -198,7 +198,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
198198
[cdkConnectedOverlayOrigin]="overlayOrigin"
199199
[cdkConnectedOverlayPositions]="positions"
200200
[cdkConnectedOverlayTransformOriginOn]="'.ant-cascader-dropdown'"
201-
[cdkConnectedOverlayOpen]="menuVisible"
201+
[cdkConnectedOverlayOpen]="menuVisible()"
202202
(overlayOutsideClick)="onClickOutside($event)"
203203
(detach)="closeMenu()"
204204
(positionChange)="onPositionChange($event)"
@@ -220,7 +220,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
220220
#menu
221221
class="ant-cascader-menus"
222222
[class.ant-cascader-rtl]="dir === 'rtl'"
223-
[class.ant-cascader-menus-hidden]="!menuVisible"
223+
[class.ant-cascader-menus-hidden]="!menuVisible()"
224224
[class.ant-cascader-menu-empty]="shouldShowEmpty"
225225
[class]="nzMenuClassName"
226226
[style]="nzMenuStyle"
@@ -291,7 +291,7 @@ const defaultDisplayRender = (labels: string[]): string => labels.join(' / ');
291291
'[class.ant-select-borderless]': `nzVariant === 'borderless'`,
292292
'[class.ant-select-filled]': `nzVariant === 'filled'`,
293293
'[class.ant-select-underlined]': `nzVariant === 'underlined'`,
294-
'[class.ant-select-open]': 'menuVisible',
294+
'[class.ant-select-open]': 'menuVisible()',
295295
'[class.ant-select-focused]': 'isFocused',
296296
'[class.ant-select-multiple]': 'nzMultiple',
297297
'[class.ant-select-single]': '!nzMultiple',
@@ -416,7 +416,7 @@ export class NzCascaderComponent
416416
shouldShowEmpty: boolean = false;
417417

418418
el: HTMLElement = this.elementRef.nativeElement;
419-
menuVisible = false;
419+
readonly menuVisible = signal(false);
420420
isLoading = false;
421421
labelRenderText?: string;
422422
labelRenderContext = {};
@@ -528,9 +528,7 @@ export class NzCascaderComponent
528528
map(([{ status, hasFeedback }, noStatus]) => ({ status: noStatus ? '' : status, hasFeedback })),
529529
takeUntilDestroyed(this.destroyRef)
530530
)
531-
.subscribe(({ status, hasFeedback }) => {
532-
this.setStatusStyles(status, hasFeedback);
533-
});
531+
.subscribe(({ status, hasFeedback }) => this.setStatusStyles(status, hasFeedback));
534532

535533
const srv = this.cascaderService;
536534

@@ -570,9 +568,9 @@ export class NzCascaderComponent
570568
this.dropdownWidthStyle = '';
571569
});
572570

573-
this.i18nService.localeChange.pipe(startWith(), takeUntilDestroyed(this.destroyRef)).subscribe(() => {
574-
this.setLocale();
575-
});
571+
this.i18nService.localeChange
572+
.pipe(startWith(), takeUntilDestroyed(this.destroyRef))
573+
.subscribe(() => this.setLocale());
576574

577575
this.size.set(this.nzSize);
578576

@@ -672,7 +670,7 @@ export class NzCascaderComponent
672670
}
673671

674672
setMenuVisible(visible: boolean): void {
675-
if (this.nzDisabled || this.menuVisible === visible) {
673+
if (this.nzDisabled || this.menuVisible() === visible) {
676674
return;
677675
}
678676
if (visible) {
@@ -683,7 +681,7 @@ export class NzCascaderComponent
683681
this.inputValue = '';
684682
}
685683

686-
this.menuVisible = visible;
684+
this.menuVisible.set(visible);
687685
this.nzVisibleChange.emit(visible);
688686
this.cdr.detectChanges();
689687
}
@@ -752,7 +750,7 @@ export class NzCascaderComponent
752750
}
753751

754752
handleInputBlur(): void {
755-
this.menuVisible ? this.focus() : this.blur();
753+
this.menuVisible() ? this.focus() : this.blur();
756754
}
757755

758756
handleInputFocus(): void {
@@ -772,7 +770,7 @@ export class NzCascaderComponent
772770
this.focus();
773771
}
774772
if (this.isActionTrigger('click')) {
775-
this.delaySetMenuVisible(!this.menuVisible, 100);
773+
this.delaySetMenuVisible(!this.menuVisible(), 100);
776774
}
777775
this.onTouched();
778776
}
@@ -790,7 +788,7 @@ export class NzCascaderComponent
790788
onTriggerMouseLeave(event: MouseEvent): void {
791789
if (
792790
this.nzDisabled ||
793-
!this.menuVisible ||
791+
!this.menuVisible() ||
794792
this.isOpening ||
795793
!this.isActionTrigger('hover') ||
796794
this.openControlled
@@ -1130,7 +1128,7 @@ export class NzCascaderComponent
11301128
* and may exceed the boundary of browser's window.
11311129
*/
11321130
private reposition(): void {
1133-
if (this.overlay && this.overlay.overlayRef && this.menuVisible) {
1131+
if (this.overlay && this.overlay.overlayRef && this.menuVisible()) {
11341132
Promise.resolve().then(() => {
11351133
this.overlay.overlayRef.updatePosition();
11361134
this.cdr.markForCheck();
@@ -1257,7 +1255,7 @@ export class NzCascaderComponent
12571255
}
12581256

12591257
// Press any keys above to reopen menu.
1260-
if (!this.menuVisible && keyCode !== BACKSPACE && keyCode !== ESCAPE && !this.openControlled) {
1258+
if (!this.menuVisible() && keyCode !== BACKSPACE && keyCode !== ESCAPE && !this.openControlled) {
12611259
// The `setMenuVisible` runs `detectChanges()`, so we don't need to run `markForCheck()` additionally.
12621260
return this.ngZone.run(() => this.setMenuVisible(true));
12631261
}
@@ -1267,7 +1265,7 @@ export class NzCascaderComponent
12671265
return;
12681266
}
12691267

1270-
if (!this.menuVisible) {
1268+
if (!this.menuVisible()) {
12711269
return;
12721270
}
12731271

components/cascader/demo/basic.ts

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
33

44
import { NzCascaderModule, NzCascaderOption } from 'ng-zorro-antd/cascader';
@@ -45,78 +45,15 @@ const options: NzCascaderOption[] = [
4545
}
4646
];
4747

48-
const otherOptions: NzCascaderOption[] = [
49-
{
50-
value: 'fujian',
51-
label: 'Fujian',
52-
children: [
53-
{
54-
value: 'xiamen',
55-
label: 'Xiamen',
56-
children: [
57-
{
58-
value: 'Kulangsu',
59-
label: 'Kulangsu',
60-
isLeaf: true
61-
}
62-
]
63-
}
64-
]
65-
},
66-
{
67-
value: 'guangxi',
68-
label: 'Guangxi',
69-
children: [
70-
{
71-
value: 'guilin',
72-
label: 'Guilin',
73-
children: [
74-
{
75-
value: 'Lijiang',
76-
label: 'Li Jiang River',
77-
isLeaf: true
78-
}
79-
]
80-
}
81-
]
82-
}
83-
];
84-
8548
@Component({
8649
selector: 'nz-demo-cascader-basic',
8750
imports: [FormsModule, NzCascaderModule],
88-
template: `
89-
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"></nz-cascader>
90-
<a (click)="changeNzOptions()" class="change-options">Change Options</a>
91-
`,
92-
styles: [
93-
`
94-
.change-options {
95-
display: inline-block;
96-
font-size: 12px;
97-
margin-left: 8px;
98-
}
99-
`
100-
]
51+
template: `<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)" />`
10152
})
102-
export class NzDemoCascaderBasicComponent implements OnInit {
103-
nzOptions: NzCascaderOption[] | null = null;
53+
export class NzDemoCascaderBasicComponent {
54+
readonly nzOptions: NzCascaderOption[] = options;
10455
values: string[] | null = null;
10556

106-
ngOnInit(): void {
107-
setTimeout(() => {
108-
this.nzOptions = options;
109-
}, 100);
110-
}
111-
112-
changeNzOptions(): void {
113-
if (this.nzOptions === options) {
114-
this.nzOptions = otherOptions;
115-
} else {
116-
this.nzOptions = options;
117-
}
118-
}
119-
12057
onChanges(values: string): void {
12158
console.log(values, this.values);
12259
}

components/cascader/demo/change-on-function.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ const options: NzCascaderOption[] = [
6060
[nzOptions]="nzOptions"
6161
[(ngModel)]="values"
6262
(ngModelChange)="onChanges($event)"
63-
></nz-cascader>
63+
/>
6464
`
6565
})
6666
export class NzDemoCascaderChangeOnFunctionComponent {
67-
nzOptions: NzCascaderOption[] = options;
67+
readonly nzOptions: NzCascaderOption[] = options;
6868
values: string[] | null = null;
6969

7070
onChanges(values: string[]): void {

components/cascader/demo/change-on-select.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,11 @@ const options: NzCascaderOption[] = [
4949
selector: 'nz-demo-cascader-change-on-select',
5050
imports: [FormsModule, NzCascaderModule],
5151
template: `
52-
<nz-cascader
53-
nzChangeOnSelect
54-
[nzOptions]="nzOptions"
55-
[(ngModel)]="values"
56-
(ngModelChange)="onChanges($event)"
57-
></nz-cascader>
52+
<nz-cascader nzChangeOnSelect [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)" />
5853
`
5954
})
6055
export class NzDemoCascaderChangeOnSelectComponent {
61-
nzOptions: NzCascaderOption[] = options;
56+
readonly nzOptions: NzCascaderOption[] = options;
6257
values: string[] | null = null;
6358

6459
onChanges(values: string[]): void {

components/cascader/demo/custom-field-names.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ const options = [
6363
[nzShowSearch]="true"
6464
[(ngModel)]="values"
6565
(ngModelChange)="onChanges($event)"
66-
></nz-cascader>
66+
/>
6767
`
6868
})
6969
export class NzDemoCascaderCustomFieldNamesComponent {
70-
nzOptions: NzCascaderOption[] = options;
70+
readonly nzOptions: NzCascaderOption[] = options;
7171
values: string[] | null = null;
7272

7373
onChanges(values: string[]): void {

components/cascader/demo/custom-render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const options: NzCascaderOption[] = [
5858
[nzOptions]="nzOptions"
5959
[(ngModel)]="values"
6060
(ngModelChange)="onChanges($event)"
61-
></nz-cascader>
61+
/>
6262
6363
<ng-template #renderTpl let-labels="labels" let-selectedOptions="selectedOptions">
6464
@for (label of labels; track label) {
@@ -78,7 +78,7 @@ const options: NzCascaderOption[] = [
7878
`
7979
})
8080
export class NzDemoCascaderCustomRenderComponent {
81-
nzOptions: NzCascaderOption[] = options;
81+
readonly nzOptions: NzCascaderOption[] = options;
8282
values: string[] | null = null;
8383

8484
onChanges(values: string[]): void {

components/cascader/demo/custom-template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ const options: NzCascaderOption[] = [
3737
[nzOptions]="nzOptions"
3838
[(ngModel)]="values"
3939
(ngModelChange)="onChanges($event)"
40-
></nz-cascader>
40+
/>
4141
<ng-template #renderTpl let-option let-index="index">{{ index + 1 }}. {{ option.label }}</ng-template>
4242
`
4343
})
4444
export class NzDemoCascaderCustomTemplateComponent {
45-
nzOptions = options;
45+
readonly nzOptions = options;
4646
values: string[] | null = null;
4747

4848
onChanges(values: string): void {

components/cascader/demo/default-value-and-async-options.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, signal } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
33

44
import { NzCascaderModule, NzCascaderOption } from 'ng-zorro-antd/cascader';
@@ -48,21 +48,17 @@ const options: NzCascaderOption[] = [
4848
@Component({
4949
selector: 'nz-demo-cascader-default-value-and-async-options',
5050
imports: [FormsModule, NzCascaderModule],
51-
template: `
52-
<nz-cascader [(ngModel)]="values" [nzOptions]="nzOptions" (ngModelChange)="onChanges($event)"></nz-cascader>
53-
`
51+
template: `<nz-cascader [(ngModel)]="values" [nzOptions]="nzOptions()" (ngModelChange)="onChanges($event)" />`
5452
})
5553
export class NzDemoCascaderDefaultValueAndAsyncOptionsComponent implements OnInit {
56-
nzOptions: NzCascaderOption[] | null = null;
54+
readonly nzOptions = signal<NzCascaderOption[] | null>(null);
5755
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
5856

5957
onChanges(values: string[]): void {
6058
console.log(values, this.values);
6159
}
6260

6361
ngOnInit(): void {
64-
setTimeout(() => {
65-
this.nzOptions = options;
66-
}, 500);
62+
setTimeout(() => this.nzOptions.set(options), 500);
6763
}
6864
}

components/cascader/demo/default-value-and-lazy-load.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ const scenicspots: { [key: string]: Array<{ value: string; label: string; isLeaf
5454
@Component({
5555
selector: 'nz-demo-cascader-default-value-and-lazy-load',
5656
imports: [FormsModule, NzCascaderModule],
57-
template: `
58-
<nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)"></nz-cascader>
59-
`
57+
template: `<nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)" />`
6058
})
6159
export class NzDemoCascaderDefaultValueAndLazyLoadComponent {
6260
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];

components/cascader/demo/default-value.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,11 @@ const options: NzCascaderOption[] = [
4848
@Component({
4949
selector: 'nz-demo-cascader-default-value',
5050
imports: [FormsModule, NzCascaderModule],
51-
template: `
52-
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"></nz-cascader>
53-
`
51+
template: `<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)" />`
5452
})
5553
export class NzDemoCascaderDefaultValueComponent {
56-
nzOptions: NzCascaderOption[] = options;
57-
54+
readonly nzOptions: NzCascaderOption[] = options;
5855
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
59-
/* // or like this:
60-
values: any[] = [{
61-
value: 'zhejiang',
62-
label: 'Zhejiang'
63-
}, {
64-
value: 'hangzhou',
65-
label: 'Hangzhou'
66-
}, {
67-
value: 'xihu',
68-
label: 'West Lake'
69-
}]; */
7056

7157
onChanges(values: string[]): void {
7258
console.log(values, this.values);

0 commit comments

Comments
 (0)