feat(demo): make our value accessors more closely match Angular's

This commit is contained in:
Ken Sodemann
2017-11-16 09:40:45 -06:00
parent e50d7fbfe4
commit c23e2f717c
7 changed files with 49 additions and 14 deletions

View File

@ -15,8 +15,8 @@ describe('Demo Inputs Page', () => {
describe('input one', () => {
it('should display the starting text', () => {
page.navigateTo();
const el = page.getInputOne();
expect(el.getAttribute('value')).toEqual('This is data for test input one');
const el = page.getIonicTextInput();
expect(el.getAttribute('value')).toEqual('This is the Ionic Text Input');
});
});
});

View File

@ -9,11 +9,11 @@ export class InputsPage {
return element(by.css('.title')).getText();
}
getInputOne() {
return element(by.id('inputOne'));
getIonicTextInput() {
return element(by.id('ionTextInput'));
}
getOutputOneText() {
return element(by.id('outputOne')).getText();
getIonicTextInputOutputText() {
return element(by.id('ionTextInputOutput')).getText();
}
}

View File

@ -5,18 +5,34 @@
<ion-grid>
<ion-row>
<ion-col>
<ion-input id="inputOne" name="inputOne" [(ngModel)]="testInputOne"></ion-input>
<input id="stdTextInput" name="stdTextInput" [(ngModel)]="stdTextInput" (change)="change($event)" (blur)="onBlur($event)" />
</ion-col>
<ion-col>
Entered Data: <span id="outputOne">{{testInputOne}}</span>
Entered Data: <span id="stdTextInputOutput">{{stdTextInput}}</span>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-checkbox id="checkboxOne" name="checkboxOne" [(ngModel)]="testCheckboxOne"></ion-checkbox>
<ion-input id="ionTextInput" name="ionTextInput" [(ngModel)]="ionTextInput" (change)="change($event)" (blur)="onBlur($event)"></ion-input>
</ion-col>
<ion-col>
Entered Data: <span id="outputCheckboxOne">{{testCheckboxOne}}</span>
Entered Data: <span id="ionTextInputOutput">{{ionTextInput}}</span>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<input type="checkbox" id="stdCheckbox" name="stdCheckbox" [(ngModel)]="stdCheckbox" (change)="change($event)" (blur)="onBlur($event)"/>
</ion-col>
<ion-col>
Entered Data: <span id="stdCheckboxOutput">{{stdCheckbox}}</span>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-checkbox id="ionCheckbox" name="ionCheckbox" [(ngModel)]="ionCheckbox" (change)="change($event)" (blur)="onBlur($event)"></ion-checkbox>
</ion-col>
<ion-col>
Entered Data: <span id="ionCheckboxOutput">{{ionCheckbox}}</span>
</ion-col>
</ion-row>
</ion-grid>

View File

@ -1,7 +1,9 @@
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { SharedModule } from '../shared/shared.module';
import { InputsTestPageComponent } from './inputs-test-page.component';
describe('InputsTestPageComponent', () => {
@ -12,6 +14,7 @@ describe('InputsTestPageComponent', () => {
async(() => {
TestBed.configureTestingModule({
declarations: [InputsTestPageComponent],
imports: [FormsModule, SharedModule],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
})

View File

@ -7,10 +7,21 @@ 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';
ionTextInput = 'This is the Ionic Text Input';
stdTextInput = 'This is the HTML Text Input';
ionCheckbox = true;
stdCheckbox = true;
constructor() {}
ngOnInit() {}
onBlur(evt) {
console.log('blur: ', evt);
}
change(evt) {
console.log('change: ', evt);
}
}

View File

@ -16,13 +16,11 @@ export class IonCheckboxValueAccessorDirective implements ControlValueAccessor {
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);
}

View File

@ -2,6 +2,8 @@ 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
// May also need to look at this to see if we need anything else:
// https://github.com/angular/angular/blob/5.0.1/packages/forms/src/directives/default_value_accessor.ts#L33-L101
@Directive({
selector: 'ion-input',
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: IonInputValueAccessorDirective, multi: true }]
@ -24,6 +26,11 @@ export class IonInputValueAccessorDirective implements ControlValueAccessor {
this.onChange(value);
}
@HostListener('blur')
_handleBlurEvent() {
this.onTouched();
}
registerOnChange(fn: (value: any) => void) {
this.onChange = fn;
}