Skip to content

Commit 7b6c74f

Browse files
Merge pull request #14924 from IgniteUI/aahmedov/fix-touched-ivalid-color-not-applied-#14900-17.2.x
fix(combo): update touched property and apply invalid color on blur when required - 17.2.x
2 parents fa249c6 + 40020d7 commit 7b6c74f

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
@@ -1218,6 +1218,9 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12181218
/** @hidden @internal */
12191219
public handleClosed() {
12201220
this.closed.emit({ owner: this });
1221+
if(this.comboInput.nativeElement !== document.activeElement){
1222+
this.validateComboState();
1223+
}
12211224
}
12221225

12231226
/** @hidden @internal */
@@ -1257,14 +1260,15 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12571260
public onBlur() {
12581261
if (this.collapsed) {
12591262
this._onTouchedCallback();
1260-
if (this.ngControl && this.ngControl.invalid) {
1261-
this.valid = IgxInputState.INVALID;
1262-
} else {
1263-
this.valid = IgxInputState.INITIAL;
1264-
}
1263+
this.validateComboState();
12651264
}
12661265
}
12671266

1267+
/** @hidden @internal */
1268+
public onFocus(): void {
1269+
this._onTouchedCallback();
1270+
}
1271+
12681272
/** @hidden @internal */
12691273
public setActiveDescendant(): void {
12701274
this.activeDescendant = this.dropdown.focusedItem?.id || '';
@@ -1289,6 +1293,14 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12891293
this.manageRequiredAsterisk();
12901294
};
12911295

1296+
private validateComboState() {
1297+
if (this.ngControl && this.ngControl.invalid) {
1298+
this.valid = IgxInputState.INVALID;
1299+
} else {
1300+
this.valid = IgxInputState.INITIAL;
1301+
}
1302+
}
1303+
12921304
private get isTouchedOrDirty(): boolean {
12931305
return (this.ngControl.control.touched || this.ngControl.control.dirty);
12941306
}

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
@@ -3416,6 +3416,27 @@ describe('igxCombo', () => {
34163416
expect(combo.valid).toEqual(IgxInputState.INITIAL);
34173417
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
34183418
}));
3419+
it('should mark as touched and invalid when combo is focused, dropdown appears, and user clicks away without selection', fakeAsync(() => {
3420+
const ngModel = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel);
3421+
expect(combo.valid).toEqual(IgxInputState.INITIAL);
3422+
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
3423+
expect(ngModel.touched).toBeFalse();
3424+
3425+
combo.open();
3426+
input.triggerEventHandler('focus', {});
3427+
fixture.detectChanges();
3428+
expect(ngModel.touched).toBeTrue();
3429+
const documentClickEvent = new MouseEvent('click', { bubbles: true });
3430+
document.body.dispatchEvent(documentClickEvent);
3431+
fixture.detectChanges();
3432+
tick();
3433+
document.body.focus();
3434+
fixture.detectChanges();
3435+
tick();
3436+
expect(combo.valid).toEqual(IgxInputState.INVALID);
3437+
expect(combo.comboInput.valid).toEqual(IgxInputState.INVALID);
3438+
expect(ngModel.touched).toBeTrue();
3439+
}));
34193440
});
34203441
});
34213442
describe('Display density', () => {
@@ -3627,7 +3648,7 @@ class IgxComboFormComponent {
36273648
@Component({
36283649
template: `
36293650
<form #form="ngForm">
3630-
<igx-combo #testCombo class="input-container" [placeholder]="'Locations'"
3651+
<igx-combo #testCombo #testComboNgModel="ngModel" class="input-container" [placeholder]="'Locations'"
36313652
name="anyName" required [(ngModel)]="values"
36323653
[data]="items" [filterable]="filterableFlag"
36333654
[displayKey]="'field'" [valueKey]="'field'"

0 commit comments

Comments
 (0)