Skip to content

Commit fedd123

Browse files
fix(simple-combo): preserved the input value when selectionChanging is canceled
1 parent 2a3d7a0 commit fedd123

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
@@ -402,13 +402,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
402402
if (this.disabled) {
403403
return;
404404
}
405+
406+
const oldSelection = this.selection;
405407
this.clearSelection(true);
408+
406409
if(!this.collapsed){
407410
this.focusSearchInput(true);
408411
}
409412
event.stopPropagation();
410413

411-
this.comboInput.value = this.filterValue = this.searchValue = '';
414+
if (this.selection !== oldSelection) {
415+
this.comboInput.value = this.filterValue = this.searchValue = '';
416+
}
417+
412418
this.dropdown.focusedItem = null;
413419
this.composing = false;
414420
this.comboInput.focus();
@@ -500,6 +506,15 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
500506
this._updateInput = true;
501507
} else if (this.isRemote) {
502508
this.registerRemoteEntries(newValueAsArray, false);
509+
} else {
510+
args.displayText = this.createDisplayText(oldItems, []);
511+
512+
const oldSelectionArray = args.oldSelection ? [args.oldSelection] : [];
513+
this.comboInput.value = this._displayValue = this.searchValue = this.createDisplayText(oldSelectionArray, []);
514+
515+
if (this.isRemote) {
516+
this.registerRemoteEntries(newValueAsArray, false);
517+
}
503518
}
504519
}
505520

@@ -572,7 +587,10 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
572587

573588
private clear(): void {
574589
this.clearSelection(true);
575-
this.comboInput.value = this._displayValue = this.searchValue = '';
590+
const oldSelection = this.selection;
591+
if (this.selection !== oldSelection) {
592+
this.comboInput.value = this._displayValue = this.searchValue = '';
593+
}
576594
}
577595

578596
private isValid(value: any): boolean {

0 commit comments

Comments
 (0)