Skip to content

Commit 48dda50

Browse files
mmalerbajelbourn
authored andcommitted
fix(autofill): listen for animation events outside the zone, but emit autofill events inside (#11798)
1 parent dd8c807 commit 48dda50

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/cdk/text-field/autofill.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
10-
import {Component, ElementRef, ViewChild} from '@angular/core';
10+
import {Component, ElementRef, NgZone, ViewChild} from '@angular/core';
1111
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
1212
import {EMPTY} from 'rxjs';
1313
import {AutofillEvent, AutofillMonitor} from './autofill';
@@ -150,6 +150,19 @@ describe('AutofillMonitor', () => {
150150
expect(spy).toHaveBeenCalled();
151151
});
152152

153+
it('should emit on stream inside the NgZone', () => {
154+
const inputEl = testComponent.input1.nativeElement;
155+
let animationStartCallback: Function = () => {};
156+
inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb);
157+
const autofillStream = autofillMonitor.monitor(inputEl);
158+
const spy = jasmine.createSpy('zone spy');
159+
160+
autofillStream.subscribe(() => spy(NgZone.isInAngularZone()));
161+
expect(spy).not.toHaveBeenCalled();
162+
163+
animationStartCallback({animationName: 'cdk-text-field-autofill-start', target: inputEl});
164+
expect(spy).toHaveBeenCalledWith(true);
165+
});
153166
});
154167

155168
describe('cdkAutofill', () => {

src/cdk/text-field/autofill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ export class AutofillMonitor implements OnDestroy {
7171
const listener = (event: AnimationEvent) => {
7272
if (event.animationName === 'cdk-text-field-autofill-start') {
7373
element.classList.add('cdk-text-field-autofilled');
74-
result.next({target: event.target as Element, isAutofilled: true});
74+
this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: true}));
7575
} else if (event.animationName === 'cdk-text-field-autofill-end') {
7676
element.classList.remove('cdk-text-field-autofilled');
77-
result.next({target: event.target as Element, isAutofilled: false});
77+
this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: false}));
7878
}
7979
};
8080

0 commit comments

Comments
 (0)