Skip to content

Commit 06e2696

Browse files
authored
refactor(module:form): use inject pattern (#9200)
1 parent 2e96828 commit 06e2696

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

components/form/form-control.component.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66
import {
77
AfterContentInit,
88
ChangeDetectionStrategy,
9-
ChangeDetectorRef,
109
Component,
1110
ContentChild,
1211
Input,
1312
OnChanges,
14-
OnDestroy,
1513
OnInit,
1614
SimpleChanges,
1715
TemplateRef,
1816
ViewEncapsulation,
1917
booleanAttribute,
20-
inject
18+
inject,
19+
DestroyRef,
20+
ChangeDetectorRef
2121
} from '@angular/core';
22+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2223
import { AbstractControl, FormControlDirective, FormControlName, NgControl, NgModel } from '@angular/forms';
23-
import { Observable, Subject, Subscription } from 'rxjs';
24-
import { filter, startWith, takeUntil, tap } from 'rxjs/operators';
24+
import { Observable, Subscription } from 'rxjs';
25+
import { filter, startWith, tap } from 'rxjs/operators';
2526

2627
import { helpMotion } from 'ng-zorro-antd/core/animation';
2728
import { NzFormStatusService } from 'ng-zorro-antd/core/form';
@@ -67,11 +68,15 @@ import { NzFormDirective } from './form.directive';
6768
},
6869
imports: [NzOutletModule]
6970
})
70-
export class NzFormControlComponent implements OnChanges, OnDestroy, OnInit, AfterContentInit, OnDestroy {
71+
export class NzFormControlComponent implements OnChanges, OnInit, AfterContentInit {
72+
private cdr = inject(ChangeDetectorRef);
73+
public i18n = inject(NzI18nService);
74+
private nzFormStatusService = inject(NzFormStatusService);
75+
private destroyRef = inject(DestroyRef);
76+
7177
private _hasFeedback = false;
7278
private validateChanges: Subscription = Subscription.EMPTY;
7379
private validateString: string | null = null;
74-
private destroyed$ = new Subject<void>();
7580
private localeId!: string;
7681
private autoErrorTip?: string;
7782

@@ -129,7 +134,7 @@ export class NzFormControlComponent implements OnChanges, OnDestroy, OnInit, Aft
129134
/** miss detect https://github.com/angular/angular/issues/10887 **/
130135
if (this.validateControl && this.validateControl.statusChanges) {
131136
this.validateChanges = (this.validateControl.statusChanges as Observable<NzSafeAny>)
132-
.pipe(startWith(null), takeUntil(this.destroyed$))
137+
.pipe(startWith(null), takeUntilDestroyed(this.destroyRef))
133138
.subscribe(() => {
134139
if (!this.disableAutoTips) {
135140
this.updateAutoErrorTip();
@@ -222,7 +227,7 @@ export class NzFormControlComponent implements OnChanges, OnDestroy, OnInit, Aft
222227
}
223228

224229
private subscribeAutoTips(observable?: Observable<NzSafeAny>): void {
225-
observable?.pipe(takeUntil(this.destroyed$)).subscribe(() => {
230+
observable?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
226231
if (!this.disableAutoTips) {
227232
this.updateAutoErrorTip();
228233
this.setStatus();
@@ -234,12 +239,8 @@ export class NzFormControlComponent implements OnChanges, OnDestroy, OnInit, Aft
234239
private nzFormItemComponent = inject(NzFormItemComponent, { host: true, optional: true });
235240
private nzFormDirective = inject(NzFormDirective, { optional: true });
236241

237-
constructor(
238-
private cdr: ChangeDetectorRef,
239-
i18n: NzI18nService,
240-
private nzFormStatusService: NzFormStatusService
241-
) {
242-
this.subscribeAutoTips(i18n.localeChange.pipe(tap(locale => (this.localeId = locale.locale))));
242+
constructor() {
243+
this.subscribeAutoTips(this.i18n.localeChange.pipe(tap(locale => (this.localeId = locale.locale))));
243244
this.subscribeAutoTips(this.nzFormDirective?.getInputObservable('nzAutoTips'));
244245
this.subscribeAutoTips(
245246
this.nzFormDirective
@@ -263,11 +264,6 @@ export class NzFormControlComponent implements OnChanges, OnDestroy, OnInit, Aft
263264
this.setStatus();
264265
}
265266

266-
ngOnDestroy(): void {
267-
this.destroyed$.next();
268-
this.destroyed$.complete();
269-
}
270-
271267
ngAfterContentInit(): void {
272268
if (!this.validateControl && !this.validateString) {
273269
if (this.defaultValidateControl instanceof FormControlDirective) {

components/form/form-item.component.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, ViewEncapsulation } from '@angular/core';
7-
import { Subject } from 'rxjs';
6+
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject, ViewEncapsulation } from '@angular/core';
87

98
export type NzFormControlStatusType = 'success' | 'error' | 'warning' | 'validating' | '';
109

@@ -25,13 +24,13 @@ export type NzFormControlStatusType = 'success' | 'error' | 'warning' | 'validat
2524
},
2625
template: `<ng-content></ng-content>`
2726
})
28-
export class NzFormItemComponent implements OnDestroy, OnDestroy {
27+
export class NzFormItemComponent {
28+
private cdr = inject(ChangeDetectorRef);
29+
2930
status: NzFormControlStatusType = '';
3031
hasFeedback = false;
3132
withHelpClass = false;
3233

33-
private destroy$ = new Subject<boolean>();
34-
3534
setWithHelpViaTips(value: boolean): void {
3635
this.withHelpClass = value;
3736
this.cdr.markForCheck();
@@ -46,11 +45,4 @@ export class NzFormItemComponent implements OnDestroy, OnDestroy {
4645
this.hasFeedback = hasFeedback;
4746
this.cdr.markForCheck();
4847
}
49-
50-
constructor(private cdr: ChangeDetectorRef) {}
51-
52-
ngOnDestroy(): void {
53-
this.destroy$.next(true);
54-
this.destroy$.complete();
55-
}
5648
}

components/form/form-label.component.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55

66
import {
77
ChangeDetectionStrategy,
8-
ChangeDetectorRef,
98
Component,
109
Input,
11-
OnDestroy,
1210
ViewEncapsulation,
1311
booleanAttribute,
14-
inject
12+
inject,
13+
ChangeDetectorRef
1514
} from '@angular/core';
16-
import { Subject } from 'rxjs';
17-
import { filter, takeUntil } from 'rxjs/operators';
15+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
16+
import { filter } from 'rxjs/operators';
1817

1918
import { ThemeType } from '@ant-design/icons-angular';
2019

@@ -59,7 +58,9 @@ function toTooltipIcon(value: string | NzFormTooltipIcon): Required<NzFormToolti
5958
},
6059
imports: [NzOutletModule, NzTooltipDirective, NzIconModule]
6160
})
62-
export class NzFormLabelComponent implements OnDestroy {
61+
export class NzFormLabelComponent {
62+
private cdr = inject(ChangeDetectorRef);
63+
6364
@Input() nzFor?: string;
6465
@Input({ transform: booleanAttribute }) nzRequired = false;
6566
@Input({ transform: booleanAttribute })
@@ -107,48 +108,41 @@ export class NzFormLabelComponent implements OnDestroy {
107108

108109
private labelWrap: boolean | 'default' = 'default';
109110

110-
private destroy$ = new Subject<boolean>();
111-
112111
private nzFormDirective = inject(NzFormDirective, { skipSelf: true, optional: true });
113112

114-
constructor(private cdr: ChangeDetectorRef) {
113+
constructor() {
115114
if (this.nzFormDirective) {
116115
this.nzFormDirective
117116
.getInputObservable('nzNoColon')
118117
.pipe(
119118
filter(() => this.noColon === 'default'),
120-
takeUntil(this.destroy$)
119+
takeUntilDestroyed()
121120
)
122121
.subscribe(() => this.cdr.markForCheck());
123122

124123
this.nzFormDirective
125124
.getInputObservable('nzTooltipIcon')
126125
.pipe(
127126
filter(() => this._tooltipIcon === 'default'),
128-
takeUntil(this.destroy$)
127+
takeUntilDestroyed()
129128
)
130129
.subscribe(() => this.cdr.markForCheck());
131130

132131
this.nzFormDirective
133132
.getInputObservable('nzLabelAlign')
134133
.pipe(
135134
filter(() => this.labelAlign === 'default'),
136-
takeUntil(this.destroy$)
135+
takeUntilDestroyed()
137136
)
138137
.subscribe(() => this.cdr.markForCheck());
139138

140139
this.nzFormDirective
141140
.getInputObservable('nzLabelWrap')
142141
.pipe(
143142
filter(() => this.labelWrap === 'default'),
144-
takeUntil(this.destroy$)
143+
takeUntilDestroyed()
145144
)
146145
.subscribe(() => this.cdr.markForCheck());
147146
}
148147
}
149-
150-
ngOnDestroy(): void {
151-
this.destroy$.next(true);
152-
this.destroy$.complete();
153-
}
154148
}

components/form/form.directive.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
*/
55

66
import { Direction, Directionality } from '@angular/cdk/bidi';
7-
import { Directive, Input, OnChanges, OnDestroy, SimpleChange, SimpleChanges, booleanAttribute } from '@angular/core';
7+
import {
8+
Directive,
9+
Input,
10+
OnChanges,
11+
SimpleChange,
12+
SimpleChanges,
13+
booleanAttribute,
14+
DestroyRef,
15+
inject
16+
} from '@angular/core';
17+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
818
import { Observable, Subject } from 'rxjs';
9-
import { filter, map, takeUntil } from 'rxjs/operators';
19+
import { filter, map } from 'rxjs/operators';
1020

1121
import { ThemeType } from '@ant-design/icons-angular';
1222

13-
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
23+
import { NzConfigKey, WithConfig } from 'ng-zorro-antd/core/config';
1424
import { InputObservable } from 'ng-zorro-antd/core/types';
1525

1626
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'form';
@@ -35,7 +45,10 @@ export const DefaultTooltipIcon = {
3545
'[class.ant-form-rtl]': `dir === 'rtl'`
3646
}
3747
})
38-
export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
48+
export class NzFormDirective implements OnChanges, InputObservable {
49+
private destroyRef = inject(DestroyRef);
50+
private directionality = inject(Directionality);
51+
3952
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
4053

4154
@Input() nzLayout: NzFormLayoutType = 'horizontal';
@@ -47,7 +60,6 @@ export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
4760
@Input({ transform: booleanAttribute }) @WithConfig() nzLabelWrap: boolean = false;
4861

4962
dir: Direction = 'ltr';
50-
destroy$ = new Subject<boolean>();
5163
private inputChanges$ = new Subject<SimpleChanges>();
5264

5365
getInputObservable<K extends keyof this>(changeType: K): Observable<SimpleChange> {
@@ -57,23 +69,17 @@ export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
5769
);
5870
}
5971

60-
constructor(
61-
public nzConfigService: NzConfigService,
62-
private directionality: Directionality
63-
) {
72+
constructor() {
6473
this.dir = this.directionality.value;
65-
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
74+
this.directionality.change?.pipe(takeUntilDestroyed()).subscribe(direction => {
6675
this.dir = direction;
6776
});
77+
this.destroyRef.onDestroy(() => {
78+
this.inputChanges$.complete();
79+
});
6880
}
6981

7082
ngOnChanges(changes: SimpleChanges): void {
7183
this.inputChanges$.next(changes);
7284
}
73-
74-
ngOnDestroy(): void {
75-
this.inputChanges$.complete();
76-
this.destroy$.next(true);
77-
this.destroy$.complete();
78-
}
7985
}

0 commit comments

Comments
 (0)