Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autofill): avoid firing unnecessary event on initial render of input #12116

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/cdk/text-field/autofill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,25 @@ describe('AutofillMonitor', () => {
let animationStartCallback: Function = () => {};
inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb);
const autofillStream = autofillMonitor.monitor(inputEl);
const spy = jasmine.createSpy('zone spy');
const spy = jasmine.createSpy('autofill spy');

autofillStream.subscribe(() => spy(NgZone.isInAngularZone()));
expect(spy).not.toHaveBeenCalled();

animationStartCallback({animationName: 'cdk-text-field-autofill-start', target: inputEl});
expect(spy).toHaveBeenCalledWith(true);
});

it('should not emit on init if input is unfilled', () => {
const inputEl = testComponent.input1.nativeElement;
let animationStartCallback: Function = () => {};
inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb);
const autofillStream = autofillMonitor.monitor(inputEl);
const spy = jasmine.createSpy('autofill spy');
autofillStream.subscribe(() => spy());
animationStartCallback({animationName: 'cdk-text-field-autofill-end', target: inputEl});
expect(spy).not.toHaveBeenCalled();
});
});

describe('cdkAutofill', () => {
Expand Down
14 changes: 10 additions & 4 deletions src/cdk/text-field/autofill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ export class AutofillMonitor implements OnDestroy {
}

const result = new Subject<AutofillEvent>();
const cssClass = 'cdk-text-field-autofilled';
const listener = (event: AnimationEvent) => {
if (event.animationName === 'cdk-text-field-autofill-start') {
element.classList.add('cdk-text-field-autofilled');
// Animation events fire on initial element render, we check for the presence of the autofill
// CSS class to make sure this is a real change in state, not just the initial render before
// we fire off events.
if (event.animationName === 'cdk-text-field-autofill-start' &&
!element.classList.contains(cssClass)) {
element.classList.add(cssClass);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're repeating element.classList in a few places here. Consider moving it out into a variable.

this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: true}));
} else if (event.animationName === 'cdk-text-field-autofill-end') {
element.classList.remove('cdk-text-field-autofilled');
} else if (event.animationName === 'cdk-text-field-autofill-end' &&
element.classList.contains(cssClass)) {
element.classList.remove(cssClass);
this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: false}));
}
};
Expand Down