Skip to content

Commit

Permalink
refactor(module:*): use ECMAScript standard class field (#8718)
Browse files Browse the repository at this point in the history
* refactor: replace explicit constructor injections with `inject`

* refactor(module:*): use ECMAScript standard class field

* test(module:drawer): update transition style expectations in Drawer test
  • Loading branch information
Laffery authored Jan 16, 2025
1 parent 5898da7 commit f1d8d92
Show file tree
Hide file tree
Showing 59 changed files with 312 additions and 351 deletions.
4 changes: 2 additions & 2 deletions components/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ViewChild,
ViewEncapsulation,
afterRender,
inject,
numberAttribute
} from '@angular/core';

Expand Down Expand Up @@ -75,11 +76,10 @@ export class NzAvatarComponent implements OnChanges {

@ViewChild('textEl', { static: false }) textEl?: ElementRef<HTMLSpanElement>;

private el: HTMLElement = this.elementRef.nativeElement;
private el: HTMLElement = inject(ElementRef).nativeElement;

constructor(
public nzConfigService: NzConfigService,
private elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {
afterRender(() => this.calcStringSize());
Expand Down
36 changes: 21 additions & 15 deletions components/cascader/cascader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
ZERO
} from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
import { Component, DebugElement, inject, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject as testingInject, TestBed, tick } from '@angular/core/testing';
import { FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { provideNoopAnimations } from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -73,15 +73,19 @@ describe('cascader', () => {
});
});

beforeEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
overlayContainer = currentOverlayContainer;
overlayContainerElement = currentOverlayContainer.getContainerElement();
}));
beforeEach(
testingInject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
overlayContainer = currentOverlayContainer;
overlayContainerElement = currentOverlayContainer.getContainerElement();
})
);

afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
}));
afterEach(
testingInject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
})
);

