mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-08 23:58:13 +08:00
feat(demo): make our value accessors more closely match Angular's
This commit is contained in:
@ -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>
|
||||
|
||||
@ -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();
|
||||
})
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user