feat(value-accessors): set ionic classes

This commit is contained in:
Ken Sodemann
2018-02-16 17:12:12 -06:00
parent 0fd0d1f654
commit cc59a4e1fb
6 changed files with 79 additions and 26 deletions

View File

@ -1,8 +1,7 @@
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
import { import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
ControlValueAccessor,
NG_VALUE_ACCESSOR import { setIonicClasses } from './util/set-ionic-classes';
} from '@angular/forms';
@Directive({ @Directive({
/* tslint:disable-next-line:directive-selector */ /* tslint:disable-next-line:directive-selector */
@ -17,8 +16,8 @@ import {
}) })
export class BooleanValueAccessor implements ControlValueAccessor { export class BooleanValueAccessor implements ControlValueAccessor {
constructor(private element: ElementRef, private renderer: Renderer2) { constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {}; this.onChange = () => { };
this.onTouched = () => {}; this.onTouched = () => { };
} }
onChange: (value: any) => void; onChange: (value: any) => void;
@ -26,16 +25,23 @@ export class BooleanValueAccessor implements ControlValueAccessor {
writeValue(value: any) { writeValue(value: any) {
this.renderer.setProperty(this.element.nativeElement, 'checked', value); this.renderer.setProperty(this.element.nativeElement, 'checked', value);
setIonicClasses(this.element);
} }
@HostListener('ionChange', ['$event.target.checked']) @HostListener('ionChange', ['$event.target.checked'])
_handleIonChange(value: any) { _handleIonChange(value: any) {
this.onChange(value); this.onChange(value);
setTimeout(() => {
setIonicClasses(this.element);
});
} }
@HostListener('ionBlur') @HostListener('ionBlur')
_handleBlurEvent() { _handleBlurEvent() {
this.onTouched(); this.onTouched();
setTimeout(() => {
setIonicClasses(this.element);
});
} }
registerOnChange(fn: (value: any) => void): void { registerOnChange(fn: (value: any) => void): void {

View File

@ -1,6 +1,8 @@
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { setIonicClasses } from './util/set-ionic-classes';
@Directive({ @Directive({
/* tslint:disable-next-line:directive-selector */ /* tslint:disable-next-line:directive-selector */
selector: 'ion-input[type=number]', selector: 'ion-input[type=number]',
@ -14,8 +16,8 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
}) })
export class NumericValueAccessor implements ControlValueAccessor { export class NumericValueAccessor implements ControlValueAccessor {
constructor(private element: ElementRef, private renderer: Renderer2) { constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {}; this.onChange = () => { };
this.onTouched = () => {}; this.onTouched = () => { };
} }
onChange: (value: any) => void; onChange: (value: any) => void;
@ -30,21 +32,28 @@ export class NumericValueAccessor implements ControlValueAccessor {
'value', 'value',
normalizedValue normalizedValue
); );
setIonicClasses(this.element);
} }
@HostListener('input', ['$event.target.value']) @HostListener('input', ['$event.target.value'])
_handleInputEvent(value: any): void { _handleInputEvent(value: any): void {
this.onChange(value); this.onChange(value);
setTimeout(() => {
setIonicClasses(this.element);
});
} }
@HostListener('ionBlur') @HostListener('ionBlur')
_handleBlurEvent(): void { _handleBlurEvent(): void {
this.onTouched(); this.onTouched();
setTimeout(() => {
setIonicClasses(this.element);
});
} }
registerOnChange(fn: (_: number | null) => void): void { registerOnChange(fn: (_: number | null) => void): void {
this.onChange = value => { this.onChange = value => {
fn(value == '' ? null : parseFloat(value)); fn(value === '' ? null : parseFloat(value));
}; };
} }

View File

@ -1,14 +1,7 @@
import { import { Directive, ElementRef, HostListener, Input, Renderer2 } from '@angular/core';
Directive, import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
ElementRef,
HostListener, import { setIonicClasses } from './util/set-ionic-classes';
Input,
Renderer2
} from '@angular/core';
import {
ControlValueAccessor,
NG_VALUE_ACCESSOR
} from '@angular/forms';
@Directive({ @Directive({
/* tslint:disable-next-line:directive-selector */ /* tslint:disable-next-line:directive-selector */
@ -28,8 +21,8 @@ export class RadioValueAccessor implements ControlValueAccessor {
onTouched: () => void; onTouched: () => void;
constructor(private element: ElementRef, private renderer: Renderer2) { constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {}; this.onChange = () => { };
this.onTouched = () => {}; this.onTouched = () => { };
} }
writeValue(value: any) { writeValue(value: any) {
@ -38,16 +31,23 @@ export class RadioValueAccessor implements ControlValueAccessor {
'checked', 'checked',
value === this.value value === this.value
); );
setIonicClasses(this.element);
} }
@HostListener('ionSelect', ['$event.target.checked']) @HostListener('ionSelect', ['$event.target.checked'])
_handleIonSelect(value: any) { _handleIonSelect(value: any) {
this.onChange(value); this.onChange(value);
setTimeout(() => {
setIonicClasses(this.element);
});
} }
@HostListener('ionBlur') @HostListener('ionBlur')
_handleBlurEvent() { _handleBlurEvent() {
this.onTouched(); this.onTouched();
setTimeout(() => {
setIonicClasses(this.element);
});
} }
registerOnChange(fn: (value: any) => void): void { registerOnChange(fn: (value: any) => void): void {

View File

@ -1,6 +1,8 @@
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { setIonicClasses } from './util/set-ionic-classes';
// NOTE: May need to look at this to see if we need anything else: // 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 // https://github.com/angular/angular/blob/5.0.2/packages/forms/src/directives/select_control_value_accessor.ts#L28-L158
@Directive({ @Directive({
@ -16,8 +18,8 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
}) })
export class SelectValueAccessor implements ControlValueAccessor { export class SelectValueAccessor implements ControlValueAccessor {
constructor(private element: ElementRef, private renderer: Renderer2) { constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {}; this.onChange = () => { };
this.onTouched = () => {}; this.onTouched = () => { };
} }
onChange: (value: any) => void; onChange: (value: any) => void;
@ -25,16 +27,23 @@ export class SelectValueAccessor implements ControlValueAccessor {
writeValue(value: any) { writeValue(value: any) {
this.renderer.setProperty(this.element.nativeElement, 'value', value); this.renderer.setProperty(this.element.nativeElement, 'value', value);
setIonicClasses(this.element);
} }
@HostListener('ionChange', ['$event.target.value']) @HostListener('ionChange', ['$event.target.value'])
_handleChangeEvent(value: any) { _handleChangeEvent(value: any) {
this.onChange(value); this.onChange(value);
setTimeout(() => {
setIonicClasses(this.element);
});
} }
@HostListener('ionBlur') @HostListener('ionBlur')
_handleBlurEvent() { _handleBlurEvent() {
this.onTouched(); this.onTouched();
setTimeout(() => {
setIonicClasses(this.element);
});
} }
registerOnChange(fn: (value: any) => void) { registerOnChange(fn: (value: any) => void) {

View File

@ -1,6 +1,8 @@
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { setIonicClasses } from './util/set-ionic-classes';
@Directive({ @Directive({
/* tslint:disable-next-line:directive-selector */ /* tslint:disable-next-line:directive-selector */
selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar', selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar',
@ -14,8 +16,8 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
}) })
export class TextValueAccessor implements ControlValueAccessor { export class TextValueAccessor implements ControlValueAccessor {
constructor(private element: ElementRef, private renderer: Renderer2) { constructor(private element: ElementRef, private renderer: Renderer2) {
this.onChange = () => {}; this.onChange = () => { };
this.onTouched = () => {}; this.onTouched = () => { };
} }
onChange: (value: any) => void; onChange: (value: any) => void;
@ -23,16 +25,23 @@ export class TextValueAccessor implements ControlValueAccessor {
writeValue(value: any) { writeValue(value: any) {
this.renderer.setProperty(this.element.nativeElement, 'value', value); this.renderer.setProperty(this.element.nativeElement, 'value', value);
setIonicClasses(this.element);
} }
@HostListener('input', ['$event.target.value']) @HostListener('input', ['$event.target.value'])
_handleInputEvent(value: any) { _handleInputEvent(value: any) {
this.onChange(value); this.onChange(value);
setTimeout(() => {
setIonicClasses(this.element);
});
} }
@HostListener('ionBlur') @HostListener('ionBlur')
_handleBlurEvent() { _handleBlurEvent() {
this.onTouched(); this.onTouched();
setTimeout(() => {
setIonicClasses(this.element);
});
} }
registerOnChange(fn: (value: any) => void) { registerOnChange(fn: (value: any) => void) {

View File

@ -0,0 +1,20 @@
import { ElementRef } from '@angular/core';
export function setIonicClasses(element: ElementRef) {
const classList = element.nativeElement.classList;
classList.remove('ion-invalid');
classList.remove('ion-valid');
classList.remove('ion-touched');
classList.remove('ion-untouched');
classList.remove('ion-dirty');
classList.remove('ion-pristine');
classList.forEach((cls: string) => {
if (cls === 'ng-invalid') { classList.add('ion-invalid'); }
if (cls === 'ng-valid') { classList.add('ion-valid'); }
if (cls === 'ng-touched') { classList.add('ion-touched'); }
if (cls === 'ng-untouched') { classList.add('ion-untouched'); }
if (cls === 'ng-dirty') { classList.add('ion-dirty'); }
if (cls === 'ng-pristine') { classList.add('ion-pristine'); }
});
}