Skip to content

Commit 92a6d2b

Browse files
Merge pull request #14387 from IgniteUI/bpachilova/comboArrayValueKey-14103-17.2.x
fix(combo): Properly handle selection when value key is array - 17.2.x
2 parents bec9363 + e5472f4 commit 92a6d2b

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

projects/igniteui-angular/src/lib/combo/combo.common.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { noop, Subject } from 'rxjs';
3030
import { takeUntil } from 'rxjs/operators';
3131
import { DisplayDensityBase, DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
3232
import { IgxSelectionAPIService } from '../core/selection';
33-
import { CancelableBrowserEventArgs, cloneArray, IBaseCancelableBrowserEventArgs, IBaseEventArgs, isNaNvalue, rem } from '../core/utils';
33+
import { CancelableBrowserEventArgs, cloneArray, IBaseCancelableBrowserEventArgs, IBaseEventArgs, rem } from '../core/utils';
3434
import { SortingDirection } from '../data-operations/sorting-strategy';
3535
import { IForOfState, IgxForOfDirective } from '../directives/for-of/for_of.directive';
3636
import { IgxIconService } from '../icon/icon.service';
@@ -46,6 +46,7 @@ import {
4646
import { IComboItemAdditionEvent, IComboSearchInputEventArgs } from './public_api';
4747
import { ComboResourceStringsEN, IComboResourceStrings } from '../core/i18n/combo-resources';
4848
import { getCurrentResourceStrings } from '../core/i18n/resources';
49+
import { isEqual } from 'lodash-es';
4950

5051
export const IGX_COMBO_COMPONENT = /*@__PURE__*/new InjectionToken<IgxComboBase>('IgxComboComponentToken');
5152

@@ -1282,9 +1283,7 @@ export abstract class IgxComboBaseDirective extends DisplayDensityBase implement
12821283
}
12831284

12841285
return keys.map(key => {
1285-
const item = isNaNvalue(key)
1286-
? this.data.find(entry => isNaNvalue(entry[this.valueKey]))
1287-
: this.data.find(entry => entry[this.valueKey] === key);
1286+
const item = this.data.find(entry => isEqual(entry[this.valueKey], key));
12881287

12891288
return item !== undefined ? item : { [this.valueKey]: key };
12901289
});

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,55 @@ describe('igxCombo', () => {
23422342
expect(combo.value).toEqual([]);
23432343
expect(combo.displayValue).toEqual('Selected Count: 0');
23442344
});
2345+
it('should handle selection for combo with array type value key correctly - issue #14103', () => {
2346+
fixture = TestBed.createComponent(ComboArrayTypeValueKeyComponent);
2347+
fixture.detectChanges();
2348+
combo = fixture.componentInstance.combo;
2349+
input = fixture.debugElement.query(By.css(`.${CSS_CLASS_COMBO_INPUTGROUP}`));
2350+
const items = fixture.componentInstance.items;
2351+
expect(combo).toBeDefined();
2352+
2353+
const selectionSpy = spyOn(combo.selectionChanging, 'emit');
2354+
let expectedResults: IComboSelectionChangingEventArgs = {
2355+
newValue: [combo.data[1][combo.valueKey]],
2356+
oldValue: [],
2357+
newSelection: [combo.data[1]],
2358+
oldSelection: [],
2359+
added: [combo.data[1]],
2360+
removed: [],
2361+
event: undefined,
2362+
owner: combo,
2363+
displayText: `${combo.data[1][combo.displayKey]}`,
2364+
cancel: false
2365+
};
2366+
2367+
let expectedDisplayText = items[1][combo.displayKey];
2368+
combo.select([fixture.componentInstance.items[1].value]);
2369+
fixture.detectChanges();
2370+
2371+
expect(selectionSpy).toHaveBeenCalledWith(expectedResults);
2372+
expect(input.nativeElement.value).toEqual(expectedDisplayText);
2373+
2374+
expectedDisplayText = `${items[1][combo.displayKey]}, ${items[2][combo.displayKey]}`;
2375+
expectedResults = {
2376+
newValue: [combo.data[1][combo.valueKey], combo.data[2][combo.valueKey]],
2377+
oldValue: [combo.data[1][combo.valueKey]],
2378+
newSelection: [combo.data[1], combo.data[2]],
2379+
oldSelection: [combo.data[1]],
2380+
added: [combo.data[2]],
2381+
removed: [],
2382+
event: undefined,
2383+
owner: combo,
2384+
displayText: expectedDisplayText,
2385+
cancel: false
2386+
};
2387+
2388+
combo.select([items[2].value]);
2389+
fixture.detectChanges();
2390+
2391+
expect(selectionSpy).toHaveBeenCalledWith(expectedResults);
2392+
expect(input.nativeElement.value).toEqual(expectedDisplayText);
2393+
});
23452394
});
23462395
describe('Grouping tests: ', () => {
23472396
beforeEach(() => {
@@ -3797,3 +3846,32 @@ export class IgxComboBindingDataAfterInitComponent implements AfterViewInit {
37973846
}, 1000);
37983847
}
37993848
}
3849+
3850+
@Component({
3851+
template: `
3852+
<igx-combo [data]="items" valueKey="value" displayKey="item"></igx-combo>`,
3853+
standalone: true,
3854+
imports: [IgxComboComponent]
3855+
})
3856+
export class ComboArrayTypeValueKeyComponent {
3857+
@ViewChild(IgxComboComponent, { read: IgxComboComponent, static: true })
3858+
public combo: IgxComboComponent;
3859+
public items: any[] = [];
3860+
3861+
constructor() {
3862+
this.items = [
3863+
{
3864+
item: "Item1",
3865+
value: [1, 2, 3]
3866+
},
3867+
{
3868+
item: "Item2",
3869+
value: [4, 5, 6]
3870+
},
3871+
{
3872+
item: "Item3",
3873+
value: [7, 8, 9]
3874+
}
3875+
];
3876+
}
3877+
}

projects/igniteui-angular/src/lib/core/utils.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,6 @@ export const isEqual = (obj1, obj2): boolean => {
226226
return obj1 === obj2;
227227
};
228228

229-
/**
230-
* Checks if provided variable is the value NaN
231-
*
232-
* @param value Value to check
233-
* @returns true if provided variable is NaN
234-
* @hidden
235-
*/
236-
export const isNaNvalue = (value: any): boolean => isNaN(value) && value !== undefined && typeof value !== 'string';
237-
238229
/**
239230
* Utility service taking care of various utility functions such as
240231
* detecting browser features, general cross browser DOM manipulation, etc.

0 commit comments

Comments
 (0)