Skip to content

Commit ddbabc8

Browse files
Merge pull request #14343 from IgniteUI/ganastasov/fix-14200-18.0
fix(simple-combo): trigger selectionChanging on input clear - 18.0
2 parents 3f203d1 + 9c3893e commit ddbabc8

File tree

3 files changed

+165
-3
lines changed

3 files changed

+165
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ All notable changes for each version of this project will be documented in this
7676
### General
7777
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`
7878
- The `contextMenu` event now fires when the end-user clicks to the right of the right-most cell in the grid in case the grid's columns don't span its full width. For this reason the event argument of the event is now of type `IGridContextMenuEventArgs` which contains the row object as well as the cell one. The latter will be `null` if the event didn't originate from a cell. **This is not a breaking change** as the new type extends the old.
79+
- `IgxSimpleCombo`
80+
- **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`.
7981

8082
## 17.1.0
8183
### New Features

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
@@ -292,7 +292,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
292292
this.filterValue = '';
293293
}
294294
if (super.selection.length) {
295-
this.selectionService.clear(this.id);
295+
const args: ISimpleComboSelectionChangingEventArgs = {
296+
newValue: undefined,
297+
oldValue: this.selectedItem,
298+
newSelection: undefined,
299+
oldSelection: this.selection,
300+
displayText: typeof event === 'string' ? event : event?.target?.value,
301+
owner: this,
302+
cancel: false
303+
};
304+
this.selectionChanging.emit(args);
305+
if (!args.cancel) {
306+
this.selectionService.select_items(this.id, [], true);
307+
}
296308
}
297309
// when filtering the focused item should be the first item or the currently selected item
298310
if (!this.dropdown.focusedItem || this.dropdown.focusedItem.id !== this.dropdown.items[0].id) {
@@ -388,13 +400,19 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
388400
if (this.disabled) {
389401
return;
390402
}
403+
404+
const oldSelection = this.selection;
391405
this.clearSelection(true);
406+
392407
if(!this.collapsed){
393408
this.focusSearchInput(true);
394409
}
395410
event.stopPropagation();
396411

397-
this.comboInput.value = this.filterValue = this.searchValue = '';
412+
if (this.selection !== oldSelection) {
413+
this.comboInput.value = this.filterValue = this.searchValue = '';
414+
}
415+
398416
this.dropdown.focusedItem = null;
399417
this.composing = false;
400418
this.comboInput.focus();
@@ -486,6 +504,15 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
486504
this._updateInput = true;
487505
} else if (this.isRemote) {
488506
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+
}
489516
}
490517
}
491518

@@ -558,7 +585,10 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
558585

559586
private clear(): void {
560587
this.clearSelection(true);
561-
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+
}
562592
}
563593

564594
private isValid(value: any): boolean {

0 commit comments

Comments
 (0)