abstruse
Version:
Abstruse CI
59 lines (48 loc) • 1.43 kB
text/typescript
import { Component, Input, Output, OnChanges, SimpleChanges } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
export class AppSelectboxComponent implements OnChanges {
data: { key: string, value: string }[];
index: number;
opened: boolean;
tmp: string;
private onTouchedCallback: () => void = () => { };
private onChangeCallback: (_: any) => void = () => { };
get value(): string {
return this.data[this.index].key;
}
set value(val: string) {
let ind = this.data.findIndex(d => d.key === val);
if (ind !== -1) {
this.index = ind;
this.onChangeCallback(this.data[this.index].key);
}
}
ngOnChanges(changes: SimpleChanges) {
if (this.data && this.data.length) {
this.index = this.data.findIndex(d => d.key === this.tmp);
if (this.data[this.index]) {
this.value = this.data[this.index].key;
}
}
}
writeValue(val: string) {
if (val === null) {
return;
}
this.tmp = val;
this.index = this.data.findIndex(d => d.key === val);
}
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
}