describe('default', () => {
let fixture: ComponentFixture<NzDemoCascaderDefaultComponent>;
Expand Down Expand Up @@ -1864,10 +1868,12 @@ describe('cascader', () => {
let cascader: DebugElement;
let testComponent: NzDemoCascaderLoadDataComponent;

afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
}));
afterEach(
testingInject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(NzDemoCascaderLoadDataComponent);
Expand Down Expand Up @@ -2495,9 +2501,9 @@ export class NzDemoCascaderStatusComponent {
`
})
export class NzDemoCascaderInFormComponent {
private fb = inject(FormBuilder);
validateForm = this.fb.group({
demo: this.fb.control<string[] | null>(null, Validators.required)
});
nzOptions: NzSafeAny[] | null = options1;
constructor(private fb: FormBuilder) {}
}
8 changes: 4 additions & 4 deletions components/cascader/demo/reactive-form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnDestroy } from '@angular/core';
import { Component, inject, OnDestroy } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { Subscription } from 'rxjs';

Expand Down Expand Up @@ -67,15 +67,15 @@ const options: NzCascaderOption[] = [
]
})
export class NzDemoCascaderReactiveFormComponent implements OnDestroy {
private fb = inject(FormBuilder);
form = this.fb.group({
name: this.fb.control<string[] | null>(null, Validators.required)
});
nzOptions: NzCascaderOption[] = options;
changeSubscription: Subscription;

constructor(private fb: FormBuilder) {
const control = this.form.controls.name;
this.changeSubscription = control.valueChanges.subscribe(data => {
constructor() {
this.changeSubscription = this.form.controls.name.valueChanges.subscribe(data => {
this.onChanges(data);
});
}
Expand Down
12 changes: 6 additions & 6 deletions components/cdk/overflow/overflow-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
AfterContentInit,
OnDestroy,
ContentChild,
ChangeDetectorRef
ChangeDetectorRef,
inject
} from '@angular/core';
import { BehaviorSubject, combineLatest, Observable, ReplaySubject, Subject } from 'rxjs';
import { filter, map, pairwise, startWith, switchMap, takeUntil, withLatestFrom } from 'rxjs/operators';
Expand Down Expand Up @@ -41,6 +42,9 @@ export class NzOverflowContainerComponent implements OnInit, AfterContentInit, O
@ContentChild(NzOverflowRestDirective) overflowRest: NzOverflowRestDirective | undefined = undefined;
overflowItems$ = new ReplaySubject<QueryList<NzOverflowItemDirective>>(1);
destroy$ = new Subject<void>();

private nzResizeObserver = inject(NzResizeObserver);
private elementRef = inject(ElementRef);
containerWidth$ = this.nzResizeObserver
.observe(this.elementRef.nativeElement)
.pipe(map(([item]) => item.target.clientWidth || 0));
Expand Down Expand Up @@ -68,11 +72,7 @@ export class NzOverflowContainerComponent implements OnInit, AfterContentInit, O
}
}

constructor(
private nzResizeObserver: NzResizeObserver,
private elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {}
constructor(private cdr: ChangeDetectorRef) {}

ngOnInit(): void {
const overflowItemsWidth$ = this.overflowItems$.pipe(
Expand Down
11 changes: 5 additions & 6 deletions components/cdk/overflow/overflow-item.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ChangeDetectorRef, Directive, ElementRef } from '@angular/core';
import { ChangeDetectorRef, Directive, ElementRef, inject } from '@angular/core';
import { distinctUntilChanged, map, startWith, tap } from 'rxjs/operators';

import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
Expand All @@ -15,6 +15,9 @@ import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
}
})
export class NzOverflowItemDirective {
private nzResizeObserver = inject(NzResizeObserver);
private elementRef = inject(ElementRef);

overflowStyle: { [key: string]: string | number | undefined } | undefined = undefined;
itemWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(
map(([item]) => (item.target as HTMLElement).offsetWidth),
Expand All @@ -25,11 +28,7 @@ export class NzOverflowItemDirective {
})
);
itemWidth: number | undefined = undefined;
constructor(
private nzResizeObserver: NzResizeObserver,
public elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {}
constructor(private cdr: ChangeDetectorRef) {}

setItemStyle(display: boolean, order: number): void {
const mergedHidden = !display;
Expand Down
11 changes: 5 additions & 6 deletions components/cdk/overflow/overflow-rest.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ChangeDetectorRef, Directive, ElementRef } from '@angular/core';
import { ChangeDetectorRef, Directive, ElementRef, inject } from '@angular/core';
import { map, startWith, tap } from 'rxjs/operators';

import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
Expand All @@ -15,18 +15,17 @@ import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
}
})
export class NzOverflowRestDirective {
private nzResizeObserver = inject(NzResizeObserver);
private elementRef = inject(ElementRef);

restStyle: { [key: string]: string | number | undefined } | undefined = undefined;
restWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(
map(([item]) => (item.target as HTMLElement).offsetWidth),
startWith(0),
tap(width => (this.restWidth = width))
);
restWidth = 0;
constructor(
private nzResizeObserver: NzResizeObserver,
private elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {}
constructor(private cdr: ChangeDetectorRef) {}

setRestStyle(display: boolean, order: number): void {
const mergedHidden = !display;
Expand Down
11 changes: 5 additions & 6 deletions components/cdk/overflow/overflow-suffix.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ChangeDetectorRef, Directive, ElementRef } from '@angular/core';
import { ChangeDetectorRef, Directive, ElementRef, inject } from '@angular/core';
import { map, tap } from 'rxjs/operators';

import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
Expand All @@ -15,17 +15,16 @@ import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
}
})
export class NzOverflowSuffixDirective {
private nzResizeObserver = inject(NzResizeObserver);
private elementRef = inject(ElementRef);

suffixStyle = {};
suffixWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(
map(([item]) => (item.target as HTMLElement).offsetWidth),
tap(width => (this.suffixWidth = width))
);
suffixWidth = 0;
constructor(
private nzResizeObserver: NzResizeObserver,
private elementRef: ElementRef,
private cdr: ChangeDetectorRef
) {}
constructor(private cdr: ChangeDetectorRef) {}

setSuffixStyle(start: number | null, order: number): void {
if (start !== null) {
Expand Down
7 changes: 3 additions & 4 deletions components/color-picker/color-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Component,
EventEmitter,
forwardRef,
inject,
Input,
OnChanges,
OnDestroy,
Expand Down Expand Up @@ -131,16 +132,14 @@ export class NzColorPickerComponent implements OnInit, OnChanges, ControlValueAc
@Output() readonly nzOnClear = new EventEmitter<boolean>();
@Output() readonly nzOnOpenChange = new EventEmitter<boolean>();

private formBuilder = inject(FormBuilder);
private destroy$ = new Subject<void>();
private isNzDisableFirstChange: boolean = true;
blockColor: string = '';
clearColor: boolean = false;
showText: string = defaultColor.toHexString();

constructor(
private formBuilder: FormBuilder,
private cdr: ChangeDetectorRef
) {}
constructor(private cdr: ChangeDetectorRef) {}

formControl = this.formBuilder.control('');

Expand Down
9 changes: 4 additions & 5 deletions components/color-picker/demo/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';

import { NzButtonModule } from 'ng-zorro-antd/button';
Expand All @@ -14,7 +14,7 @@ import { NzInputModule } from 'ng-zorro-antd/input';
<nz-form-item>
<nz-form-label [nzSpan]="4">name</nz-form-label>
<nz-form-control [nzSpan]="16">
<input nz-input formControlName="userName" />
<input nz-input formControlName="username" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
Expand All @@ -32,13 +32,12 @@ import { NzInputModule } from 'ng-zorro-antd/input';
`
})
export class NzDemoColorPickerUseComponent {
private formBuilder = inject(FormBuilder);
validateForm = this.formBuilder.group({
userName: ['color-picker', [Validators.required]],
username: ['color-picker', [Validators.required]],
colorPicker: ['#1677ff']
});

constructor(private formBuilder: FormBuilder) {}

submitForm(): void {
console.log(this.validateForm.value);
}
Expand Down
11 changes: 5 additions & 6 deletions components/cron-expression/demo/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';

import { NzButtonModule } from 'ng-zorro-antd/button';
Expand All @@ -14,7 +14,7 @@ import { NzInputModule } from 'ng-zorro-antd/input';
<nz-form-item>
<nz-form-label [nzSpan]="6">name</nz-form-label>
<nz-form-control [nzSpan]="14">
<input nz-input formControlName="userName" />
<input nz-input formControlName="username" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
Expand All @@ -38,18 +38,17 @@ import { NzInputModule } from 'ng-zorro-antd/input';
`
})
export class NzDemoCronExpressionUseComponent {
private fb = inject(FormBuilder);
validateForm: FormGroup<{
userName: FormControl<string | null>;
username: FormControl<string | null>;
cronLinux: FormControl<string | null>;
cronSpring: FormControl<string | null>;
}> = this.fb.group({
userName: ['cron-expression', [Validators.required]],
username: ['cron-expression', [Validators.required]],
cronLinux: ['* 1 * * *', [Validators.required]],
cronSpring: ['0 * 1 * * *', [Validators.required]]
});

constructor(private fb: FormBuilder) {}

submitForm(): void {
console.log(this.validateForm.value);
}
Expand Down
19 changes: 10 additions & 9 deletions components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ESCAPE } from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { ApplicationRef, Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
import { ApplicationRef, Component, DebugElement, inject, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject as testingInject, TestBed, tick } from '@angular/core/testing';
import { FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { provideNoopAnimations } from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -47,11 +47,13 @@ describe('NzDatePickerComponent', () => {
debugElement = fixture.debugElement;
});

beforeEach(inject([OverlayContainer, NzI18nService], (oc: OverlayContainer, i18n: NzI18nService) => {
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
i18nService = i18n;
}));
beforeEach(
testingInject([OverlayContainer, NzI18nService], (oc: OverlayContainer, i18n: NzI18nService) => {
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
i18nService = i18n;
})
);

afterEach(() => {
overlayContainer.ngOnDestroy();
Expand Down Expand Up @@ -1598,9 +1600,8 @@ class NzTestDatePickerStatusComponent {
`
})
class NzTestDatePickerInFormComponent {
private fb = inject(FormBuilder);
validateForm = this.fb.group({
demo: this.fb.control<Date | null>(null, Validators.required)
});

constructor(private fb: FormBuilder) {}
}
3 changes: 0 additions & 3 deletions components/date-picker/lib/abstract-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { NzCalendarI18nInterface } from 'ng-zorro-antd/i18n';
import { DateBodyRow, DateCell } from './interface';

@Directive()
// eslint-disable-next-line @angular-eslint/directive-class-suffix
export abstract class AbstractTable implements OnInit, OnChanges {
headRow: DateCell[] = [];
bodyRows: DateBodyRow[] = [];
Expand Down Expand Up @@ -52,8 +51,6 @@ export abstract class AbstractTable implements OnInit, OnChanges {
}
}



hasRangeValue(): boolean {
return this.selectedValue?.length > 0 || this.hoverValue?.length > 0;
}
Expand Down
Loading

0 comments on commit f1d8d92

Please sign in to comment.