Skip to content

Commit 257e9c1

Browse files
Merge pull request #14308 from IgniteUI/ganastasov/fix-14200-17.2
fix(simple-combo): trigger selectionChanging on input clear - 17.2
2 parents 92a6d2b + c3eed22 commit 257e9c1

File tree

3 files changed

+164
-3
lines changed

3 files changed

+164
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ All notable changes for each version of this project will be documented in this
2828
### General
2929
- `IgxSimpleCombo`
3030
- **Behavioral Change** When bound to `ngModel` and `formControlName` directives, the model would not be updated when the user types into the input and will only be updated on selection.
31+
- **Behavioral Change** The `selectionChanging` event will now trigger when typing the first character in the input if there is a previously selected value in the `IgxSimpleCombo`.
3132

3233

3334
## 17.1.0

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,136 @@ describe('IgxSimpleCombo', () => {
15561556

15571557
expect(spy).toHaveBeenCalledTimes(1);
15581558
}));
1559+
1560+
it('should emit selectionChanging event when input value changes', () => {
1561+
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1562+
fixture.detectChanges();
1563+
1564+
combo.select('Connecticut');
1565+
fixture.detectChanges();
1566+
1567+
expect(combo.selectionChanging.emit).toHaveBeenCalledTimes(1);
1568+
expect(combo.selectionChanging.emit).toHaveBeenCalledWith({
1569+
newValue: "Connecticut",
1570+
oldValue: undefined,
1571+
newSelection: {
1572+
field: "Connecticut",
1573+
region: "New England"
1574+
},
1575+
oldSelection: undefined,
1576+
displayText: 'Connecticut',
1577+
owner: combo,
1578+
cancel: false
1579+
});
1580+
1581+
combo.handleInputChange('z');
1582+
fixture.detectChanges();
1583+
1584+
expect(combo.selectionChanging.emit).toHaveBeenCalledTimes(2);
1585+
expect(combo.selectionChanging.emit).toHaveBeenCalledWith({
1586+
oldValue: "Connecticut",
1587+
newValue: undefined,
1588+
oldSelection: {
1589+
field: "Connecticut",
1590+
region: "New England"
1591+
},
1592+
newSelection: undefined,
1593+
owner: combo,
1594+
displayText: "z",
1595+
cancel: false
1596+
});
1597+
});
1598+
1599+
it('should not change selection when selectionChanging event is canceled', () => {
1600+
spyOn(combo.selectionChanging, 'emit').and.callThrough();
1601+
1602+
fixture.detectChanges();
1603+
1604+
combo.select('Connecticut');
1605+
fixture.detectChanges();
1606+
1607+
expect(combo.selection).toEqual({
1608+
field: 'Connecticut',
1609+
region: 'New England'
1610+
});
1611+
1612+
const cancelEventSpy = spyOn(combo.selectionChanging, 'emit').and.callFake((args: ISimpleComboSelectionChangingEventArgs) => {
1613+
args.cancel = true;
1614+
});
1615+
1616+
combo.handleInputChange('z');
1617+
fixture.detectChanges();
1618+
1619+
expect(cancelEventSpy).toHaveBeenCalled();
1620+
expect(combo.selection).toEqual({
1621+
field: 'Connecticut',
1622+
region: 'New England'
1623+
});
1624+
1625+
combo.handleInputChange(' ');
1626+
fixture.detectChanges();
1627+
1628+
expect(cancelEventSpy).toHaveBeenCalled();
1629+
expect(combo.selection).toEqual({
1630+
field: 'Connecticut',
1631+
region: 'New England'
1632+
});
1633+
});
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+
});
15591689
});
15601690

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

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
294294
this.filterValue = '';
295295
}
296296
if (super.selection.length) {
297-
this.selectionService.clear(this.id);
297+
const args: ISimpleComboSelectionChangingEventArgs = {
298+
newValue: undefined,
299+
oldValue: this.selectedItem,
300+
newSelection: undefined,
301+
oldSelection: this.selection,
302+
displayText: typeof event === 'string' ? event : event?.target?.value,
303+
owner: this,
304+
cancel: false
305+
};
306+
this.selectionChanging.emit(args);
307+
if (!args.cancel) {
308+
this.selectionService.select_items(this.id, [], true);
309+
}
298310
}
299311
// when filtering the focused item should be the first item or the currently selected item
300312
if (!this.dropdown.focusedItem || this.dropdown.focusedItem.id !== this.dropdown.items[0].id) {
@@ -390,13 +402,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
390402
if (this.disabled) {
391403
return;
392404
}
405+
406+
const oldSelection = this.selection;
393407
this.clearSelection(true);
408+
394409
if(!this.collapsed){
395410
this.focusSearchInput(true);
396411
}
397412
event.stopPropagation();
398413

399-
this.comboInput.value = this.filterValue = this.searchValue = '';
414+
if (this.selection !== oldSelection) {
415+
this.comboInput.value = this.filterValue = this.searchValue = '';
416+
}
417+
400418
this.dropdown.focusedItem = null;
401419
this.composing = false;
402420
this.comboInput.focus();
@@ -488,6 +506,15 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
488506
this._updateInput = true;
489507
} else if (this.isRemote) {
490508
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+
}
491518
}
492519
}
493520

@@ -560,7 +587,10 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
560587

561588
private clear(): void {
562589
this.clearSelection(true);
563-
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+
}
564594
}
565595

566596
private isValid(value: any): boolean {

0 commit comments

Comments
 (0)