Skip to content

Commit af7fb5d

Browse files
authored
fix(module:typography): remove NgZone dependency (#8440)
This commit eliminates the `NgZone` dependency from the text edit component. Since zone.js is becoming optional, `onStable` won't emit any value when `provideZonelessChangeDetection` is used in the application config. Instead, we now use the alternative approach provided by `afterNextRender`. Most of the code that was using `onStable` should be replaced with `afterNextRender`.
1 parent 1ec0e76 commit af7fb5d

2 files changed

Lines changed: 35 additions & 28 deletions

File tree

‎components/typography/text-edit.component.ts‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ import {
1111
Component,
1212
ElementRef,
1313
EventEmitter,
14+
Injector,
1415
Input,
1516
NgZone,
1617
OnInit,
1718
Output,
1819
ViewChild,
19-
ViewEncapsulation
20+
ViewEncapsulation,
21+
afterNextRender,
22+
inject
2023
} from '@angular/core';
21-
import { BehaviorSubject, EMPTY, from, fromEvent, Observable } from 'rxjs';
22-
import { switchMap, take, takeUntil, withLatestFrom } from 'rxjs/operators';
24+
import { BehaviorSubject, EMPTY, fromEvent, Observable } from 'rxjs';
25+
import { first, switchMap, takeUntil } from 'rxjs/operators';
2326

2427
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
2528
import { NzDestroyService } from 'ng-zorro-antd/core/services';
@@ -86,6 +89,8 @@ export class NzTextEditComponent implements OnInit {
8689
// automatically removed and re-added through the `switchMap` below).
8790
private textarea$ = new BehaviorSubject<ElementRef<HTMLTextAreaElement> | null | undefined>(null);
8891

92+
private injector = inject(Injector);
93+
8994
constructor(
9095
private ngZone: NgZone,
9196
private host: ElementRef<HTMLElement>,
@@ -178,20 +183,26 @@ export class NzTextEditComponent implements OnInit {
178183
}
179184

180185
focusAndSetValue(): void {
181-
// Note: the zone may be nooped through `BootstrapOptions` when bootstrapping the root module. This means
182-
// the `onStable` will never emit any value.
183-
const onStable$ = this.ngZone.isStable ? from(Promise.resolve()) : this.ngZone.onStable.pipe(take(1));
184-
// Normally this isn't in the zone, but it can cause performance regressions for apps
185-
// using `zone-patch-rxjs` because it'll trigger a change detection when it unsubscribes.
186-
this.ngZone.runOutsideAngular(() => {
187-
onStable$.pipe(withLatestFrom(this.textarea$), takeUntil(this.destroy$)).subscribe(([, textarea]) => {
188-
if (textarea) {
189-
textarea.nativeElement.focus();
190-
textarea.nativeElement.value = this.currentText || '';
191-
this.autosizeDirective.resizeToFitContent();
192-
this.cdr.markForCheck();
193-
}
194-
});
195-
});
186+
const { injector } = this;
187+
188+
afterNextRender(
189+
() => {
190+
this.textarea$
191+
.pipe(
192+
// It may still not be available, so we need to wait until view queries
193+
// are executed during the change detection. It's safer to wait until
194+
// the query runs and the textarea is set on the behavior subject.
195+
first((textarea): textarea is ElementRef<HTMLTextAreaElement> => textarea != null),
196+
takeUntil(this.destroy$)
197+
)
198+
.subscribe(textarea => {
199+
textarea.nativeElement.focus();
200+
textarea.nativeElement.value = this.currentText || '';
201+
this.autosizeDirective.resizeToFitContent();
202+
this.cdr.markForCheck();
203+
});
204+
},
205+
{ injector }
206+
);
196207
}
197208
}

‎components/typography/typography.spec.ts‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CAPS_LOCK, ENTER, ESCAPE, TAB } from '@angular/cdk/keycodes';
22
import { OverlayContainer } from '@angular/cdk/overlay';
33
import { CommonModule } from '@angular/common';
44
import { ApplicationRef, Component, NgZone, ViewChild } from '@angular/core';
5-
import { ComponentFixture, fakeAsync, flush, flushMicrotasks, inject, TestBed, tick } from '@angular/core/testing';
5+
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
66
import { By } from '@angular/platform-browser';
77
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
88

@@ -279,22 +279,18 @@ describe('typography', () => {
279279
expect(testComponent.str).toBe('test');
280280
}));
281281

282-
it('should edit focus', fakeAsync(() => {
282+
it('should edit focus', () => {
283283
const editButton = componentElement.querySelector<HTMLButtonElement>('.ant-typography-edit');
284284
editButton!.click();
285285
fixture.detectChanges();
286-
// The zone may be already stable (see `isStable` condition), thus there're no tasks
287-
// in the queue that have been scheduled previously.
288-
// This will schedule a microtask (except of waiting for `onStable`).
289-
flushMicrotasks();
286+
287+
// `tick()` will handle over after next render hooks.
288+
TestBed.inject(ApplicationRef).tick();
290289

291290
const textarea = componentElement.querySelector<HTMLTextAreaElement>('textarea')! as HTMLTextAreaElement;
292291

293292
expect(document.activeElement === textarea).toBe(true);
294-
dispatchFakeEvent(textarea, 'blur');
295-
flush();
296-
fixture.detectChanges();
297-
}));
293+
});
298294

299295
it('should apply changes when Enter keydown', fakeAsync(() => {
300296
const editButton = componentElement.querySelector<HTMLButtonElement>('.ant-typography-edit');

0 commit comments

Comments
 (0)