Skip to content

Commit

Permalink
support bindValue with typeahead
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Mar 1, 2018
1 parent 54f55a0 commit a4768fb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
3 changes: 2 additions & 1 deletion demo/app/examples/reactive-forms.component.ts
Expand Up @@ -4,6 +4,7 @@ import { NgOption } from '@ng-select/ng-select';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DataService } from '../shared/data.service';
import { NgSelectComponent } from '../../../src/ng-select/ng-select.component';
import { delay } from 'rxjs/operators';

@Component({
selector: 'reactive-forms',
Expand Down Expand Up @@ -256,7 +257,7 @@ export class ReactiveFormsComponent {
}

private loadAlbums() {
this.dataService.getAlbums().subscribe(albums => {
this.dataService.getAlbums().pipe(delay(500)).subscribe(albums => {
this.allAlbums = albums;
this.albums = [...this.allAlbums];
this.selectFirstAlbum();
Expand Down
5 changes: 2 additions & 3 deletions demo/app/examples/search.component.ts
Expand Up @@ -44,6 +44,7 @@ import { DataService } from '../shared/data.service';
---html,true
<ng-select [items]="serverSideFilterItems"
bindLabel="name"
bindValue="name"
[addTag]="true"
[multiple]="true"
[typeahead]="peopleTypeahead"
Expand All @@ -64,9 +65,7 @@ export class SelectSearchComponent {

searchTerm = new EventEmitter<string>();
peopleTypeahead = new EventEmitter<string>();
selectedPersons = [{
name: 'Karyn Wright'
}];
selectedPersons = ['Karyn Wright', 'Other'];
selectedPerson: any;
selectedCustom: any;

Expand Down
46 changes: 44 additions & 2 deletions src/ng-select/ng-select.component.spec.ts
Expand Up @@ -446,6 +446,34 @@ describe('NgSelectComponent', function () {
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.selectedItems).toEqual([]);
}));

it('should select by bindValue when items are not loaded and typeahead is used', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectSelectedTypeaheadWithBindValueMultipleCmp,
`<ng-select [items]="cities"
bindLabel="name"
bindValue="id"
[multiple]="true"
[typeahead]="filter"
placeholder="select value"
[(ngModel)]="selectedCities">
</ng-select>`);

fixture.componentInstance.selectedCities = [1, 2];
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.selectedItems).toEqual([
jasmine.objectContaining({ label: 1, value: { id: 1, name: 1 } }),
jasmine.objectContaining({ label: 2, value: { id: 2, name: 2 } })
]);

fixture.componentInstance.cities = [
{ id: 1, name: 'Vilnius' },
{ id: 2, name: 'Kaunas' },
{ id: 3, name: 'Pabrade' },
];
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.selectedCities).toEqual([1, 2]);
}));
});

describe('multiple', () => {
Expand Down Expand Up @@ -879,7 +907,7 @@ describe('NgSelectComponent', function () {

fixture.componentInstance.dropdownPosition = 'top';
tickAndDetectChanges(fixture);

fixture.componentInstance.select.open();
tickAndDetectChanges(fixture);

Expand Down Expand Up @@ -1784,6 +1812,20 @@ class NgSelectSelectedSimpleMultipleCmp {
];
}

@Component({
template: ``
})
class NgSelectSelectedTypeaheadWithBindValueMultipleCmp {
@ViewChild(NgSelectComponent) select: NgSelectComponent;
selectedCities = [];
filter = new Subject<string>();
cities = [];

ngOnInit() {
this.filter.subscribe();
}
}

@Component({
template: ``
})
Expand Down Expand Up @@ -1915,5 +1957,5 @@ class NgSelectEventsTestCmp {

onClear() { }

onScrollToEnd() {}
onScrollToEnd() { }
}
7 changes: 6 additions & 1 deletion src/ng-select/ng-select.component.ts
Expand Up @@ -489,14 +489,19 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
}

const select = (val: any) => {
const item = this.itemsList.findItem(val);
let item = this.itemsList.findItem(val);
if (item) {
this.itemsList.select(item);
} else {
const isObject = val instanceof Object;
const simpleValue = !isObject && !this.bindValue;
if (isObject || simpleValue) {
this.itemsList.select(this.itemsList.mapItem(val, simpleValue, null));
} else if (!isObject && this.bindValue && this._isTypeahead) {
item = {};
item[this.bindValue] = val;
item[this.bindLabel] = val;
this.itemsList.select(this.itemsList.mapItem(item, false, null));
}
}
};
Expand Down

0 comments on commit a4768fb

Please sign in to comment.