Skip to content

Commit 8632e23

Browse files
Merge pull request #14923 from IgniteUI/aahmedov/fix-touched-ivalid-color-not-applied-#14900-18.1.x
fix(combo): update touched property and apply invalid color on blur when required - 18.1.x
2 parents 16555a8 + d474b02 commit 8632e23

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

projects/igniteui-angular/src/lib/combo/combo.common.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,9 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12111211
/** @hidden @internal */
12121212
public handleClosed() {
12131213
this.closed.emit({ owner: this });
1214+
if(this.comboInput.nativeElement !== this.document.activeElement){
1215+
this.validateComboState();
1216+
}
12141217
}
12151218

12161219
/** @hidden @internal */
@@ -1250,14 +1253,15 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12501253
public onBlur() {
12511254
if (this.collapsed) {
12521255
this._onTouchedCallback();
1253-
if (this.ngControl && this.ngControl.invalid) {
1254-
this.valid = IgxInputState.INVALID;
1255-
} else {
1256-
this.valid = IgxInputState.INITIAL;
1257-
}
1256+
this.validateComboState();
12581257
}
12591258
}
12601259

1260+
/** @hidden @internal */
1261+
public onFocus(): void {
1262+
this._onTouchedCallback();
1263+
}
1264+
12611265
/** @hidden @internal */
12621266
public setActiveDescendant(): void {
12631267
this.activeDescendant = this.dropdown.focusedItem?.id || '';
@@ -1282,6 +1286,14 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12821286
this.manageRequiredAsterisk();
12831287
};
12841288

1289+
private validateComboState() {
1290+
if (this.ngControl && this.ngControl.invalid) {
1291+
this.valid = IgxInputState.INVALID;
1292+
} else {
1293+
this.valid = IgxInputState.INITIAL;
1294+
}
1295+
}
1296+
12851297
private get isTouchedOrDirty(): boolean {
12861298
return (this.ngControl.control.touched || this.ngControl.control.dirty);
12871299
}

projects/igniteui-angular/src/lib/combo/combo.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
role="combobox" aria-haspopup="listbox"
1414
[attr.aria-expanded]="!dropdown.collapsed" [attr.aria-controls]="dropdown.listId"
1515
[attr.aria-labelledby]="ariaLabelledBy || label?.id || placeholder"
16-
(blur)="onBlur()" />
16+
(blur)="onBlur()"
17+
(focus)="onFocus()" />
1718
<ng-container ngProjectAs="igx-suffix">
1819
<ng-content select="igx-suffix"></ng-content>
1920
</ng-container>

projects/igniteui-angular/src/lib/combo/combo.component.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,27 @@ describe('igxCombo', () => {
36033603
expect(combo.valid).toEqual(IgxInputState.INITIAL);
36043604
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
36053605
}));
3606+
it('should mark as touched and invalid when combo is focused, dropdown appears, and user clicks away without selection', fakeAsync(() => {
3607+
const ngModel = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel);
3608+
expect(combo.valid).toEqual(IgxInputState.INITIAL);
3609+
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
3610+
expect(ngModel.touched).toBeFalse();
3611+
3612+
combo.open();
3613+
input.triggerEventHandler('focus', {});
3614+
fixture.detectChanges();
3615+
expect(ngModel.touched).toBeTrue();
3616+
const documentClickEvent = new MouseEvent('click', { bubbles: true });
3617+
document.body.dispatchEvent(documentClickEvent);
3618+
fixture.detectChanges();
3619+
tick();
3620+
document.body.focus();
3621+
fixture.detectChanges();
3622+
tick();
3623+
expect(combo.valid).toEqual(IgxInputState.INVALID);
3624+
expect(combo.comboInput.valid).toEqual(IgxInputState.INVALID);
3625+
expect(ngModel.touched).toBeTrue();
3626+
}));
36063627
});
36073628
});
36083629
describe('Display density', () => {
@@ -3784,7 +3805,7 @@ class IgxComboFormComponent {
37843805
@Component({
37853806
template: `
37863807
<form #form="ngForm">
3787-
<igx-combo #testCombo class="input-container" [placeholder]="'Locations'"
3808+
<igx-combo #testCombo #testComboNgModel="ngModel" class="input-container" [placeholder]="'Locations'"
37883809
name="anyName" required [(ngModel)]="values"
37893810
[data]="items" [filterable]="filterableFlag"
37903811
[displayKey]="'field'" [valueKey]="'field'"

0 commit comments

Comments
 (0)