Skip to content

Commit 98c9a4a

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 809d991 commit 98c9a4a

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
115115
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
116116
// a little earlier. This avoids issues where IE delays the focusing of the input.
117117
'(focusin)': '_handleFocus()',
118-
'(blur)': '_onTouched()',
118+
'(blur)': '_handleBlur()',
119119
'(input)': '_handleInput($event)',
120120
'(keydown)': '_handleKeydown($event)',
121121
},
@@ -336,7 +336,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
336336
/** Stream of autocomplete option selections. */
337337
readonly optionSelections: Observable<MatOptionSelectionChange> = defer(() => {
338338
if (this.autocomplete && this.autocomplete.options) {
339-
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
339+
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
340340
}
341341

342342
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
@@ -459,6 +459,16 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
459459
}
460460
}
461461

462+
_handleBlur(): void {
463+
// Blur events will fire as soon as the user has their pointer down on an option. We don't
464+
// mark the control as touched in this case, because it can cause the validation to be run
465+
// before a value has been assigned. Instead, we skip marking it as touched from here
466+
// and we do so once the panel has closed.
467+
if (!this.panelOpen) {
468+
this._onTouched();
469+
}
470+
}
471+
462472
/**
463473
* In "auto" mode, the label will animate down as soon as focus is lost.
464474
* This causes the value to jump when selecting an option with the mouse.
@@ -596,6 +606,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
596606
}
597607

598608
this.closePanel();
609+
this._onTouched();
599610
}
600611

601612
/**

src/material/autocomplete/autocomplete.spec.ts

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

813-
it('should mark the autocomplete control as touched on blur', () => {
814-
fixture.componentInstance.trigger.openPanel();
815-
fixture.detectChanges();
813+
it('should mark the autocomplete control as touched on blur while panel is closed', () => {
816814
expect(fixture.componentInstance.stateCtrl.touched)
817815
.toBe(false, `Expected control to start out untouched.`);
818816

@@ -823,6 +821,28 @@ describe('MatAutocomplete', () => {
823821
.toBe(true, `Expected control to become touched on blur.`);
824822
});
825823

824+
it('should defer marking the control as touched if it is blurred while open', () => {
825+
fixture.componentInstance.trigger.openPanel();
826+
fixture.detectChanges();
827+
zone.simulateZoneExit();
828+
829+
expect(fixture.componentInstance.stateCtrl.touched)
830+
.toBe(false, 'Expected control to start out untouched.');
831+
832+
dispatchFakeEvent(input, 'blur');
833+
fixture.detectChanges();
834+
835+
expect(fixture.componentInstance.stateCtrl.touched)
836+
.toBe(false, 'Expected control to stay untouched.');
837+
838+
// Simulate clicking outside the panel.
839+
dispatchFakeEvent(document, 'click');
840+
fixture.detectChanges();
841+
842+
expect(fixture.componentInstance.stateCtrl.touched)
843+
.toBe(true, 'Expected control to become touched once panel closes.');
844+
});
845+
826846
it('should disable the input when used with a value accessor and without `matInput`', () => {
827847
overlayContainer.ngOnDestroy();
828848
fixture.destroy();

tools/public_api_guard/material/autocomplete.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export declare class MatAutocompleteTrigger implements ControlValueAccessor, Aft
8282
readonly panelOpen: boolean;
8383
position: 'auto' | 'above' | 'below';
8484
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality, _formField: MatFormField, _document: any, _viewportRuler?: ViewportRuler | undefined);
85+
_handleBlur(): void;
8586
_handleFocus(): void;
8687
_handleInput(event: KeyboardEvent): void;
8788
_handleKeydown(event: KeyboardEvent): void;

0 commit comments

Comments
 (0)