Skip to content

Commit c946984

Browse files
committed
fix(autocomplete): autofill value changes not being propagated to the form control
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 dd81633 commit c946984

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,15 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
330330
// filter out all of the extra events, we save the value on focus and between
331331
// `input` events, and we check whether it changed.
332332
// See: https://connect.microsoft.com/IE/feedback/details/885747/
333-
if (this._canOpen() && this._previousValue !== value &&
334-
document.activeElement === event.target) {
333+
if (this._previousValue !== value) {
334+
// We still want to propagate any value changes, otherwise we can miss
335+
// out on cases like the browser autofilling the input value.
335336
this._previousValue = value;
336337
this._onChange(value);
337-
this.openPanel();
338+
339+
if (this._canOpen() && document.activeElement === event.target) {
340+
this.openPanel();
341+
}
338342
}
339343
}
340344

src/lib/autocomplete/autocomplete.spec.ts

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

458+
it('should update control value when autofilling', () => {
459+
// Simulate the browser autofilling the input by setting a value and
460+
// dispatching an `input` event while the input is out of focus.
461+
expect(document.activeElement).not.toBe(input, 'Expected input not to have focus.');
462+
input.value = 'Alabama';
463+
dispatchFakeEvent(input, 'input');
464+
fixture.detectChanges();
465+
466+
expect(fixture.componentInstance.stateCtrl.value)
467+
.toBe('Alabama', 'Expected value to be propagated to the form control.');
468+
});
469+
458470
it('should update control value when option is selected with option value', fakeAsync(() => {
459471
fixture.componentInstance.trigger.openPanel();
460472
fixture.detectChanges();

0 commit comments

Comments
 (0)