Skip to content

Commit 8d72171

Browse files
fix(simple-combo): selectionChanging on input clear and properly event cancel
1 parent 36ae1d7 commit 8d72171

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,9 +1553,6 @@ describe('IgxSimpleCombo', () => {
15531553
combo.handleInputChange('z');
15541554
fixture.detectChanges();
15551555

1556-
combo.onBlur();
1557-
fixture.detectChanges();
1558-
15591556
expect(combo.selectionChanging.emit).toHaveBeenCalledTimes(2);
15601557
expect(combo.selectionChanging.emit).toHaveBeenCalledWith({
15611558
oldValue: "Connecticut",
@@ -1570,6 +1567,41 @@ describe('IgxSimpleCombo', () => {
15701567
cancel: false
15711568
});
15721569
});
1570+
1571+
it('should not change selection when selectionChanging event is canceled', () => {
1572+
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1573+
fixture.detectChanges();
1574+
1575+
combo.select('Connecticut');
1576+
fixture.detectChanges();
1577+
1578+
expect(combo.selection).toEqual({
1579+
field: 'Connecticut',
1580+
region: 'New England'
1581+
});
1582+
1583+
const cancelEventSpy = spyOn(combo.selectionChanging, 'emit').and.callFake((args: ISimpleComboSelectionChangingEventArgs) => {
1584+
args.cancel = true;
1585+
});
1586+
1587+
combo.handleInputChange('z');
1588+
fixture.detectChanges();
1589+
1590+
expect(cancelEventSpy).toHaveBeenCalled();
1591+
expect(combo.selection).toEqual({
1592+
field: 'Connecticut',
1593+
region: 'New England'
1594+
});
1595+
1596+
combo.handleInputChange(' ');
1597+
fixture.detectChanges();
1598+
1599+
expect(cancelEventSpy).toHaveBeenCalled();
1600+
expect(combo.selection).toEqual({
1601+
field: 'Connecticut',
1602+
region: 'New England'
1603+
});
1604+
});
15731605
});
15741606

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

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

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

116116
private _collapsing = false;
117117

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

282280
/** @hidden @internal */
283281
public override handleInputChange(event?: any): void {
284-
if (this.hasSelectedItem) {
285-
this._previousSelection = { selectedItem: this.selectedItem, selection: this.selection };
286-
}
287282
if (event !== undefined) {
288283
this.filterValue = this.searchValue = typeof event === 'string' ? event : event.target.value;
289284
}
@@ -297,7 +292,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
297292
this.filterValue = '';
298293
}
299294
if (super.selection.length) {
300-
this.selectionService.clear(this.id);
295+
this.clearSelection();
301296
}
302297
// when filtering the focused item should be the first item or the currently selected item
303298
if (!this.dropdown.focusedItem || this.dropdown.focusedItem.id !== this.dropdown.items[0].id) {
@@ -471,10 +466,6 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
471466
};
472467
if (args.newSelection !== args.oldSelection) {
473468
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);
478469
}
479470
// TODO: refactor below code as it sets the selection and the display text
480471
if (!args.cancel) {
@@ -493,13 +484,7 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
493484
this._updateInput = true;
494485
} else if (this.isRemote) {
495486
this.registerRemoteEntries(newValueAsArray, false);
496-
} else if (args.cancel) {
497-
if (this._updateInput && this._previousSelection.selectedItem !== '') {
498-
this.selectionService.select_items(this.id, [this._previousSelection.selectedItem], true);
499-
this._value = [this._previousSelection.selectedItem];
500-
}
501487
}
502-
this._previousSelection = { selectedItem: '', selection: [] };
503488
}
504489

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

0 commit comments

Comments
 (0)