Skip to content

Commit 7202af6

Browse files
committed
feat(combo): add display text overwrite to selection change event, #6342
1 parent 15013f6 commit 7202af6

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

Diff for: projects/igniteui-angular/src/lib/combo/combo.component.spec.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ describe('igxCombo', () => {
10181018
newSelection: [targetItem.itemID],
10191019
added: [targetItem.itemID],
10201020
removed: [],
1021+
textValue: `${targetItem.value[combo.displayKey]}`,
10211022
event: undefined,
10221023
cancel: false
10231024
});
@@ -1032,6 +1033,7 @@ describe('igxCombo', () => {
10321033
newSelection: [],
10331034
added: [],
10341035
removed: [targetItem.itemID],
1036+
textValue: '',
10351037
event: undefined,
10361038
cancel: false
10371039
});
@@ -1056,6 +1058,7 @@ describe('igxCombo', () => {
10561058
newSelection: newSelection,
10571059
added: [combo.data[1], combo.data[5], combo.data[6]],
10581060
removed: [],
1061+
textValue: `${newSelection.map(entry => entry[combo.displayKey])}`,
10591062
event: undefined,
10601063
cancel: false
10611064
});
@@ -1072,6 +1075,7 @@ describe('igxCombo', () => {
10721075
newSelection: newSelection,
10731076
removed: [],
10741077
added: [combo.data[3]],
1078+
textValue: `${newSelection.map(entry => entry[combo.displayKey])}`,
10751079
event: undefined,
10761080
cancel: false
10771081
});
@@ -1087,6 +1091,7 @@ describe('igxCombo', () => {
10871091
newSelection: newSelection,
10881092
removed: oldSelection,
10891093
added: [combo.data[0]],
1094+
textValue: `${newSelection.map(entry => entry[combo.displayKey])}`,
10901095
event: undefined,
10911096
cancel: false
10921097
});
@@ -1103,6 +1108,7 @@ describe('igxCombo', () => {
11031108
oldSelection: oldSelection,
11041109
newSelection: newSelection,
11051110
removed: [combo.data[0]],
1111+
textValue: `${newSelection.map(entry => entry[combo.displayKey])}`,
11061112
added: [],
11071113
event: undefined,
11081114
cancel: false
@@ -1420,11 +1426,12 @@ describe('igxCombo', () => {
14201426
const dropdown = combo.dropdown;
14211427
let timesFired = 1;
14221428
const mockEvent = new MouseEvent('click');
1423-
const eventParams = {
1429+
const eventParams: IComboSelectionChangeEventArgs = {
14241430
oldSelection: [],
14251431
newSelection: [],
14261432
added: [],
14271433
removed: [],
1434+
textValue: '',
14281435
event: mockEvent,
14291436
cancel: false
14301437
};
@@ -1591,12 +1598,13 @@ describe('igxCombo', () => {
15911598
fixture.detectChanges();
15921599
const combo = fixture.componentInstance.combo;
15931600
const selectionSpy = spyOn(fixture.componentInstance, 'onSelectionChange');
1594-
const expectedResults = {
1601+
const expectedResults: IComboSelectionChangeEventArgs = {
15951602
newSelection: [combo.data[0]],
15961603
oldSelection: [],
15971604
added: [combo.data[0]],
15981605
removed: [],
15991606
event: undefined,
1607+
textValue: `${combo.data[0][combo.displayKey]}`,
16001608
cancel: false
16011609
};
16021610
combo.selectItems([combo.data[0]]);
@@ -1605,6 +1613,7 @@ describe('igxCombo', () => {
16051613
newSelection: [],
16061614
oldSelection: [combo.data[0]],
16071615
added: [],
1616+
textValue: '',
16081617
removed: [combo.data[0]]
16091618
});
16101619
combo.deselectItems([combo.data[0]]);
@@ -1616,12 +1625,13 @@ describe('igxCombo', () => {
16161625
fixture.detectChanges();
16171626
const combo = fixture.componentInstance.combo;
16181627
const selectionSpy = spyOn(fixture.componentInstance, 'onSelectionChange');
1619-
const expectedResults = {
1628+
const expectedResults: IComboSelectionChangeEventArgs = {
16201629
newSelection: [combo.data[0], combo.data[1], combo.data[2]],
16211630
oldSelection: [],
16221631
added: [combo.data[0], combo.data[1], combo.data[2]],
16231632
removed: [],
16241633
event: undefined,
1634+
textValue: `${[combo.data[0], combo.data[1], combo.data[2]].map(entry => entry[combo.displayKey]).join(', ')}`,
16251635
cancel: false
16261636
};
16271637
combo.selectItems([combo.data[0], combo.data[1], combo.data[2]]);
@@ -1631,13 +1641,15 @@ describe('igxCombo', () => {
16311641
newSelection: [combo.data[1], combo.data[2]],
16321642
oldSelection: [combo.data[0], combo.data[1], combo.data[2]],
16331643
added: [],
1644+
textValue: `${[combo.data[1], combo.data[2]].map(entry => entry[combo.displayKey]).join(', ')}`,
16341645
removed: [combo.data[0]]
16351646
});
16361647
expect(selectionSpy).toHaveBeenCalledWith(expectedResults);
16371648
Object.assign(expectedResults, {
16381649
newSelection: [combo.data[4], combo.data[5], combo.data[6]],
16391650
oldSelection: [combo.data[1], combo.data[2]],
16401651
added: [combo.data[4], combo.data[5], combo.data[6]],
1652+
textValue: `${[combo.data[4], combo.data[5], combo.data[6]].map(entry => entry[combo.displayKey]).join(', ')}`,
16411653
removed: [combo.data[1], combo.data[2]]
16421654
});
16431655
combo.selectItems([combo.data[4], combo.data[5], combo.data[6]], true);

Diff for: projects/igniteui-angular/src/lib/combo/combo.component.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export interface IComboSelectionChangeEventArgs extends CancelableEventArgs, IBa
9191
added: any[];
9292
/** An array containing the values that will be removed from the selection (if any) */
9393
removed: any[];
94+
/** The text displayed in the combo text box */
95+
textValue: string;
9496
/** The user interaction that triggered the selection change */
9597
event?: Event;
9698
}
@@ -1382,12 +1384,15 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
13821384
protected setSelection(newSelection: Set<any>, event?: Event): void {
13831385
const removed = diffInSets(this.selection.get(this.id), newSelection);
13841386
const added = diffInSets(newSelection, this.selection.get(this.id));
1387+
const newSelectionAsArray = Array.from(newSelection);
1388+
const text = this.concatTextValue(newSelectionAsArray);
13851389
const args: IComboSelectionChangeEventArgs = {
1386-
newSelection: Array.from(newSelection),
1390+
newSelection: newSelectionAsArray,
13871391
oldSelection: Array.from(this.selection.get(this.id) || []),
13881392
added,
13891393
removed,
13901394
event,
1395+
textValue: text,
13911396
cancel: false
13921397
};
13931398
this.onSelectionChange.emit(args);
@@ -1415,6 +1420,17 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
14151420
}
14161421
}
14171422

1423+
/** Returns a string that should be populated in the combo's text box
1424+
* If the combo is remote, returns an empty string (as the items may not be loaded yet)
1425+
*/
1426+
private concatTextValue(selection: any[]): string {
1427+
if (this.isRemote) { return ''; }
1428+
const value = this.displayKey !== null && this.displayKey !== undefined ?
1429+
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1430+
selection.join(', ');
1431+
return value;
1432+
}
1433+
14181434
/** if there is a valueKey - map the keys to data items, else - just return the keys */
14191435
private convertKeysToItems(keys: any[]) {
14201436
if (this.comboAPI.valueKey === null) {

0 commit comments

Comments
 (0)