Skip to content

Commit

Permalink
fix: keep selected items while bindValue is incorrect
Browse files Browse the repository at this point in the history
closes #993
  • Loading branch information
varnastadeus committed Dec 20, 2018
1 parent 30f324c commit b7294a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng-select/items-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ItemsList {
const multiple = this._ngSelect.multiple;
for (const selected of this.selectedItems) {
const value = this._ngSelect.bindValue ? this.resolveNested(selected.value, this._ngSelect.bindValue) : selected.value;
const item = this.findItem(value);
const item = isDefined(value) ? this.findItem(value) : null;
this._selectionModel.unselect(selected, multiple);
this._selectionModel.select(item || selected, multiple, this._ngSelect.selectableGroupAsModel);
}
Expand Down
24 changes: 24 additions & 0 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ describe('NgSelectComponent', function () {
expect(select.itemsList.filteredItems[0].selected).toBeTruthy();
}));

it('should keep selected item while setting new items and bindValue is incorrect', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
bindLabel="name"
bindValue="value"
[clearable]="true"
[(ngModel)]="selectedCityId">
</ng-select>`);

tickAndDetectChanges(fixture); // triggers write value

select = fixture.componentInstance.select;
select.select(select.itemsList.items[1]);
tickAndDetectChanges(fixture);

fixture.componentInstance.cities = [...fixture.componentInstance.cities];
tickAndDetectChanges(fixture);

expect(select.selectedItems[0]).toEqual(jasmine.objectContaining({
value: { id: 2, name: 'Kaunas' }
}));
}));

it('should clear previous single select value when setting new model', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
Expand Down

0 comments on commit b7294a3

Please sign in to comment.