Skip to content

Commit d0b8dd5

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 09dc459 commit d0b8dd5

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
@@ -116,7 +116,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
116116
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
117117
// a little earlier. This avoids issues where IE delays the focusing of the input.
118118
'(focusin)': '_handleFocus()',
119-
'(blur)': '_onTouched()',
119+
'(blur)': '_handleBlur()',
120120
'(input)': '_handleInput($event)',
121121
'(keydown)': '_handleKeydown($event)',
122122
},
@@ -337,7 +337,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
337337
/** Stream of autocomplete option selections. */
338338
readonly optionSelections: Observable<MatOptionSelectionChange> = defer(() => {
339339
if (this.autocomplete && this.autocomplete.options) {
340-
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
340+
return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
341341
}
342342

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

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

606616
this.closePanel();
617+
this._onTouched();
607618
}
608619

609620
/**

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
@@ -90,6 +90,7 @@ export declare class MatAutocompleteTrigger implements ControlValueAccessor, Aft
9090
readonly panelOpen: boolean;
9191
position: 'auto' | 'above' | 'below';
9292
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality, _formField: MatFormField, _document: any, _viewportRuler?: ViewportRuler | undefined);
93+
_handleBlur(): void;
9394
_handleFocus(): void;
9495
_handleInput(event: KeyboardEvent): void;
9596
_handleKeydown(event: KeyboardEvent): void;

0 commit comments

Comments
 (0)