checkbox wip

This commit is contained in:
Tim Lancina
2015-07-16 16:46:39 -05:00
parent 1ae396ebe4
commit a9a1a6900c
5 changed files with 92 additions and 136 deletions

View File

@ -11,10 +11,12 @@
.checkbox-on { .checkbox-on {
display: none; display: none;
pointer-events: none;
} }
.checkbox-off { .checkbox-off {
display: block; display: block;
pointer-events: none;
} }
.checkbox[aria-checked=true] { .checkbox[aria-checked=true] {

View File

@ -1,117 +1,92 @@
import {View, ElementRef, Renderer, EventEmitter, onChange, onInit} from 'angular2/angular2'; import {
import {isPresent} from 'angular2/src/facade/lang'; View,
import {setProperty} from 'angular2/src/forms/directives/shared' Directive,
ElementRef,
//pretty sure this has changed in the latest angular Optional,
import {NgControl} from 'angular2/forms'; Parent,
NgControl
} from 'angular2/angular2';
import {Ion} from '../ion'; import {Ion} from '../ion';
import {IonicConfig} from '../../config/config'; import {IonicConfig} from '../../config/config';
import {IonicComponent} from '../../config/annotations'; import {IonicComponent, IonicView} from '../../config/annotations';
import {Icon} from '../icon/icon'; import {Icon} from '../icon/icon';
@IonicComponent({ @IonicComponent({
selector: 'ion-checkbox', selector: 'ion-checkbox',
properties: [ host: {
'checked', 'class': 'item',
'disabled', '[attr.aria-checked]': 'checked'
'value' },
],
defaultProperties: { defaultProperties: {
'iconOff': 'ion-ios-circle-outline', 'iconOff': 'ion-ios-circle-outline',
'iconOn': 'ion-ios-checkmark' 'iconOn': 'ion-ios-checkmark'
}, }
//events: ['change'],
host: {
'(^click)': 'onClick($event)',
//'(change)': 'onChange($event.checked)',
'(blur)': 'onTouched()',
//'[checked]': 'checked',
'[attr.aria-checked]': 'checked',
'[attr.aria-disabled]': 'disabled',
'[attr.value]': 'value',
'role': 'checkbox',
'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: [ NgControl ]
//lifecycle: [onChange]
}) })
@View({ @IonicView({
template: ` template:
<div class="item-media media-checkbox"> '<div class="item-media media-checkbox" (click)="onClick($event)">' +
<icon [name]="iconOff" class="checkbox-off"></icon> '<icon [name]="iconOff" class="checkbox-off"></icon>' +
<icon [name]="iconOn" class="checkbox-on"></icon> '<icon [name]="iconOn" class="checkbox-on"></icon>' +
</div> '<content select="input"></content>' +
<div class="item-content"> '</div>' +
<div class="item-label"> '<div class="item-content">' +
<content></content> '<div class="item-label">' +
</div> '<content></content>' +
</div>`, '</div>' +
directives: [Icon] '</div>'
}) })
export class Checkbox extends Ion { export class Checkbox extends Ion {
_checkbox: CheckboxInput;
constructor( constructor(
ngControl: NgControl,
renderer: Renderer,
elementRef: ElementRef, elementRef: ElementRef,
ionicConfig: IonicConfig ionicConfig: IonicConfig
) { ) {
super(elementRef, ionicConfig); super(elementRef, ionicConfig);
this.ngControl = ngControl;
this.renderer = renderer;
this.elementRef = elementRef;
this.ngControl.valueAccessor = this;
//this.change = new EventEmitter("change");
} }
/** onAllChangesDone() {
* Much like ngModel, this is called from our valueAccessor for the attached // Enforce that this directive actually contains a checkbox
* ControlDirective to update the value internally. if (this._checkbox == null) {
*/ throw 'No <input type="checkbox"> found inside of <ion-checkbox>';
writeValue(value) { }
// Convert it to a boolean
this.checked = !!value;
} }
set checked(checked) { registerCheckbox(checkboxDir) {
this._checked = checked; if (this._checkbox != null) {
// doesn't trigger change/validation for control? throw 'Only one <input type="checkbox"> is allowed per <ion-checkbox>'
setProperty(this.renderer, this.elementRef, "checked", checked); }
//this.ngControl.control.checked = checked; this._checkbox = checkboxDir.elementRef.nativeElement;
//this.change.next(checked); this._checkboxDir = checkboxDir;
}
get checked() {
return this._checked
}
onClick() {
this.checked = !this.checked;
} }
get ngClassUntouched() { onClick(e) {
return isPresent(this.ngControl.control) ? this.ngControl.control.untouched : false; let val = !this._checkbox.checked;
} this._checkbox.checked = val;
get ngClassTouched() { this.checked = val;
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; } //TODO figure out a way to trigger change on the actual input to trigger
registerOnTouched(fn): void { this.onTouched = fn; } // form updates
// this._checkbox.dispatchEvent(e);
//this._checkboxDir.control.valueAccessor.writeValue(val);
}
}
@Directive({
selector: 'input[type=checkbox]'
})
export class CheckboxInput {
constructor(
elementRef: ElementRef,
@Optional() @Parent() container: Checkbox,
@Optional() control: NgControl
) {
this.elementRef = elementRef;
this.control = control ? control : null;
container && container.registerCheckbox(this);
}
} }

View File

@ -17,40 +17,12 @@ import {
import {App, Checkbox, Content, List} from 'ionic/ionic'; import {App, Checkbox, Content, List} from 'ionic/ionic';
//import {IONIC_DIRECTIVES} 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;
// }
// }
@App({ @App({
templateUrl: 'main.html', templateUrl: 'main.html'
directives: [Checkbox, List, Content, NgControlName, NgFormModel]
}) })
class IonicApp { class IonicApp {
constructor() { constructor() {
// var fb = new FormBuilder(); // var fb = new FormBuilder();
// this.controls = { // this.controls = {
// appleCtrl : ['', Validators.required], // appleCtrl : ['', Validators.required],
@ -62,15 +34,12 @@ class IonicApp {
// this.fruitsForm = fb.group(this.controls); // this.fruitsForm = fb.group(this.controls);
this.fruitsForm = new ControlGroup({ this.fruitsForm = new ControlGroup({
"appleCtrl": new Control("APPLE", isChecked), "appleCtrl": new Control("", isChecked),
"bananaCtrl": new Control("BANANA", isChecked), "bananaCtrl": new Control("", isChecked),
"grapeCtrl": new Control("GRAPE", isChecked), // "bananaCtrl": new Control("BANANA", isChecked),
"cherryCtrl": new Control("CHERRY", isChecked) // "grapeCtrl": new Control("GRAPE", isChecked),
// "cherryCtrl": new Control("CHERRY", isChecked)
}); });
this.appleCtrl = "appleCtrl";
this.bananaCtrl = "bananaCtrl";
this.grapeCtrl = "grapeCtrl";
this.cherryCtrl = "cherryCtrl";
function isChecked(ctrl) { function isChecked(ctrl) {
if (ctrl.checked) { if (ctrl.checked) {

View File

@ -1,20 +1,28 @@
<ion-content> <ion-content>
<form (^submit)="doSubmit($event)" [ng-form-model]="fruitsForm"> <form (^submit)="doSubmit($event)" [ng-form-model]="fruitsForm">
<ion-checkbox icon-on="ion-android-checkmark-circle"><input #apple type="checkbox" ng-control="appleCtrl"></ion-checkbox>
<ion-list> <ion-checkbox><input #banana type="checkbox" ng-control="bananaCtrl"><span>Test</span></ion-checkbox>
<div class="list-header">Some Switches</div>
<ion-checkbox [ng-control]="appleCtrl"> <!-- <ion-checkbox ng-control="appleCtrl">
Apples Apples
</ion-checkbox> </ion-checkbox>
<ion-checkbox [ng-control]="bananaCtrl"> <ion-checkbox ng-control="bananaCtrl">
Bananas Bananas
</ion-checkbox> </ion-checkbox>
<ion-checkbox [ng-control]="grapeCtrl"> <ion-checkbox ng-control="grapeCtrl">
Grapes Grapes
</ion-checkbox> </ion-checkbox>
<ion-checkbox [ng-control]="cherryCtrl"> <ion-checkbox ng-control="cherryCtrl">
Cherries Cherries
</ion-checkbox> </ion-checkbox> -->
</ion-list>
</form> </form>
appleCtrl.dirty: {{fruitsForm.controls.appleCtrl.dirty}}<br>
appleCtrl.value: {{fruitsForm.controls.appleCtrl.value}}<br>
apple.value: {{apple.value}}<br>
apple.checked: {{apple.checked}} <br>
bananaCtrl.dirty: {{fruitsForm.controls.bananaCtrl.dirty}}<br>
bananaCtrl.value: {{fruitsForm.controls.bananaCtrl.value}}<br>
banana.value: {{banana.value}}<br>
banana.checked: {{banana.checked}}
</ion-content> </ion-content>

View File

@ -11,7 +11,7 @@ import {
List, Item, ItemGroup, ItemGroupTitle, List, Item, ItemGroup, ItemGroupTitle,
Toolbar, Toolbar,
Icon, Icon,
Checkbox, Switch, Checkbox, CheckboxInput, Switch,
Input, TextInput, Label, Input, TextInput, Label,
Segment, SegmentButton, SegmentControlValueAccessor, Segment, SegmentButton, SegmentControlValueAccessor,
RadioGroup, RadioButton, SearchBar, RadioGroup, RadioButton, SearchBar,
@ -55,6 +55,8 @@ export const IonicDirectives = [
forwardRef(() => Segment), forwardRef(() => Segment),
forwardRef(() => SegmentButton), forwardRef(() => SegmentButton),
forwardRef(() => SegmentControlValueAccessor), forwardRef(() => SegmentControlValueAccessor),
forwardRef(() => Checkbox),
forwardRef(() => CheckboxInput),
//Checkbox, Switch //Checkbox, Switch
//RadioGroup, RadioButton, SearchBar, //RadioGroup, RadioButton, SearchBar,