Skip to content

fix(autocomplete): form control being marked as touched too early when clicking on an option #11908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const MAT_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
// a little earlier. This avoids issues where IE delays the focusing of the input.
'(focusin)': '_handleFocus()',
'(blur)': '_onTouched()',
'(blur)': '_handleBlur()',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
},
Expand Down
26 changes: 23 additions & 3 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,7 @@ describe('MDC-based MatAutocomplete', () => {
.toBe(false, `Expected control to stay pristine if value is set programmatically.`);
});

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

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

it('should defer marking the control as touched if it is blurred while open', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(false, 'Expected control to start out untouched.');

dispatchFakeEvent(input, 'blur');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(false, 'Expected control to stay untouched.');

// Simulate clicking outside the panel.
dispatchFakeEvent(document, 'click');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(true, 'Expected control to become touched once panel closes.');
});

it('should disable the input when used with a value accessor and without `matInput`', () => {
overlayContainer.ngOnDestroy();
fixture.destroy();
Expand Down
13 changes: 12 additions & 1 deletion src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
}
}

_handleBlur(): void {
// Blur events will fire as soon as the user has their pointer down on an option. We don't
// mark the control as touched in this case, because it can cause the validation to be run
// before a value has been assigned. Instead, we skip marking it as touched from here
// and we do so once the panel has closed.
if (!this.panelOpen) {
this._onTouched();
}
}

/**
* In "auto" mode, the label will animate down as soon as focus is lost.
* This causes the value to jump when selecting an option with the mouse.
Expand Down Expand Up @@ -564,6 +574,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
}

this.closePanel();
this._onTouched();
}

/**
Expand Down Expand Up @@ -781,7 +792,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
// a little earlier. This avoids issues where IE delays the focusing of the input.
'(focusin)': '_handleFocus()',
'(blur)': '_onTouched()',
'(blur)': '_handleBlur()',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
},
Expand Down
26 changes: 23 additions & 3 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,7 @@ describe('MatAutocomplete', () => {
.toBe(false, `Expected control to stay pristine if value is set programmatically.`);
});

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

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

it('should defer marking the control as touched if it is blurred while open', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(false, 'Expected control to start out untouched.');

dispatchFakeEvent(input, 'blur');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(false, 'Expected control to stay untouched.');

// Simulate clicking outside the panel.
dispatchFakeEvent(document, 'click');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.toBe(true, 'Expected control to become touched once panel closes.');
});

it('should disable the input when used with a value accessor and without `matInput`', () => {
overlayContainer.ngOnDestroy();
fixture.destroy();
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export declare abstract class _MatAutocompleteTriggerBase implements ControlValu
get panelOpen(): boolean;
position: 'auto' | 'above' | 'below';
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality, _formField: MatFormField, _document: any, _viewportRuler: ViewportRuler, _defaults?: MatAutocompleteDefaultOptions | undefined);
_handleBlur(): void;
_handleFocus(): void;
_handleInput(event: KeyboardEvent): void;
_handleKeydown(event: KeyboardEvent): void;
Expand Down