Skip to content

fix(material/autocomplete): mark control as touched once panel is closed #18318

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($event)',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick()',
Expand Down
63 changes: 61 additions & 2 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,6 @@ describe('MDC-based MatAutocomplete', () => {
});

it('should mark the autocomplete control as touched on blur', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched)
.withContext(`Expected control to start out untouched.`)
.toBe(false);
Expand All @@ -931,6 +929,67 @@ describe('MDC-based MatAutocomplete', () => {
.toBe(true);
});

it('should mark the autocomplete control as touched when the panel is closed via the keyboard', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

dispatchKeyboardEvent(input, 'keydown', TAB);
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched).toBe(
true,
`Expected control to become touched on blur.`,
);
}));

it('should mark the autocomplete control as touched when the panel is closed by clicking away', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

dispatchFakeEvent(document, 'click');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched).toBe(
true,
`Expected control to become touched on blur.`,
);
}));

it(
'should not mark the autocomplete control as touched when the panel is closed ' +
'programmatically',
fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();

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

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

_handleBlur(event: FocusEvent) {
if (!this.panelOpen || !event.isTrusted) {
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 @@ -600,6 +606,7 @@ export abstract class _MatAutocompleteTriggerBase
this._element.nativeElement.focus();
}

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

Expand Down Expand Up @@ -835,8 +842,8 @@ export abstract class _MatAutocompleteTriggerBase
// 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()',
'(input)': '_handleInput($event)',
'(blur)': '_handleBlur($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick()',
},
Expand Down
59 changes: 57 additions & 2 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,6 @@ describe('MatAutocomplete', () => {
});

it('should mark the autocomplete control as touched on blur', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched)
.withContext(`Expected control to start out untouched.`)
.toBe(false);
Expand All @@ -926,6 +924,63 @@ describe('MatAutocomplete', () => {
.toBe(true);
});

it('should mark the autocomplete control as touched when the panel is closed via the keyboard', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

dispatchKeyboardEvent(input, 'keydown', TAB);
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.withContext(`Expected control to become touched on blur.`)
.toBe(true);
}));

it('should mark the autocomplete control as touched when the panel is closed by clicking away', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

dispatchFakeEvent(document, 'click');
fixture.detectChanges();

expect(fixture.componentInstance.stateCtrl.touched)
.withContext(`Expected control to become touched on blur.`)
.toBe(true);
}));

it(
'should not mark the autocomplete control as touched when the panel is closed ' +
'programmatically',
fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();

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

fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();

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

it('should disable the input when used with a value accessor and without `matInput`', () => {
overlayContainer.ngOnDestroy();
fixture.destroy();
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/material/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
closePanel(): void;
connectedTo: _MatAutocompleteOriginBase;
// (undocumented)
_handleBlur(event: FocusEvent): void;
// (undocumented)
_handleClick(): void;
// (undocumented)
_handleFocus(): void;
Expand Down