Skip to content

Commit

Permalink
feat: programmatically select active items
Browse files Browse the repository at this point in the history
Add `active` property which describes currently selected items.
Remove `initData` property since everything works on top of `active`.
  • Loading branch information
Namek committed May 31, 2016
1 parent b018f4b commit e44b1f0
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions components/select/select.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export class SelectComponent implements OnInit {
@Input() public placeholder:string = '';
@Input() public idField:string = 'id';
@Input() public textField:string = 'text';
@Input() public initData:Array<any> = [];
@Input() public multiple:boolean = false;

@Input()
Expand All @@ -209,23 +208,48 @@ export class SelectComponent implements OnInit {
}
}

@Input()
public set active(selectedItems:Array<any>) {
if (!selectedItems || selectedItems.length === 0) {
this._active = [];
}
else {
let areItemsStrings = typeof selectedItems[0] === 'string';

this._active = selectedItems.map((item:any) => {
let data = areItemsStrings
? item
: { id: item[this.idField], text: item[this.textField] };

return new SelectItem(data);
});
}

this.data.emit(this._active);
}


@Output() public data:EventEmitter<any> = new EventEmitter();
@Output() public selected:EventEmitter<any> = new EventEmitter();
@Output() public removed:EventEmitter<any> = new EventEmitter();
@Output() public typed:EventEmitter<any> = new EventEmitter();

public options:Array<SelectItem> = [];
public itemObjects:Array<SelectItem> = [];
public active:Array<SelectItem> = [];
public activeOption:SelectItem;
public element:ElementRef;

public get active():Array<any> {
return this._active;
}

private inputMode:boolean = false;
private optionsOpened:boolean = false;
private behavior:OptionsBehavior;
private inputValue:string = '';
private _items:Array<any> = [];
private _disabled:boolean = false;
private _active:Array<SelectItem> = [];

public constructor(element:ElementRef) {
this.element = element;
Expand Down Expand Up @@ -310,10 +334,6 @@ export class SelectComponent implements OnInit {
public ngOnInit():any {
this.behavior = (this.firstItemHasChildren) ?
new ChildrenBehavior(this) : new GenericBehavior(this);
if (this.initData) {
this.active = this.initData.map((data:any) => (typeof data === 'string' ? new SelectItem(data) : new SelectItem({id: data[this.idField], text: data[this.textField]})));
this.data.emit(this.active);
}
}

public remove(item:SelectItem):void {
Expand Down

0 comments on commit e44b1f0

Please sign in to comment.