Skip to content

Commit 707905c

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 98711d7 commit 707905c

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
@@ -108,7 +108,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
108108
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
109109
// a little earlier. This avoids issues where IE delays the focusing of the input.
110110
'(focusin)': '_handleFocus()',
111-
'(blur)': '_onTouched()',
111+
'(blur)': '_handleBlur()',
112112
'(input)': '_handleInput($event)',
113113
'(keydown)': '_handleKeydown($event)',
114114
},
@@ -296,7 +296,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
296296
/** Stream of autocomplete option selections. */
297297
readonly optionSelections: Observable<MatOptionSelectionChange> = defer(() => {
298298
if (this.autocomplete && this.autocomplete.options) {
299-
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
299+
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
300300
}
301301

302302
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
@@ -422,6 +422,16 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
422422
}
423423
}
424424

425+
_handleBlur(): void {
426+
// Blur events will fire as soon as the user has their pointer down on an option. We don't
427+
// mark the control as touched in this case, because it can cause the validation to be run
428+
// before a value has been assigned. Instead, we skip marking it as touched from here
429+
// and we do so once the panel has closed.
430+
if (!this.panelOpen) {
431+
this._onTouched();
432+
}
433+
}
434+
425435
/**
426436
* In "auto" mode, the label will animate down as soon as focus is lost.
427437
* This causes the value to jump when selecting an option with the mouse.
@@ -551,6 +561,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
551561
}
552562

553563
this.closePanel();
564+
this._onTouched();
554565
}
555566

556567
/**

src/lib/autocomplete/autocomplete.spec.ts

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

734-
it('should mark the autocomplete control as touched on blur', () => {
735-
fixture.componentInstance.trigger.openPanel();
736-
fixture.detectChanges();
734+
it('should mark the autocomplete control as touched on blur while panel is closed', () => {
737735
expect(fixture.componentInstance.stateCtrl.touched)
738736
.toBe(false, `Expected control to start out untouched.`);
739737

@@ -744,6 +742,28 @@ describe('MatAutocomplete', () => {
744742
.toBe(true, `Expected control to become touched on blur.`);
745743
});
746744

745+
it('should defer marking the control as touched if it is blurred while open', () => {
746+
fixture.componentInstance.trigger.openPanel();
747+
fixture.detectChanges();
748+
zone.simulateZoneExit();
749+
750+
expect(fixture.componentInstance.stateCtrl.touched)
751+
.toBe(false, 'Expected control to start out untouched.');
752+
753+
dispatchFakeEvent(input, 'blur');
754+
fixture.detectChanges();
755+
756+
expect(fixture.componentInstance.stateCtrl.touched)
757+
.toBe(false, 'Expected control to stay untouched.');
758+
759+
// Simulate clicking outside the panel.
760+
dispatchFakeEvent(document, 'click');
761+
fixture.detectChanges();
762+
763+
expect(fixture.componentInstance.stateCtrl.touched)
764+
.toBe(true, 'Expected control to become touched once panel closes.');
765+
});
766+
747767
it('should disable the input when used with a value accessor and without `matInput`', () => {
748768
overlayContainer.ngOnDestroy();
749769
fixture.destroy();

0 commit comments

Comments
 (0)