Skip to content

Commit 059864d

Browse files
crisbetoandrewseguin
authored andcommitted
fix(autocomplete): autofill value changes not being propagated to the form control (#9887)
Currently we skip any `input` events on an autocomplete trigger that have been dispatched while the element is blurre, in order to handle some IE-specific cases. This ends up preventing the autocomplete from picking up any changes to its value that have come as a result of the browser autofill. These changes move some logic around to handle both autofilling and the IE issues. Fixes #9704.
1 parent 3816079 commit 059864d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
402402
// filter out all of the extra events, we save the value on focus and between
403403
// `input` events, and we check whether it changed.
404404
// See: https://connect.microsoft.com/IE/feedback/details/885747/
405-
if (this._previousValue !== value && document.activeElement === event.target) {
405+
if (this._previousValue !== value) {
406406
this._previousValue = value;
407407
this._onChange(value);
408408

409-
if (this._canOpen()) {
409+
if (this._canOpen() && document.activeElement === event.target) {
410410
this.openPanel();
411411
}
412412
}

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,18 @@ describe('MatAutocomplete', () => {
571571
.toEqual('al', 'Expected control value to be updated as user types.');
572572
});
573573

574+
it('should update control value when autofilling', () => {
575+
// Simulate the browser autofilling the input by setting a value and
576+
// dispatching an `input` event while the input is out of focus.
577+
expect(document.activeElement).not.toBe(input, 'Expected input not to have focus.');
578+
input.value = 'Alabama';
579+
dispatchFakeEvent(input, 'input');
580+
fixture.detectChanges();
581+
582+
expect(fixture.componentInstance.stateCtrl.value)
583+
.toBe('Alabama', 'Expected value to be propagated to the form control.');
584+
});
585+
574586
it('should update control value when option is selected with option value', fakeAsync(() => {
575587
fixture.componentInstance.trigger.openPanel();
576588
fixture.detectChanges();

0 commit comments

Comments
 (0)