Skip to content

Commit

Permalink
fix: remove default compareWith
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Mar 30, 2018
1 parent e9e6652 commit ec56c64
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/ng-select/items-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export class ItemsList {

findItem(value: any): NgOption {
if (this._ngSelect.bindValue) {
return this._items.find(item => !item.hasChildren &&
this._ngSelect.compareWith(this.resolveNested(item.value, this._ngSelect.bindValue), value));
return this._items.find(item => !item.hasChildren && this.resolveNested(item.value, this._ngSelect.bindValue) === value);
}
const index = this._items.findIndex(x => this._ngSelect.compareWith(x.value, value));
return index > -1 ?
this._items[index] :
this._items.find(item => !item.hasChildren && item.label && item.label === this.resolveNested(value, this._ngSelect.bindLabel));
const option = this._items.find(x => x.value === value);
const findBy = this._ngSelect.compareWith ?
(item: NgOption) => this._ngSelect.compareWith(item.value, value) :
(item: NgOption) => !item.hasChildren && item.label && item.label === this.resolveNested(value, this._ngSelect.bindLabel);

return option || this._items.find(item => findBy(item));
}

unselect(item: NgOption) {
Expand Down
6 changes: 4 additions & 2 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('NgSelectComponent', function () {
expect(vilnius.selected).toBeTruthy();
}));

it('should set items correctly after ngModel set first when typeahead and multiselect is used', fakeAsync(() => {
it('should set items correctly after ngModel set first when typeahead and multi-select is used', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectTestCmp,
`<ng-select [items]="cities"
Expand Down Expand Up @@ -2367,7 +2367,9 @@ class NgSelectTestCmp {
});
}

compareWith = (a, b) => a.name === b.name && a.district === b.district;
compareWith(a, b) {
return a.name === b.name && a.district === b.district
}

toggleVisible() {
this.visible = !this.visible;
Expand Down
2 changes: 1 addition & 1 deletion src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
private _defaultValue = 'value';
private _typeaheadLoading = false;
private _primitive: boolean;
private _compareWith: CompareWithFn;

private readonly _destroy$ = new Subject<void>();
private _compareWith = (a: any, b: any) => a === b;
private _onChange = (_: NgOption) => { };
private _onTouched = () => { };

Expand Down

0 comments on commit ec56c64

Please sign in to comment.