diff --git a/packages/angular/demo/src/app/inputs/inputs-test-page.component.html b/packages/angular/demo/src/app/inputs/inputs-test-page.component.html index b429d334ce..90bcbaae4e 100644 --- a/packages/angular/demo/src/app/inputs/inputs-test-page.component.html +++ b/packages/angular/demo/src/app/inputs/inputs-test-page.component.html @@ -11,6 +11,14 @@ Entered Data: {{testInputOne}} + + + + + + Entered Data: {{testCheckboxOne}} + + Home diff --git a/packages/angular/demo/src/app/inputs/inputs-test-page.component.ts b/packages/angular/demo/src/app/inputs/inputs-test-page.component.ts index b664a78afc..f7bdc749e4 100644 --- a/packages/angular/demo/src/app/inputs/inputs-test-page.component.ts +++ b/packages/angular/demo/src/app/inputs/inputs-test-page.component.ts @@ -7,6 +7,7 @@ import { Component, OnInit, ViewEncapsulation } from '@angular/core'; encapsulation: ViewEncapsulation.None }) export class InputsTestPageComponent implements OnInit { + testCheckboxOne = true; testInputOne = 'This is data for test input one'; constructor() {} diff --git a/packages/angular/demo/src/app/shared/ion-checkbox-value-accessor.directive.ts b/packages/angular/demo/src/app/shared/ion-checkbox-value-accessor.directive.ts new file mode 100644 index 0000000000..950b453ba5 --- /dev/null +++ b/packages/angular/demo/src/app/shared/ion-checkbox-value-accessor.directive.ts @@ -0,0 +1,40 @@ +import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; +import { ControlValueAccessor, DefaultValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; + +// NOTE: this is just a sample. It really belongs in @ionic/angular and not at all int his app here +@Directive({ + selector: 'ion-checkbox', + providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IonCheckboxValueAccessorDirective, multi: true }] +}) +export class IonCheckboxValueAccessorDirective implements ControlValueAccessor { + constructor(private element: ElementRef, private renderer: Renderer2) { + this.onChange = () => {}; + this.onTouched = () => {}; + } + + onChange: (value: any) => void; + onTouched: () => void; + + writeValue(value: any) { + console.log('writeValue', value); + this.renderer.setProperty(this.element.nativeElement, 'checked', value); + } + + @HostListener('change', ['$event.target.checked']) + _handleIonChange(value: any) { + console.log('handle change', value); + this.onChange(value); + } + + registerOnChange(fn: (value: any) => void): void { + this.onChange = fn; + } + + registerOnTouched(fn: () => void): void { + this.onTouched = fn; + } + + setDisabledState(isDisabled: boolean): void { + this.renderer.setProperty(this.element.nativeElement, 'disabled', isDisabled); + } +} diff --git a/packages/angular/demo/src/app/shared/ion-input-value-accessor.directive.ts b/packages/angular/demo/src/app/shared/ion-input-value-accessor.directive.ts index 8fcda4aadd..945f0c4a26 100644 --- a/packages/angular/demo/src/app/shared/ion-input-value-accessor.directive.ts +++ b/packages/angular/demo/src/app/shared/ion-input-value-accessor.directive.ts @@ -6,27 +6,29 @@ import { ControlValueAccessor, DefaultValueAccessor, NG_VALUE_ACCESSOR } from '@ selector: 'ion-input', providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IonInputValueAccessorDirective, multi: true }] }) -export class IonInputValueAccessorDirective implements ControlValueAccessor { +export class IonInputValueAccessorDirective implements ControlValueAccessor { constructor(private element: ElementRef, private renderer: Renderer2) { this.onChange = () => {}; + this.onTouched = () => {}; } - onChange: (value: string) => void; + onChange: (value: any) => void; + onTouched: () => void; - writeValue(value: string) { + writeValue(value: any) { this.renderer.setProperty(this.element.nativeElement, 'value', value); } @HostListener('input', ['$event.target.value']) - _handleIllyChange(value: string) { + _handleInputEvent(value: any) { this.onChange(value); } - registerOnChange(fn: (value: string) => void) { - this.onChange = value => { - fn(value); - }; + registerOnChange(fn: (value: any) => void) { + this.onChange = fn; } - registerOnTouched() {} + registerOnTouched(fn: () => void) { + this.onTouched = fn; + } } diff --git a/packages/angular/demo/src/app/shared/shared.module.ts b/packages/angular/demo/src/app/shared/shared.module.ts index c266503146..9258aa64f4 100644 --- a/packages/angular/demo/src/app/shared/shared.module.ts +++ b/packages/angular/demo/src/app/shared/shared.module.ts @@ -1,9 +1,10 @@ import { NgModule } from '@angular/core'; +import { IonCheckboxValueAccessorDirective } from './ion-checkbox-value-accessor.directive'; import { IonInputValueAccessorDirective } from './ion-input-value-accessor.directive'; @NgModule({ - exports: [IonInputValueAccessorDirective], - declarations: [IonInputValueAccessorDirective] + exports: [IonCheckboxValueAccessorDirective, IonInputValueAccessorDirective], + declarations: [IonCheckboxValueAccessorDirective, IonInputValueAccessorDirective] }) export class SharedModule { }