Skip to content

Commit e787f2f

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

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
@@ -1251,11 +1251,17 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12511251

12521252
/** @hidden @internal */
12531253
public onBlur() {
1254+
this._onTouchedCallback();
12541255
if (this.collapsed) {
12551256
this.validateComboState();
12561257
}
12571258
}
12581259

1260+
/** @hidden @internal */
1261+
public onFocus(): void {
1262+
this._onTouchedCallback();
1263+
}
1264+
12591265
/** @hidden @internal */
12601266
public setActiveDescendant(): void {
12611267
this.activeDescendant = this.dropdown.focusedItem?.id || '';
@@ -1281,8 +1287,6 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12811287
};
12821288

12831289
private validateComboState() {
1284-
this._onTouchedCallback();
1285-
12861290
if (this.ngControl && this.ngControl.invalid) {
12871291
this.valid = IgxInputState.INVALID;
12881292
} 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
@@ -3603,23 +3603,28 @@ describe('igxCombo', () => {
36033603
expect(combo.valid).toEqual(IgxInputState.INITIAL);
36043604
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
36053605
}));
3606-
it('should mark the combo as touched and invalid when opened and the user clicks away', fakeAsync(() => {
3607-
// Access the NgModel for the testCombo
3606+
it('should mark as touched and invalid when combo is focused, dropdown appears, and user clicks away without selection', fakeAsync(() => {
36083607
const ngModel = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel);
36093608
expect(combo.valid).toEqual(IgxInputState.INITIAL);
36103609
expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL);
36113610
expect(ngModel.touched).toBeFalse();
3611+
36123612
combo.open();
3613+
input.triggerEventHandler('focus', {});
3614+
fixture.detectChanges();
3615+
expect(ngModel.touched).toBeTrue();
3616+
combo.searchInput.nativeElement.focus();
36133617
fixture.detectChanges();
3614-
36153618
const documentClickEvent = new MouseEvent('click', { bubbles: true });
36163619
document.body.dispatchEvent(documentClickEvent);
36173620
fixture.detectChanges();
36183621
tick();
3619-
3622+
document.body.focus();
3623+
fixture.detectChanges();
3624+
tick();
36203625
expect(combo.valid).toEqual(IgxInputState.INVALID);
36213626
expect(combo.comboInput.valid).toEqual(IgxInputState.INVALID);
3622-
expect(ngModel.touched).toBeTrue(); // NgModel should now be marked as touched
3627+
expect(ngModel.touched).toBeTrue();
36233628
}));
36243629
});
36253630
});

0 commit comments

Comments
 (0)