Skip to content

Commit

Permalink
fix: update change event to fire object (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Mar 8, 2018
1 parent 5ca9989 commit 1898ca4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 13 additions & 6 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,18 +1651,23 @@ describe('NgSelectComponent', function () {
const fixture = createTestingModule(
NgSelectEventsTestCmp,
`<ng-select [items]="cities"
(change)="onChange()"
[(ngModel)]="selectedCity">
bindValue="id"
bindLabel="name"
(change)="onChange($event)"
[(ngModel)]="selectedCityId">
</ng-select>`);

spyOn(fixture.componentInstance, 'onChange');

fixture.componentInstance.selectedCity = fixture.componentInstance.cities[0];
fixture.componentInstance.selectedCityId = fixture.componentInstance.cities[1].id;
tickAndDetectChanges(fixture);

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

fixture.componentInstance.select.select(fixture.componentInstance.cities[1]);

expect(fixture.componentInstance.onChange).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.onChange).toHaveBeenCalledWith(select.selectedItems[0].value);
expect(fixture.componentInstance.selectedCityId).toBe(0);
}));

it('do not fire change when item not changed', fakeAsync(() => {
Expand Down Expand Up @@ -2193,8 +2198,10 @@ class NgSelectFilterTestCmp {
class NgSelectEventsTestCmp {
@ViewChild(NgSelectComponent) select: NgSelectComponent;
selectedCity: { id: number; name: string };
selectedCityId: number
selectedCities: Array<{ id: number; name: string }>;
cities = [
{ id: 0, name: 'All' },
{ id: 1, name: 'Vilnius' },
{ id: 2, name: 'Kaunas' },
{ id: 3, name: 'Pabrade' },
Expand Down
18 changes: 9 additions & 9 deletions src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
}

private _updateNgModel() {
const values = [];
const model = [];
for (const item of this.selectedItems) {
if (this.bindValue) {
let resolvedValue = null;
Expand All @@ -544,20 +544,20 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
} else {
resolvedValue = this.itemsList.resolveNested(item.value, this.bindValue);
}
values.push(resolvedValue);
model.push(resolvedValue);
} else {
values.push(item.value);
model.push(item.value);
}
}

let ngModel = null;
if (this.multiple) {
ngModel = values;
} else if (isDefined(values[0])) {
ngModel = values[0];
this._onChange(model);
this.changeEvent.emit(this.selectedItems.map(x => x.value));
} else {
this._onChange(isDefined(model[0]) ? model[0] : null);
this.changeEvent.emit(this.selectedItems[0] && this.selectedItems[0].value);
}
this._onChange(ngModel);
this.changeEvent.emit(ngModel);

this._cd.markForCheck();
}

Expand Down

0 comments on commit 1898ca4

Please sign in to comment.