Skip to content

Commit 28810e1

Browse files
committed
fix(combo): onBlur and onFocus event handling this._onTouchedCallback();
1 parent f55827a commit 28810e1

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,11 +1258,17 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12581258

12591259
/** @hidden @internal */
12601260
public onBlur() {
1261+
this._onTouchedCallback();
12611262
if (this.collapsed) {
12621263
this.validateComboState();
12631264
}
12641265
}
12651266

1267+
/** @hidden @internal */
1268+
public onFocus(): void {
1269+
this._onTouchedCallback();
1270+
}
1271+
12661272
/** @hidden @internal */
12671273
public setActiveDescendant(): void {
12681274
this.activeDescendant = this.dropdown.focusedItem?.id || '';
@@ -1288,8 +1294,6 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12881294
};
12891295

12901296
private validateComboState() {
1291-
this._onTouchedCallback();
1292-
12931297
if (this.ngControl && this.ngControl.invalid) {
12941298
this.valid = IgxInputState.INVALID;
12951299
} else {

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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,23 +3416,28 @@ describe('igxCombo', () => {
34163416
expect(combo.valid).toEqual(IgxInputState.INITIAL);
34173417
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
34183418
}));
3419-
it('should mark the combo as touched and invalid when opened and the user clicks away', fakeAsync(() => {
3420-
// Access the NgModel for the testCombo
3419+
it('should mark as touched and invalid when combo is focused, dropdown appears, and user clicks away without selection', fakeAsync(() => {
34213420
const ngModel = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel);
34223421
expect(combo.valid).toEqual(IgxInputState.INITIAL);
34233422
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
34243423
expect(ngModel.touched).toBeFalse();
3424+
34253425
combo.open();
3426+
input.triggerEventHandler('focus', {});
3427+
fixture.detectChanges();
3428+
expect(ngModel.touched).toBeTrue();
3429+
combo.searchInput.nativeElement.focus();
34263430
fixture.detectChanges();
3427-
34283431
const documentClickEvent = new MouseEvent('click', { bubbles: true });
34293432
document.body.dispatchEvent(documentClickEvent);
34303433
fixture.detectChanges();
34313434
tick();
3432-
3435+
document.body.focus();
3436+
fixture.detectChanges();
3437+
tick();
34333438
expect(combo.valid).toEqual(IgxInputState.INVALID);
34343439
expect(combo.comboInput.valid).toEqual(IgxInputState.INVALID);
3435-
expect(ngModel.touched).toBeTrue(); // NgModel should now be marked as touched
3440+
expect(ngModel.touched).toBeTrue();
34363441
}));
34373442
});
34383443
});

0 commit comments

Comments
 (0)