Skip to content

Commit

Permalink
feat: support for simple arrays (ng-select#129)
Browse files Browse the repository at this point in the history
* feat: support for simple arrays
closes ng-select#100
  • Loading branch information
varnastadeus authored and anjmao committed Nov 20, 2017
1 parent b37bfee commit ec860cb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
28 changes: 14 additions & 14 deletions ng-select/items-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export class ItemsList {
private _markedIndex = -1;
private _selected: NgOption[] = [];
private _multiple = false;
private _bindLabel: string;
private _bindValue: string;
private _simple = false;

get value(): NgOption[] {
return this._selected;
Expand All @@ -20,7 +19,8 @@ export class ItemsList {
return this.filteredItems[this._markedIndex];
}

setItems(items: NgOption[]) {
setItems(items: NgOption[], simple: boolean = false) {
this._simple = simple;
this.items = this.mapItems(items);
this.filteredItems = [...this.items];
}
Expand All @@ -30,12 +30,6 @@ export class ItemsList {
this.clearSelected();
}

setBindOptions(bindLabel: string, bindValue: string) {
this._bindLabel = bindLabel;
this._bindValue = bindValue;
}


select(item: NgOption) {
if (!this._multiple) {
this.clearSelected();
Expand All @@ -44,16 +38,16 @@ export class ItemsList {
item.selected = true;
}

findItem(value): NgOption {
findItem(value, bindValue: string, bindLabel: string): NgOption {
if (!value) {
return null;
}
if (this._bindValue) {
return this.items.find(x => x[this._bindValue] === value);
if (bindValue) {
return this.items.find(x => x[bindValue] === value);
}
const index = this.items.indexOf(value);
return index > -1 ? this.items[index] :
this.items.find(x => x[this._bindLabel] === value[this._bindLabel])
this.items.find(x => x[bindLabel] === value[bindLabel])
}

unselect(item: NgOption) {
Expand Down Expand Up @@ -148,9 +142,15 @@ export class ItemsList {

private mapItems(items: NgOption[]) {
return items.map((item, index) => {
let option = item;
if (this._simple) {
option = {};
option['label'] = item as any;
}

return {
index: index,
...item
...option
};
})
}
Expand Down
24 changes: 24 additions & 0 deletions ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ describe('NgSelectComponent', function () {
discardPeriodicTasks();
}));

it('bind to simple array', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectSimpleCmp,
`<ng-select [items]="cities"
[(ngModel)]="selectedCity">
</ng-select>`);

selectOption(fixture, KeyCode.ArrowDown, 0);
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.selectedCity).toBe('Vilnius');
fixture.componentInstance.selectedCity = 'Kaunas';
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.selectedItems).toEqual([jasmine.objectContaining({ label: 'Kaunas' })]);
}));

it('bind to object', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectBasicTestCmp,
Expand Down Expand Up @@ -994,6 +1009,15 @@ class NgSelectSelectedSimpleCmp {
];
}

@Component({
template: ``
})
class NgSelectSimpleCmp {
@ViewChild(NgSelectComponent) select: NgSelectComponent;
cities = ['Vilnius', 'Kaunas', 'Pabrade'];
selectedCity;
}

@Component({
template: ``
})
Expand Down
39 changes: 22 additions & 17 deletions ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ElementRef,
ChangeDetectionStrategy,
Optional,
SimpleChanges,
Renderer2, ContentChildren, QueryList
} from '@angular/core';

Expand Down Expand Up @@ -57,7 +58,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie

// inputs
@Input() items = [];
@Input() bindLabel = 'label';
@Input() bindLabel: string;
@Input() bindValue: string;
@Input() clearable = true;
@Input() disableVirtualScroll = false;
Expand Down Expand Up @@ -102,6 +103,9 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
filterValue: string = null;

private _ngModel = null;
private _simple = false;
private _defaultLabel = 'label';
private _defaultValue = 'value';

private onChange = (_: NgOption) => { };
private onTouched = () => { };
Expand All @@ -121,8 +125,21 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
this.mergeGlobalConfig(config);
}

ngOnChanges(changes: SimpleChanges) {
if (changes.multiple) {
this.itemsList.setMultiple(changes.multiple.currentValue);
}
if (changes.items) {
this.setItems(changes.items.currentValue || []);
}
}

ngOnInit() {
this.handleDocumentClick();
this.bindLabel = this.bindLabel || this._defaultLabel;
if (this._simple) {
this.bindValue = this._defaultLabel;
}
}

ngAfterViewInit() {
Expand All @@ -131,18 +148,6 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
}
}

ngOnChanges(changes) {
if (changes.bindLabel || changes.bindValue) {
this.itemsList.setBindOptions(this.bindLabel, this.bindValue);
}
if (changes.multiple) {
this.itemsList.setMultiple(changes.multiple.currentValue);
}
if (changes.items) {
this.setItems(changes.items.currentValue || []);
}
}

ngOnDestroy() {
this.changeDetectorRef.detach();
this.disposeDocumentClickListener();
Expand Down Expand Up @@ -277,7 +282,6 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie

selectTag() {
let tag = {}

if (this.addTag instanceof Function) {
tag = this.addTag(this.filterValue);
} else {
Expand Down Expand Up @@ -351,7 +355,9 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
}

private setItems(items: NgOption[]) {
this.itemsList.setItems(items);
const firstItem = items[0];
this._simple = firstItem && !(firstItem instanceof Object);
this.itemsList.setItems(items, this._simple);
if (this._ngModel) {
this.itemsList.clearSelected();
this.selectWriteValue(this._ngModel);
Expand All @@ -365,7 +371,6 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
private setItemsFromNgOptions() {
if (!this.bindValue) {
this.bindValue = 'value';
this.itemsList.setBindOptions(this.bindLabel, this.bindValue);
}

const handleNgOptions = (options) => {
Expand Down Expand Up @@ -439,7 +444,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
}

const select = (val: any) => {
let item = this.itemsList.findItem(val);
let item = this.itemsList.findItem(val, this.bindValue, this.bindLabel);
if (item) {
this.itemsList.select(item);
} else {
Expand Down

0 comments on commit ec860cb

Please sign in to comment.