From 42511bcb5e654f74f42d54f35f13d82b3c81c178 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 3 Dec 2017 16:43:27 +0100 Subject: [PATCH] fix(select): not marked as touched when clicking away Currently `mat-select` is only marked as touched when the trigger is blurred. This means that it still considered untouched if the user opens the panel and clicks away, which then requires another click to show any validation messages. These changes switch to considering the control as touched whenever the panel is closed. Fixes #8573. --- src/lib/select/select.spec.ts | 22 +++++++++++++++++++++- src/lib/select/select.ts | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 490ba33ac4fc..a0016d434f37 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -1118,7 +1118,7 @@ describe('MatSelect', () => { .not.toContain('mat-selected', `Expected option w/ the old value not to be selected.`); })); - it('should set the control to touched when the select is touched', fakeAsync(() => { + it('should set the control to touched when the select is blurred', fakeAsync(() => { expect(fixture.componentInstance.control.touched) .toEqual(false, `Expected the control to start off as untouched.`); @@ -1141,6 +1141,26 @@ describe('MatSelect', () => { .toEqual(true, `Expected the control to be touched as soon as focus left the select.`); })); + it('should set the control to touched when the panel is closed', fakeAsync(() => { + expect(fixture.componentInstance.control.touched) + .toBe(false, 'Expected the control to start off as untouched.'); + + trigger.click(); + dispatchFakeEvent(trigger, 'blur'); + fixture.detectChanges(); + flush(); + + expect(fixture.componentInstance.control.touched) + .toBe(false, 'Expected the control to stay untouched when menu opened.'); + + fixture.componentInstance.select.close(); + fixture.detectChanges(); + flush(); + + expect(fixture.componentInstance.control.touched) + .toBe(true, 'Expected the control to be touched when the panel was closed.'); + })); + it('should not set touched when a disabled select is touched', fakeAsync(() => { expect(fixture.componentInstance.control.touched) .toBe(false, 'Expected the control to start off as untouched.'); diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 5785e6459ecb..b51b45658034 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -540,6 +540,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit, if (this._panelOpen) { this._panelOpen = false; this._changeDetectorRef.markForCheck(); + this._onTouched(); this.focus(); } }