Skip to content

Commit 3f203d1

Browse files
Merge pull request #14383 from IgniteUI/bpachilova/comboArrayValueKey-14103-18.0.x
fix(combo): Properly handle selection when value key is array - 18.0.x
2 parents 30e5735 + 19d1eb8 commit 3f203d1

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
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
@@ -28,7 +28,7 @@ import { caseSensitive } from '@igniteui/material-icons-extended';
2828
import { noop, Subject } from 'rxjs';
2929
import { takeUntil } from 'rxjs/operators';
3030
import { IgxSelectionAPIService } from '../core/selection';
31-
import { CancelableBrowserEventArgs, cloneArray, IBaseCancelableBrowserEventArgs, IBaseEventArgs, isNaNvalue, rem } from '../core/utils';
31+
import { CancelableBrowserEventArgs, cloneArray, IBaseCancelableBrowserEventArgs, IBaseEventArgs, rem } from '../core/utils';
3232
import { SortingDirection } from '../data-operations/sorting-strategy';
3333
import { IForOfState, IgxForOfDirective } from '../directives/for-of/for_of.directive';
3434
import { IgxIconService } from '../icon/icon.service';
@@ -46,6 +46,7 @@ import { ComboResourceStringsEN, IComboResourceStrings } from '../core/i18n/comb
4646
import { getCurrentResourceStrings } from '../core/i18n/resources';
4747
import { DOCUMENT } from '@angular/common';
4848
import { Size } from '../grids/common/enums';
49+
import { isEqual } from 'lodash-es';
4950

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

@@ -1288,9 +1289,7 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh
12881289
}
12891290

12901291
return keys.map(key => {
1291-
const item = isNaNvalue(key)
1292-
? this.data.find(entry => isNaNvalue(entry[this.valueKey]))
1293-
: this.data.find(entry => entry[this.valueKey] === key);
1292+
const item = this.data.find(entry => isEqual(entry[this.valueKey], key));
12941293

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

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,55 @@ describe('igxCombo', () => {
23432343
expect(combo.value).toEqual([]);
23442344
expect(combo.displayValue).toEqual('Selected Count: 0');
23452345
});
2346+
it('should handle selection for combo with array type value key correctly - issue #14103', () => {
2347+
fixture = TestBed.createComponent(ComboArrayTypeValueKeyComponent);
2348+
fixture.detectChanges();
2349+
combo = fixture.componentInstance.combo;
2350+
input = fixture.debugElement.query(By.css(`.${CSS_CLASS_COMBO_INPUTGROUP}`));
2351+
const items = fixture.componentInstance.items;
2352+
expect(combo).toBeDefined();
2353+
2354+
const selectionSpy = spyOn(combo.selectionChanging, 'emit');
2355+
let expectedResults: IComboSelectionChangingEventArgs = {
2356+
newValue: [combo.data[1][combo.valueKey]],
2357+
oldValue: [],
2358+
newSelection: [combo.data[1]],
2359+
oldSelection: [],
2360+
added: [combo.data[1]],
2361+
removed: [],
2362+
event: undefined,
2363+
owner: combo,
2364+
displayText: `${combo.data[1][combo.displayKey]}`,
2365+
cancel: false
2366+
};
2367+
2368+
let expectedDisplayText = items[1][combo.displayKey];
2369+
combo.select([fixture.componentInstance.items[1].value]);
2370+
fixture.detectChanges();
2371+
2372+
expect(selectionSpy).toHaveBeenCalledWith(expectedResults);
2373+
expect(input.nativeElement.value).toEqual(expectedDisplayText);
2374+
2375+
expectedDisplayText = `${items[1][combo.displayKey]}, ${items[2][combo.displayKey]}`;
2376+
expectedResults = {
2377+
newValue: [combo.data[1][combo.valueKey], combo.data[2][combo.valueKey]],
2378+
oldValue: [combo.data[1][combo.valueKey]],
2379+
newSelection: [combo.data[1], combo.data[2]],
2380+
oldSelection: [combo.data[1]],
2381+
added: [combo.data[2]],
2382+
removed: [],
2383+
event: undefined,
2384+
owner: combo,
2385+
displayText: expectedDisplayText,
2386+
cancel: false
2387+
};
2388+
2389+
combo.select([items[2].value]);
2390+
fixture.detectChanges();
2391+
2392+
expect(selectionSpy).toHaveBeenCalledWith(expectedResults);
2393+
expect(input.nativeElement.value).toEqual(expectedDisplayText);
2394+
});
23462395
});
23472396
describe('Grouping tests: ', () => {
23482397
beforeEach(() => {
@@ -3768,3 +3817,32 @@ export class IgxComboBindingDataAfterInitComponent implements AfterViewInit {
37683817
}, 1000);
37693818
}
37703819
}
3820+
3821+
@Component({
3822+
template: `
3823+
<igx-combo [data]="items" valueKey="value" displayKey="item"></igx-combo>`,
3824+
standalone: true,
3825+
imports: [IgxComboComponent]
3826+
})
3827+
export class ComboArrayTypeValueKeyComponent {
3828+
@ViewChild(IgxComboComponent, { read: IgxComboComponent, static: true })
3829+
public combo: IgxComboComponent;
3830+
public items: any[] = [];
3831+
3832+
constructor() {
3833+
this.items = [
3834+
{
3835+
item: "Item1",
3836+
value: [1, 2, 3]
3837+
},
3838+
{
3839+
item: "Item2",
3840+
value: [4, 5, 6]
3841+
},
3842+
{
3843+
item: "Item3",
3844+
value: [7, 8, 9]
3845+
}
3846+
];
3847+
}
3848+
}

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

Lines changed: 2 additions & 11 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.
@@ -624,8 +615,8 @@ export function* intoChunks<T>(arr: T[], size: number) {
624615
}
625616

626617
/**
627-
* @param size
628-
* @returns string that represents the --component-size default value
618+
* @param size
619+
* @returns string that represents the --component-size default value
629620
*/
630621
export function getComponentCssSizeVar(size: string) {
631622
switch (size) {

0 commit comments

Comments
 (0)