feat(value-accessors): move the value accessors into @ionic/angular

This commit is contained in:
Ken Sodemann
2017-12-05 15:03:18 -06:00
committed by GitHub
parent a2b88c5cb8
commit 5641424db3
9 changed files with 85 additions and 81 deletions

View File

@ -0,0 +1,41 @@
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
// NOTE: May need to look at this to see if we need anything else:
// https://github.com/angular/angular/blob/5.0.2/packages/forms/src/directives/select_control_value_accessor.ts#L28-L158
@Directive({
/* tslint:disable-next-line:directive-selector */
selector: 'ion-select, ion-radio-group, ion-segment, ion-datetime',
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IonSelectValueAccessor, multi: true }]
})
export class IonSelectValueAccessor implements ControlValueAccessor {
constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {};
this.onTouched = () => {};
}
onChange: (value: any) => void;
onTouched: () => void;
writeValue(value: any) {
this.renderer.setProperty(this.element.nativeElement, 'value', value);
}
@HostListener('ionChange', ['$event.target.value'])
_handleChangeEvent(value: any) {
this.onChange(value);
}
@HostListener('ionBlur')
_handleBlurEvent() {
this.onTouched();
}
registerOnChange(fn: (value: any) => void) {
this.onChange = fn;
}
registerOnTouched(fn: () => void) {
this.onTouched = fn;
}
}