Skip to content

Commit 37f8028

Browse files
fix(simple-combo): preserved the input value when selectionChanging is canceled
1 parent 5e03c5d commit 37f8028

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,7 @@ describe('IgxSimpleCombo', () => {
15981598

15991599
it('should not change selection when selectionChanging event is canceled', () => {
16001600
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1601+
16011602
fixture.detectChanges();
16021603

16031604
combo.select('Connecticut');
@@ -1630,6 +1631,61 @@ describe('IgxSimpleCombo', () => {
16301631
region: 'New England'
16311632
});
16321633
});
1634+
1635+
1636+
it('should preserved the input value of the combo when selectionChanging event is canceled', () => {
1637+
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1638+
fixture.detectChanges();
1639+
1640+
const comboInput = fixture.debugElement.query(By.css(`.igx-input-group__input`));
1641+
fixture.detectChanges();
1642+
1643+
combo.select('Connecticut');
1644+
fixture.detectChanges();
1645+
1646+
expect(combo.selection).toEqual({
1647+
field: 'Connecticut',
1648+
region: 'New England'
1649+
});
1650+
1651+
const cancelEventSpy = spyOn(combo.selectionChanging, 'emit').and.callFake((args: ISimpleComboSelectionChangingEventArgs) => {
1652+
args.cancel = true;
1653+
});
1654+
1655+
const clearButton = fixture.debugElement.query(By.css(`.${CSS_CLASS_CLEARBUTTON}`));
1656+
clearButton.triggerEventHandler('click', UIInteractions.getMouseEvent('click'));
1657+
fixture.detectChanges();
1658+
1659+
expect(cancelEventSpy).toHaveBeenCalled();
1660+
expect(combo.selection).toEqual({
1661+
field: 'Connecticut',
1662+
region: 'New England'
1663+
});
1664+
1665+
expect(comboInput.nativeElement.value).toEqual('Connecticut');
1666+
1667+
combo.handleInputChange('z');
1668+
fixture.detectChanges();
1669+
1670+
expect(cancelEventSpy).toHaveBeenCalled();
1671+
expect(combo.selection).toEqual({
1672+
field: 'Connecticut',
1673+
region: 'New England'
1674+
});
1675+
1676+
expect(comboInput.nativeElement.value).toEqual('Connecticut');
1677+
1678+
combo.handleInputChange(' ');
1679+
fixture.detectChanges();
1680+
1681+
expect(cancelEventSpy).toHaveBeenCalled();
1682+
expect(combo.selection).toEqual({
1683+
field: 'Connecticut',
1684+
region: 'New England'
1685+
});
1686+
1687+
expect(comboInput.nativeElement.value).toEqual('Connecticut');
1688+
});
16331689
});
16341690

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

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
400400
if (this.disabled) {
401401
return;
402402
}
403+
404+
const oldSelection = this.selection;
403405
this.clearSelection(true);
406+
404407
if(!this.collapsed){
405408
this.focusSearchInput(true);
406409
}
407410
event.stopPropagation();
408411

409-
this.comboInput.value = this.filterValue = this.searchValue = '';
412+
if (this.selection !== oldSelection) {
413+
this.comboInput.value = this.filterValue = this.searchValue = '';
414+
}
415+
410416
this.dropdown.focusedItem = null;
411417
this.composing = false;
412418
this.comboInput.focus();
@@ -498,6 +504,15 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
498504
this._updateInput = true;
499505
} else if (this.isRemote) {
500506
this.registerRemoteEntries(newValueAsArray, false);
507+
} else {
508+
args.displayText = this.createDisplayText(oldItems, []);
509+
510+
const oldSelectionArray = args.oldSelection ? [args.oldSelection] : [];
511+
this.comboInput.value = this._displayValue = this.searchValue = this.createDisplayText(oldSelectionArray, []);
512+
513+
if (this.isRemote) {
514+
this.registerRemoteEntries(newValueAsArray, false);
515+
}
501516
}
502517
}
503518

@@ -570,7 +585,10 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
570585

571586
private clear(): void {
572587
this.clearSelection(true);
573-
this.comboInput.value = this._displayValue = this.searchValue = '';
588+
const oldSelection = this.selection;
589+
if (this.selection !== oldSelection) {
590+
this.comboInput.value = this._displayValue = this.searchValue = '';
591+
}
574592
}
575593

576594
private isValid(value: any): boolean {

0 commit comments

Comments
 (0)