Skip to content

Commit 0884dcc

Browse files
committed
fix(material/autocomplete): mark control as touched once panel is closed
Currently we mark the autocomplete control as touched on `blur`. The problem is that the `blur` event happens a split second before the panel is closed which can cause the error styling to show up and disappear quickly which looks glitchy. These changes change it so that the control is marked as touched once the panel is closed. Also makes a couple of underscored properties private since they weren't used anywhere in the view. Fixes #18313.
1 parent 8424209 commit 0884dcc

File tree

3 files changed

+98
-11
lines changed

3 files changed

+98
-11
lines changed

src/material-experimental/mdc-autocomplete/autocomplete.spec.ts

+48
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,54 @@ describe('MDC-based MatAutocomplete', () => {
843843
.toBe(false);
844844
});
845845

846+
it('should mark the autocomplete control as touched when the panel is closed via the keyboard',
847+
fakeAsync(() => {
848+
fixture.componentInstance.trigger.openPanel();
849+
fixture.detectChanges();
850+
zone.simulateZoneExit();
851+
852+
expect(fixture.componentInstance.stateCtrl.touched)
853+
.toBe(false, `Expected control to start out untouched.`);
854+
855+
dispatchKeyboardEvent(input, 'keydown', TAB);
856+
fixture.detectChanges();
857+
858+
expect(fixture.componentInstance.stateCtrl.touched)
859+
.toBe(true, `Expected control to become touched on blur.`);
860+
}));
861+
862+
it('should mark the autocomplete control as touched when the panel is closed by clicking away',
863+
fakeAsync(() => {
864+
fixture.componentInstance.trigger.openPanel();
865+
fixture.detectChanges();
866+
zone.simulateZoneExit();
867+
868+
expect(fixture.componentInstance.stateCtrl.touched)
869+
.toBe(false, `Expected control to start out untouched.`);
870+
871+
dispatchFakeEvent(document, 'click');
872+
fixture.detectChanges();
873+
874+
expect(fixture.componentInstance.stateCtrl.touched)
875+
.toBe(true, `Expected control to become touched on blur.`);
876+
}));
877+
878+
it('should not mark the autocomplete control as touched when the panel is closed ' +
879+
'programmatically', fakeAsync(() => {
880+
fixture.componentInstance.trigger.openPanel();
881+
fixture.detectChanges();
882+
zone.simulateZoneExit();
883+
884+
expect(fixture.componentInstance.stateCtrl.touched)
885+
.toBe(false, `Expected control to start out untouched.`);
886+
887+
fixture.componentInstance.trigger.closePanel();
888+
fixture.detectChanges();
889+
890+
expect(fixture.componentInstance.stateCtrl.touched)
891+
.toBe(false, `Expected control to stay untouched.`);
892+
}));
893+
846894
it('should mark the autocomplete control as touched on blur', () => {
847895
fixture.componentInstance.trigger.openPanel();
848896
fixture.detectChanges();

src/material/autocomplete/autocomplete-trigger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
540540
}
541541

542542
this.closePanel();
543+
this._onTouched();
543544
}
544545

545546
/**
@@ -761,7 +762,6 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
761762
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
762763
// a little earlier. This avoids issues where IE delays the focusing of the input.
763764
'(focusin)': '_handleFocus()',
764-
'(blur)': '_onTouched()',
765765
'(input)': '_handleInput($event)',
766766
'(keydown)': '_handleKeydown($event)',
767767
},

src/material/autocomplete/autocomplete.spec.ts

+49-10
Original file line numberDiff line numberDiff line change
@@ -837,18 +837,57 @@ describe('MatAutocomplete', () => {
837837
.toBe(false);
838838
});
839839

840-
it('should mark the autocomplete control as touched on blur', () => {
841-
fixture.componentInstance.trigger.openPanel();
842-
fixture.detectChanges();
843-
expect(fixture.componentInstance.stateCtrl.touched)
844-
.withContext(`Expected control to start out untouched.`).toBe(false);
840+
it('should mark the autocomplete control as touched when the panel is closed via the keyboard',
841+
fakeAsync(() => {
842+
fixture.componentInstance.trigger.openPanel();
843+
fixture.detectChanges();
844+
zone.simulateZoneExit();
845845

846-
dispatchFakeEvent(input, 'blur');
847-
fixture.detectChanges();
846+
expect(fixture.componentInstance.stateCtrl.touched)
847+
.withContext(`Expected control to start out untouched.`)
848+
.toBe(false);
848849

849-
expect(fixture.componentInstance.stateCtrl.touched)
850-
.withContext(`Expected control to become touched on blur.`).toBe(true);
851-
});
850+
dispatchKeyboardEvent(input, 'keydown', TAB);
851+
fixture.detectChanges();
852+
853+
expect(fixture.componentInstance.stateCtrl.touched)
854+
.withContext(`Expected control to become touched on blur.`)
855+
.toBe(true);
856+
}));
857+
858+
it('should mark the autocomplete control as touched when the panel is closed by clicking away',
859+
fakeAsync(() => {
860+
fixture.componentInstance.trigger.openPanel();
861+
fixture.detectChanges();
862+
zone.simulateZoneExit();
863+
864+
expect(fixture.componentInstance.stateCtrl.touched)
865+
.withContext(`Expected control to start out untouched.`)
866+
.toBe(false);
867+
868+
dispatchFakeEvent(document, 'click');
869+
fixture.detectChanges();
870+
871+
expect(fixture.componentInstance.stateCtrl.touched)
872+
.withContext(`Expected control to become touched on blur.`)
873+
.toBe(true);
874+
}));
875+
876+
it('should not mark the autocomplete control as touched when the panel is closed ' +
877+
'programmatically', fakeAsync(() => {
878+
fixture.componentInstance.trigger.openPanel();
879+
fixture.detectChanges();
880+
zone.simulateZoneExit();
881+
882+
expect(fixture.componentInstance.stateCtrl.touched)
883+
.toBe(false, `Expected control to start out untouched.`);
884+
885+
fixture.componentInstance.trigger.closePanel();
886+
fixture.detectChanges();
887+
888+
expect(fixture.componentInstance.stateCtrl.touched)
889+
.toBe(false, `Expected control to stay untouched.`);
890+
}));
852891

853892
it('should disable the input when used with a value accessor and without `matInput`', () => {
854893
overlayContainer.ngOnDestroy();

0 commit comments

Comments
 (0)