Skip to content

Commit a247af0

Browse files
fix(simple-combo): trigger selectionChanging on input clear
1 parent 5a70555 commit a247af0

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,48 @@ describe('IgxSimpleCombo', () => {
15281528
expect(combo.filteredData.length).toBeLessThan(combo.data.length)
15291529
expect(combo.filteredData[0].field).toBe(target.value)
15301530
});
1531+
1532+
it('should emit selectionChanging event when input value changes and input loses focus', () => {
1533+
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1534+
fixture.detectChanges();
1535+
1536+
combo.select('Connecticut');
1537+
fixture.detectChanges();
1538+
1539+
expect(combo.selectionChanging.emit).toHaveBeenCalledTimes(1);
1540+
expect(combo.selectionChanging.emit).toHaveBeenCalledWith({
1541+
newValue: "Connecticut",
1542+
oldValue: undefined,
1543+
newSelection: {
1544+
field: "Connecticut",
1545+
region: "New England"
1546+
},
1547+
oldSelection: undefined,
1548+
displayText: 'Connecticut',
1549+
owner: combo,
1550+
cancel: false
1551+
});
1552+
1553+
combo.handleInputChange('z');
1554+
fixture.detectChanges();
1555+
1556+
combo.onBlur();
1557+
fixture.detectChanges();
1558+
1559+
expect(combo.selectionChanging.emit).toHaveBeenCalledTimes(2);
1560+
expect(combo.selectionChanging.emit).toHaveBeenCalledWith({
1561+
oldValue: "Connecticut",
1562+
newValue: undefined,
1563+
oldSelection: {
1564+
field: "Connecticut",
1565+
region: "New England"
1566+
},
1567+
newSelection: undefined,
1568+
owner: combo,
1569+
displayText: "",
1570+
cancel: false
1571+
});
1572+
});
15311573
});
15321574

15331575
describe('Display density', () => {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
115115

116116
private _collapsing = false;
117117

118+
private _previousSelection = { selectedItem: '', selection: [] };
119+
118120
/** @hidden @internal */
119121
public get filteredData(): any[] | null {
120122
return this._filteredData;
@@ -279,6 +281,9 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
279281

280282
/** @hidden @internal */
281283
public override handleInputChange(event?: any): void {
284+
if (this.hasSelectedItem) {
285+
this._previousSelection = { selectedItem: this.selectedItem, selection: this.selection };
286+
}
282287
if (event !== undefined) {
283288
this.filterValue = this.searchValue = typeof event === 'string' ? event : event.target.value;
284289
}
@@ -466,6 +471,10 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
466471
};
467472
if (args.newSelection !== args.oldSelection) {
468473
this.selectionChanging.emit(args);
474+
} else if (this._updateInput && newSelection.size === 0 && this._previousSelection.selectedItem !== '') {
475+
args.oldValue = this._previousSelection.selectedItem;
476+
args.oldSelection = this._previousSelection.selection;
477+
this.selectionChanging.emit(args);
469478
}
470479
// TODO: refactor below code as it sets the selection and the display text
471480
if (!args.cancel) {
@@ -482,9 +491,15 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
482491
}
483492
this._onChangeCallback(args.newValue);
484493
this._updateInput = true;
494+
} else if (args.cancel) {
495+
if (this._updateInput && this._previousSelection.selectedItem !== '') {
496+
this.selectionService.select_items(this.id, [this._previousSelection.selectedItem], true);
497+
this._value = [this._previousSelection.selectedItem];
498+
}
485499
} else if (this.isRemote) {
486500
this.registerRemoteEntries(newValueAsArray, false);
487-
}
501+
}
502+
this._previousSelection = { selectedItem: '', selection: [] };
488503
}
489504

490505
protected createDisplayText(newSelection: any[], oldSelection: any[]): string {

0 commit comments

Comments
 (0)