Skip to content

Commit 0c7b057

Browse files
committed
fix(autocomplete): form control being marked as touched too early when clicking on an option
Currently we mark the autocomplete form control as touched on every blur event. This can be an issue for the case where a control is being validated on blur, because the input becomes blurred as soon as the user has their pointer down on something else (e.g. one of the options). This will cause validation to run before the value has been assigned. With these changes we switch to marking the control as touched once the panel has been closed. Fixes #11903.
1 parent c276e26 commit 0c7b057

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
107107
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
108108
// a little earlier. This avoids issues where IE delays the focusing of the input.
109109
'(focusin)': '_handleFocus()',
110-
'(blur)': '_onTouched()',
110+
'(blur)': '_handleBlur()',
111111
'(input)': '_handleInput($event)',
112112
'(keydown)': '_handleKeydown($event)',
113113
},
@@ -246,7 +246,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
246246
/** Stream of autocomplete option selections. */
247247
readonly optionSelections: Observable<MatOptionSelectionChange> = defer(() => {
248248
if (this.autocomplete && this.autocomplete.options) {
249-
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
249+
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
250250
}
251251

252252
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
@@ -376,6 +376,16 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
376376
}
377377
}
378378

379+
_handleBlur(): void {
380+
// Blur events will fire as soon as the user has their pointer down on an option. We don't
381+
// mark the control as touched in this case, because it can cause the validation to be run
382+
// before a value has been assigned. Instead, we skip marking it as touched from here
383+
// and we do so once the panel has closed.
384+
if (!this.panelOpen) {
385+
this._onTouched();
386+
}
387+
}
388+
379389
/**
380390
* In "auto" mode, the label will animate down as soon as focus is lost.
381391
* This causes the value to jump when selecting an option with the mouse.
@@ -498,6 +508,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
498508
}
499509

500510
this.closePanel();
511+
this._onTouched();
501512
}
502513

503514
/**

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,7 @@ describe('MatAutocomplete', () => {
699699
.toBe(false, `Expected control to stay pristine if value is set programmatically.`);
700700
});
701701

702-
it('should mark the autocomplete control as touched on blur', () => {
703-
fixture.componentInstance.trigger.openPanel();
704-
fixture.detectChanges();
702+
it('should mark the autocomplete control as touched on blur while panel is closed', () => {
705703
expect(fixture.componentInstance.stateCtrl.touched)
706704
.toBe(false, `Expected control to start out untouched.`);
707705

@@ -712,6 +710,28 @@ describe('MatAutocomplete', () => {
712710
.toBe(true, `Expected control to become touched on blur.`);
713711
});
714712

713+
it('should defer marking the control as touched if it is blurred while open', () => {
714+
fixture.componentInstance.trigger.openPanel();
715+
fixture.detectChanges();
716+
zone.simulateZoneExit();
717+
718+
expect(fixture.componentInstance.stateCtrl.touched)
719+
.toBe(false, 'Expected control to start out untouched.');
720+
721+
dispatchFakeEvent(input, 'blur');
722+
fixture.detectChanges();
723+
724+
expect(fixture.componentInstance.stateCtrl.touched)
725+
.toBe(false, 'Expected control to stay untouched.');
726+
727+
// Simulate clicking outside the panel.
728+
dispatchFakeEvent(document, 'click');
729+
fixture.detectChanges();
730+
731+
expect(fixture.componentInstance.stateCtrl.touched)
732+
.toBe(true, 'Expected control to become touched once panel closes.');
733+
});
734+
715735
it('should disable the input when used with a value accessor and without `matInput`', () => {
716736
overlayContainer.ngOnDestroy();
717737
fixture.destroy();

0 commit comments

Comments
 (0)