forms wip

This commit is contained in:
Tim Lancina
2015-06-23 11:29:12 -05:00
parent d030d9e136
commit fded55b679
3 changed files with 121 additions and 25 deletions

View File

@ -1,4 +1,6 @@
import {ElementRef} from 'angular2/angular2' import {ElementRef, Renderer} from 'angular2/angular2';
import {isPresent} from 'angular2/src/facade/lang';
import {setProperty} from 'angular2/src/forms/directives/shared'
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations'; import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
import {Ancestor} from 'angular2/src/core/annotations_impl/visibility'; import {Ancestor} from 'angular2/src/core/annotations_impl/visibility';
@ -6,7 +8,7 @@ import {View} from 'angular2/src/core/annotations_impl/view';
import {onInit} from 'angular2/angular2'; import {onInit} from 'angular2/angular2';
//pretty sure this has changed in the latest angular //pretty sure this has changed in the latest angular
import {ControlDirective} from 'angular2/forms'; import {NgControl} from 'angular2/forms';
import {IonicComponent} from '../../config/component'; import {IonicComponent} from '../../config/component';
import {Icon} from '../icon/icon'; import {Icon} from '../icon/icon';
@ -38,21 +40,32 @@ export class Checkbox {
}, },
host: { host: {
'(^click)': 'onClick($event)', '(^click)': 'onClick($event)',
'[checked]': 'checked',
'[attr.aria-checked]': 'checked', '[attr.aria-checked]': 'checked',
'[attr.aria-disabled]': 'disabled', '[attr.aria-disabled]': 'disabled',
'[attr.value]': 'value', '[attr.value]': 'value',
'role': 'checkbox', 'role': 'checkbox',
'class': 'item' 'class': 'item',
'[class.ng-untouched]': 'ngClassUntouched',
'[class.ng-touched]': 'ngClassTouched',
'[class.ng-pristine]': 'ngClassPristine',
'[class.ng-dirty]': 'ngClassDirty',
'[class.ng-valid]': 'ngClassValid',
'[class.ng-invalid]': 'ngClassInvalid'
}, },
appInjector: [ ControlDirective ] appInjector: [ NgControl ]
} }
} }
constructor( constructor(
cd: ControlDirective ngControl: NgControl,
renderer: Renderer,
elementRef: ElementRef
) { ) {
this.controlDirective = cd; this.ngControl = ngControl;
cd.valueAccessor = this; this.renderer = renderer;
this.elementRef = elementRef;
this.ngControl.valueAccessor = this;
} }
onInit() { onInit() {
@ -66,10 +79,12 @@ export class Checkbox {
writeValue(value) { writeValue(value) {
// Convert it to a boolean // Convert it to a boolean
this.checked = !!value; this.checked = !!value;
setProperty(this.renderer, this.elementRef, "checked", value);
} }
set checked(checked) { set checked(checked) {
this._checked = checked this._checked = checked
this.ngControl.control.checked = checked;
//this.controlDirective._control().updateValue(this._checked); //this.controlDirective._control().updateValue(this._checked);
} }
get checked() { get checked() {
@ -79,4 +94,22 @@ export class Checkbox {
this.checked = !this.checked; this.checked = !this.checked;
} }
get ngClassUntouched() {
return isPresent(this.ngControl.control) ? this.ngControl.control.untouched : false;
}
get ngClassTouched() {
return isPresent(this.ngControl.control) ? this.ngControl.control.touched : false;
}
get ngClassPristine() {
return isPresent(this.ngControl.control) ? this.ngControl.control.pristine : false;
}
get ngClassDirty() { return isPresent(this.ngControl.control) ? this.ngControl.control.dirty : false; }
get ngClassValid() { return isPresent(this.ngControl.control) ? this.ngControl.control.valid : false; }
get ngClassInvalid() {
return isPresent(this.ngControl.control) ? !this.ngControl.control.valid : false;
}
registerOnChange(fn): void { this.onChange = fn; }
registerOnTouched(fn): void { this.onTouched = fn; }
} }

View File

@ -1,24 +1,86 @@
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations'; import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
import {FormBuilder, Validators, formDirectives} from 'angular2/forms'; import {View} from 'angular2/src/core/annotations_impl/view';
import {IonicView} from 'ionic/ionic'; import {Query, QueryList, NgFor} from 'angular2/angular2';
// import {FormBuilder, Validators, NgFormControl, Control, NgControlName, NgControlGroup, ControlContainer} from 'angular2/forms';
import {
Control,
ControlGroup,
NgForm,
formDirectives,
Validators,
NgControl,
ControlValueAccessor,
NgControlName,
NgFormModel
} from 'angular2/forms';
import {Checkbox, Content, List} from 'ionic/ionic';
//import {IONIC_DIRECTIVES} from 'ionic/ionic'
// @Component({
// selector: 'checkbox-wrapper',
// properties: [
// 'ctrl'
// ]
// })
// @View({ //TODO: Pass in component property as checkbox content?
// template: `<ion-checkbox [ng-form-control]="ctrl">
// {{ctrlname}}
// </ion-checkbox>`,
// directives: [IonicApp, Checkbox, NgFormControl]
// })
// class CheckboxWrapper{
// constructor(@Query(Checkbox, {descendants: true}) checkboxes: QueryList){
// this.checkboxes = checkboxes;
//
// //doesn't work
// this.checkboxes.onChange(this.onCheckboxChange);
// setTimeout(() => console.log(this.ctrlname));
//
// //setTimeout(() => this.onCheckboxChange(), 3000);
// }
//
// onCheckboxChange(val) {
// debugger;
// }
// }
@Component({ selector: 'ion-app' }) @Component({ selector: 'ion-app' })
@IonicView({ @View({
templateUrl: 'main.html', templateUrl: 'main.html',
directives: [formDirectives] directives: [Checkbox, List, Content, NgControlName, NgFormModel]
}) })
class IonicApp { class IonicApp {
constructor() { constructor() {
var fb = new FormBuilder(); // var fb = new FormBuilder();
this.fruitsForm = fb.group({ // this.controls = {
appleCtrl: ['', Validators.required], // appleCtrl : ['', Validators.required],
bananaCtrl: ['', Validators.required], // bananaCtrl: ['', Validators.required],
grapeCtrl: ['', Validators.required], // grapeCtrl: ['', Validators.required],
cherryCtrl: ['', Validators.required] // cherryCtrl: ['', Validators.required]
// };
//
// this.fruitsForm = fb.group(this.controls);
this.fruitsForm = new ControlGroup({
"appleCtrl": new Control("APPLE", isChecked),
"bananaCtrl": new Control("BANANA", isChecked),
"grapeCtrl": new Control("GRAPE", isChecked),
"cherryCtrl": new Control("CHERRY", isChecked)
}); });
debugger; // this.appleCtrl = new Control("APPLE", isChecked);
// this.bananaCtrl = new Control("BANANA", isChecked);
// this.grapeCtrl = new Control("GRAPE", isChecked);
// this.cherryCtrl = new Control("CHERRY", isChecked);
function isChecked(ctrl) {
if (ctrl.checked) {
return null;
} else {
return {
'notChecked': true
}
}
}
} }
doSubmit(event) { doSubmit(event) {
@ -27,6 +89,7 @@ class IonicApp {
} }
} }
export function main(ionicBootstrap) { export function main(ionicBootstrap) {
ionicBootstrap(IonicApp); ionicBootstrap(IonicApp);
} }

View File

@ -1,18 +1,18 @@
<ion-content> <ion-content>
<form (^submit)="doSubmit($event)" [control-group]="fruitsForm"> <form (^submit)="doSubmit($event)" [ng-form-model]="fruitsForm">
<ion-list> <ion-list>
<div class="list-header">Some Switches</div> <div class="list-header">Some Switches</div>
<ion-checkbox control="appleCtrl"> <ion-checkbox [ng-control]="appleCtrl">
Apples Apples
</ion-checkbox> </ion-checkbox>
<ion-checkbox control="bananaCtrl"> <ion-checkbox [ng-control]="bananaCtrl">
Bananas Bananas
</ion-checkbox> </ion-checkbox>
<ion-checkbox control="grapeCtrl"> <ion-checkbox [ng-control]="grapeCtrl">
Grapes Grapes
</ion-checkbox> </ion-checkbox>
<ion-checkbox control="cherryCtrl"> <ion-checkbox [ng-control]="cherryCtrl">
Cherries Cherries
</ion-checkbox> </ion-checkbox>
</ion-list> </ion-list>