Skip to content

Commit c0209fc

Browse files
crisbetojelbourn
authored andcommitted
fix(select): not marked as touched when clicking away (angular#8784)
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 angular#8573.
1 parent a2438fa commit c0209fc

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/lib/select/select.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ describe('MatSelect', () => {
11801180
.not.toContain('mat-selected', `Expected option w/ the old value not to be selected.`);
11811181
}));
11821182

1183-
it('should set the control to touched when the select is touched', fakeAsync(() => {
1183+
it('should set the control to touched when the select is blurred', fakeAsync(() => {
11841184
expect(fixture.componentInstance.control.touched)
11851185
.toEqual(false, `Expected the control to start off as untouched.`);
11861186

@@ -1203,6 +1203,26 @@ describe('MatSelect', () => {
12031203
.toEqual(true, `Expected the control to be touched as soon as focus left the select.`);
12041204
}));
12051205

1206+
it('should set the control to touched when the panel is closed', fakeAsync(() => {
1207+
expect(fixture.componentInstance.control.touched)
1208+
.toBe(false, 'Expected the control to start off as untouched.');
1209+
1210+
trigger.click();
1211+
dispatchFakeEvent(trigger, 'blur');
1212+
fixture.detectChanges();
1213+
flush();
1214+
1215+
expect(fixture.componentInstance.control.touched)
1216+
.toBe(false, 'Expected the control to stay untouched when menu opened.');
1217+
1218+
fixture.componentInstance.select.close();
1219+
fixture.detectChanges();
1220+
flush();
1221+
1222+
expect(fixture.componentInstance.control.touched)
1223+
.toBe(true, 'Expected the control to be touched when the panel was closed.');
1224+
}));
1225+
12061226
it('should not set touched when a disabled select is touched', fakeAsync(() => {
12071227
expect(fixture.componentInstance.control.touched)
12081228
.toBe(false, 'Expected the control to start off as untouched.');

src/lib/select/select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
544544
if (this._panelOpen) {
545545
this._panelOpen = false;
546546
this._changeDetectorRef.markForCheck();
547+
this._onTouched();
547548
this.focus();
548549
}
549550
}

0 commit comments

Comments
 (0)