Skip to content

Commit 9c8c47d

Browse files
authored
Merge branch 'master' into SKrastev/fix-6183-master
2 parents 22214d8 + 40d39cb commit 9c8c47d

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

projects/igniteui-angular/src/lib/drop-down/drop-down.component.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,51 @@ describe('IgxDropDown ', () => {
420420
expect(list.onSelection.emit).toHaveBeenCalledWith(selectionArgs1);
421421
}));
422422

423+
it('Should be able to change selection when manipulating ISelectionEventArgs', fakeAsync(() => {
424+
const fixture = TestBed.createComponent(IgxDropDownTestScrollComponent);
425+
fixture.detectChanges();
426+
const dropdown = fixture.componentInstance.dropdownScroll;
427+
expect(dropdown.selectedItem).toEqual(null);
428+
dropdown.toggle();
429+
tick();
430+
fixture.detectChanges();
431+
tick();
432+
433+
// Overwrite selection args
434+
let expectedSelected = dropdown.items[4];
435+
const calledSelected = dropdown.items[1];
436+
const subscription = dropdown.onSelection.subscribe((e: ISelectionEventArgs) => {
437+
expect(e.newSelection).toEqual(calledSelected);
438+
e.newSelection = expectedSelected;
439+
});
440+
dropdown.selectItem(calledSelected);
441+
tick();
442+
expect(dropdown.selectedItem).toEqual(expectedSelected);
443+
444+
// Clear selection
445+
expectedSelected = null;
446+
dropdown.selectItem(calledSelected);
447+
tick();
448+
expect(dropdown.selectedItem).toEqual(expectedSelected);
449+
450+
// Set header - error
451+
dropdown.toggle();
452+
tick();
453+
fixture.detectChanges();
454+
455+
expectedSelected = dropdown.items[4];
456+
dropdown.items[4].isHeader = true;
457+
458+
spyOn(dropdown, 'selectItem').and.callThrough();
459+
expect(() => { dropdown.selectItem(calledSelected); }).toThrow();
460+
461+
// Set non-IgxDropDownItemBaseDirective
462+
expectedSelected = 7 as any;
463+
expect(() => { dropdown.selectItem(calledSelected); }).toThrow();
464+
465+
subscription.unsubscribe();
466+
}));
467+
423468
it('Should persist selection through scrolling', fakeAsync(() => {
424469
const fixture = TestBed.createComponent(IgxDropDownTestScrollComponent);
425470
fixture.detectChanges();

projects/igniteui-angular/src/lib/drop-down/drop-down.component.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export class IgxDropDownComponent extends IgxDropDownBaseDirective implements ID
139139
public get focusedItem(): IgxDropDownItemBaseDirective {
140140
if (this.virtDir) {
141141
return this._focusedItem && this._focusedItem.index !== -1 ?
142-
(this.children.find(e => e.index === this._focusedItem.index) || null) :
143-
null;
142+
(this.children.find(e => e.index === this._focusedItem.index) || null) :
143+
null;
144144
}
145145
return this._focusedItem;
146146
}
@@ -542,19 +542,35 @@ export class IgxDropDownComponent extends IgxDropDownBaseDirective implements ID
542542
this.onSelection.emit(args);
543543

544544
if (!args.cancel) {
545-
this.selection.set(this.id, new Set([newSelection]));
546-
if (!this.virtDir) {
547-
if (oldSelection) {
548-
oldSelection.selected = false;
545+
if (this.isSelectionValid(args.newSelection)) {
546+
this.selection.set(this.id, new Set([args.newSelection]));
547+
if (!this.virtDir) {
548+
if (oldSelection) {
549+
oldSelection.selected = false;
550+
}
551+
if (args.newSelection) {
552+
args.newSelection.selected = true;
553+
}
549554
}
550-
if (newSelection) {
551-
newSelection.selected = true;
555+
if (event) {
556+
this.toggleDirective.close();
552557
}
553-
}
554-
if (event) {
555-
this.toggleDirective.close();
558+
} else {
559+
throw new Error('Please provide a valid drop-down item for the selection!');
556560
}
557561
}
558562
}
563+
564+
/**
565+
* Checks whether the selection is valid
566+
* `null` - the selection should be emptied
567+
* Virtual? - the selection should at least have and `index` and `value` property
568+
* Non-virtual? - the selection should be a valid drop-down item and **not** be a header
569+
*/
570+
protected isSelectionValid(selection: any): boolean {
571+
return selection === null
572+
|| (this.virtDir && selection.hasOwnProperty('value') && selection.hasOwnProperty('index'))
573+
|| (selection instanceof IgxDropDownItemComponent && !selection.isHeader);
574+
}
559575
}
560576

0 commit comments

Comments
 (0)