Skip to content

Commit 078ece6

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 d9977bd commit 078ece6

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
112112
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
113113
// a little earlier. This avoids issues where IE delays the focusing of the input.
114114
'(focusin)': '_handleFocus()',
115-
'(blur)': '_onTouched()',
115+
'(blur)': '_handleBlur()',
116116
'(input)': '_handleInput($event)',
117117
'(keydown)': '_handleKeydown($event)',
118118
},
@@ -319,7 +319,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
319319
/** Stream of autocomplete option selections. */
320320
readonly optionSelections: Observable<MatOptionSelectionChange> = defer(() => {
321321
if (this.autocomplete && this.autocomplete.options) {
322-
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
322+
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
323323
}
324324

325325
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
@@ -441,6 +441,16 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
441441
}
442442
}
443443

444+
_handleBlur(): void {
445+
// Blur events will fire as soon as the user has their pointer down on an option. We don't
446+
// mark the control as touched in this case, because it can cause the validation to be run
447+
// before a value has been assigned. Instead, we skip marking it as touched from here
448+
// and we do so once the panel has closed.
449+
if (!this.panelOpen) {
450+
this._onTouched();
451+
}
452+
}
453+
444454
/**
445455
* In "auto" mode, the label will animate down as soon as focus is lost.
446456
* This causes the value to jump when selecting an option with the mouse.
@@ -578,6 +588,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
578588
}
579589

580590
this.closePanel();
591+
this._onTouched();
581592
}
582593

583594
/**

src/material/autocomplete/autocomplete.spec.ts

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

763-
it('should mark the autocomplete control as touched on blur', () => {
764-
fixture.componentInstance.trigger.openPanel();
765-
fixture.detectChanges();
763+
it('should mark the autocomplete control as touched on blur while panel is closed', () => {
766764
expect(fixture.componentInstance.stateCtrl.touched)
767765
.toBe(false, `Expected control to start out untouched.`);
768766

@@ -773,6 +771,28 @@ describe('MatAutocomplete', () => {
773771
.toBe(true, `Expected control to become touched on blur.`);
774772
});
775773

774+
it('should defer marking the control as touched if it is blurred while open', () => {
775+
fixture.componentInstance.trigger.openPanel();
776+
fixture.detectChanges();
777+
zone.simulateZoneExit();
778+
779+
expect(fixture.componentInstance.stateCtrl.touched)
780+
.toBe(false, 'Expected control to start out untouched.');
781+
782+
dispatchFakeEvent(input, 'blur');
783+
fixture.detectChanges();
784+
785+
expect(fixture.componentInstance.stateCtrl.touched)
786+
.toBe(false, 'Expected control to stay untouched.');
787+
788+
// Simulate clicking outside the panel.
789+
dispatchFakeEvent(document, 'click');
790+
fixture.detectChanges();
791+
792+
expect(fixture.componentInstance.stateCtrl.touched)
793+
.toBe(true, 'Expected control to become touched once panel closes.');
794+
});
795+
776796
it('should disable the input when used with a value accessor and without `matInput`', () => {
777797
overlayContainer.ngOnDestroy();
778798
fixture.destroy();

0 commit comments

Comments
 (0)