From 1ddbcd9902c3f9fefb7e1b2560b11b5bd904095a Mon Sep 17 00:00:00 2001 From: Namek Date: Mon, 27 Jun 2016 18:20:32 +0200 Subject: [PATCH] feat: programmatically select active items (#223) * feat: programmatically select active items Add `active` property which describes currently selected items. Remove `initData` property since everything works on top of `active`. * docs(README): change info about properties: `initData` -> `active` --- README.md | 2 +- components/select/select.ts | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 components/select/select.ts diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 54be6bce..04b4ac11 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Follow me [![twitter](https://img.shields.io/twitter/follow/valorkin.svg?style=s Items may be nested by adding a `children` property to any item, whose value should be another array of items. Items that have children may omit having an ID. If `items` are specified, all items are expected to be available locally and all selection operations operate on this local array only. If omitted, items are not available locally, and the `query` option should be provided to fetch data. - - `initData` (`?Array`) - Initial selection data to set. This should be an object with `id` and `text` properties in the case of input type 'Single', + - `active` (`?Array`) - selection data to set. This should be an object with `id` and `text` properties in the case of input type 'Single', or an array of such objects otherwise. This option is mutually exclusive with value. - `allowClear` (`?boolean=false`) (*not yet supported*) - Set to `true` to allow the selection to be cleared. This option only applies to single-value inputs. - `placeholder` (`?string=''`) - Placeholder text to display when the element has no focus and selected items. diff --git a/components/select/select.ts b/components/select/select.ts old mode 100644 new mode 100755 index e79a08f1..6191221e --- a/components/select/select.ts +++ b/components/select/select.ts @@ -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 = []; @Input() public multiple:boolean = false; @Input() @@ -217,6 +216,23 @@ export class SelectComponent implements OnInit { } } + @Input() + public set active(selectedItems:Array) { + 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); + }); + } + } + @Output() public data:EventEmitter = new EventEmitter(); @Output() public selected:EventEmitter = new EventEmitter(); @Output() public removed:EventEmitter = new EventEmitter(); @@ -224,16 +240,20 @@ export class SelectComponent implements OnInit { public options:Array = []; public itemObjects:Array = []; - public active:Array = []; public activeOption:SelectItem; public element:ElementRef; + public get active():Array { + return this._active; + } + private inputMode:boolean = false; private optionsOpened:boolean = false; private behavior:OptionsBehavior; private inputValue:string = ''; private _items:Array = []; private _disabled:boolean = false; + private _active:Array = []; public constructor(element:ElementRef) { this.element = element; @@ -319,10 +339,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 {