Skip to content

Commit a3366a5

Browse files
authored
Merge pull request #12037 from IgniteUI/mkirkova/fix-11884
Preserve selection on blur when having a match and a partial match
2 parents 8ffeeb8 + 33806fc commit a3366a5

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,8 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12361236

12371237
protected findMatch = (element: any): boolean => {
12381238
const value = this.displayKey ? element[this.displayKey] : element;
1239-
return value?.toString().toLowerCase() === this.searchValue.trim().toLowerCase();
1239+
const searchValue = this.searchValue || this.comboInput?.value;
1240+
return value?.toString().toLowerCase() === searchValue.trim().toLowerCase();
12401241
};
12411242

12421243
protected manageRequiredAsterisk(): void {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ describe('igxCombo', () => {
609609
combo.ngOnInit();
610610
combo.data = data;
611611
combo.dropdown = dropdown;
612+
combo.comboInput = {
613+
value: '',
614+
} as any;
612615
combo.filterable = true;
613616
const matchSpy = spyOn<any>(combo, 'checkMatch').and.callThrough();
614617
spyOn(combo.searchInputUpdate, 'emit');

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,8 @@ describe('IgxSimpleCombo', () => {
796796
beforeAll(waitForAsync(() => {
797797
TestBed.configureTestingModule({
798798
declarations: [
799-
IgxSimpleComboSampleComponent
799+
IgxSimpleComboSampleComponent,
800+
IgxComboInContainerTestComponent
800801
],
801802
imports: [
802803
IgxSimpleComboModule,
@@ -1051,6 +1052,30 @@ describe('IgxSimpleCombo', () => {
10511052
expect(combo.value).toBeFalsy();
10521053
});
10531054

1055+
it('should not clear the selection and input on blur with a match', () => {
1056+
fixture = TestBed.createComponent(IgxComboInContainerTestComponent);
1057+
combo = fixture.componentInstance.combo;
1058+
fixture.detectChanges();
1059+
1060+
input = fixture.debugElement.query(By.css(`.${CSS_CLASS_COMBO_INPUTGROUP}`));
1061+
combo.data = ['Apples', 'Apple'];
1062+
1063+
combo.select(combo.data[1]);
1064+
fixture.detectChanges();
1065+
1066+
expect(combo.selection.length).toBe(1);
1067+
expect(combo.selection[0]).toEqual('Apple');
1068+
1069+
combo.open();
1070+
fixture.detectChanges();
1071+
1072+
UIInteractions.triggerEventHandlerKeyDown('Tab', input);
1073+
fixture.detectChanges();
1074+
1075+
expect(combo.value).toEqual('Apple');
1076+
expect(combo.selection.length).toEqual(1);
1077+
});
1078+
10541079
it('should empty any invalid item values', () => {
10551080
combo.valueKey = 'key';
10561081
combo.displayKey = 'value';

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,18 +448,16 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
448448
}
449449

450450
private clearOnBlur(): void {
451-
const filtered = this.filteredData.find(this.findAllMatches);
452-
if (filtered === undefined || filtered === null || this.selectionService.size(this.id) === 0) {
451+
const filtered = this.filteredData.find(this.findMatch);
452+
if (filtered === undefined || filtered === null || this.getElementKey(filtered) !== this.selectedItem) {
453453
this.clearAndClose();
454454
return;
455455
}
456-
if (this.isPartialMatch(filtered) || this.getElementVal(filtered) !== this._internalFilter) {
457-
this.clearAndClose();
458-
}
459456
}
460457

461-
private isPartialMatch(filtered: any): boolean {
462-
return !!this._internalFilter && this._internalFilter.length !== this.getElementVal(filtered).length;
458+
private getElementKey(element: any): any {
459+
const elementVal = this.valueKey ? element[this.valueKey] : element;
460+
return elementVal;
463461
}
464462

465463
private getElementVal(element: any): string {

0 commit comments

Comments
 (0)