ng2-ui-kit
Version:
Angular2 UI Kit
104 lines (84 loc) • 2.45 kB
text/typescript
import { Component, ViewContainerRef, forwardRef, AfterViewInit, Input, OnInit } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
export const SELECT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectComponent),
multi: true
};
export class SelectComponent implements OnInit {
label: string = '';
class: string = '';
opened: boolean = false;
placeholder: string;
options: any[] = [];
active: boolean;
private el: Element;
private internalValue: any;
private onTouchedCallback: () => void = () => { };
private onChangeCallback: (_: any) => void = () => { };
constructor(viewContainerRef: ViewContainerRef) {
this.el = viewContainerRef.element.nativeElement;
}
get value(): any {
return this.internalValue;
};
set value(value: any) {
if (value !== this.internalValue) {
let data = this.options.filter(o => o.value === value).pop();
this.internalValue = data.title;
this.onChangeCallback(value);
}
}
ngOnInit() {
this.class = `ui-kit-select-container ${this.class}`;
let body = document.querySelector('body');
body.addEventListener('click', e => {
if (!this.opened || !e.target) { return; };
if (this.el !== e.target && !this.el.contains((<any>e.target))) {
this.close();
}
}, false);
}
select(i: number) {
this.options = this.options.map((o, ii) => {
if (ii === i) {
o.selected = true;
this.value = o.value;
} else {
o.selected = false;
}
return o;
});
this.close();
}
toggle() {
this.opened = !this.opened;
}
close() { this.opened = false; }
open() { this.opened = true; }
writeValue(value: any) {
if (value !== this.internalValue) {
setTimeout(() => {
let data = this.options.filter(o => o.value === value).pop();
if (data) {
this.internalValue = data.title;
}
});
}
}
onBlur() {
this.onTouchedCallback();
}
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
}