mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
radio
This commit is contained in:
@ -33,7 +33,7 @@ export class Label {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.scrollAssist = config.setting('keyboardScrollAssist');
|
this.scrollAssist = config.setting('keyboardScrollAssist');
|
||||||
this.scrollAssist = true;
|
this.scrollAssist = true; //TODO get rid of this
|
||||||
}
|
}
|
||||||
|
|
||||||
pointerStart(ev) {
|
pointerStart(ev) {
|
||||||
|
@ -10,9 +10,10 @@ import {RadioButton} from '../radio/radio';
|
|||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
selector: 'input[type=checkbox],input[type=radio]',
|
selector: 'input[type=checkbox],input[type=radio]',
|
||||||
properties: [ 'checked', 'name' ],
|
properties: [ 'checked', 'name', 'value' ],
|
||||||
host: {
|
host: {
|
||||||
'[checked]': 'checked',
|
'[checked]': 'checked',
|
||||||
|
'[value]': 'value',
|
||||||
'[attr.name]': 'name',
|
'[attr.name]': 'name',
|
||||||
'(change)': 'onChangeEvent($event)'
|
'(change)': 'onChangeEvent($event)'
|
||||||
}
|
}
|
||||||
@ -41,6 +42,8 @@ export class TapInput extends IonInput {
|
|||||||
this.tabIndex = this.tabIndex || '';
|
this.tabIndex = this.tabIndex || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//to detect switching/selecting inputs with the keyboard
|
||||||
|
//view -> model (Control)
|
||||||
onChangeEvent(ev) {
|
onChangeEvent(ev) {
|
||||||
this.container && this.container.onChangeEvent(this);
|
this.container && this.container.onChangeEvent(this);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {ElementRef, Ancestor} from 'angular2/angular2';
|
import {ElementRef, Ancestor, NgControl, Renderer} from 'angular2/angular2';
|
||||||
|
|
||||||
import {IonicDirective, IonicComponent, IonicView} from '../../config/annotations';
|
import {IonicDirective, IonicComponent, IonicView} from '../../config/annotations';
|
||||||
import {IonicConfig} from '../../config/config';
|
import {IonicConfig} from '../../config/config';
|
||||||
@ -19,10 +19,20 @@ export class RadioGroup extends Ion {
|
|||||||
buttons: Array<RadioButton> = [];
|
buttons: Array<RadioButton> = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
cd: NgControl,
|
||||||
|
renderer: Renderer,
|
||||||
elementRef: ElementRef,
|
elementRef: ElementRef,
|
||||||
ionicConfig: IonicConfig
|
ionicConfig: IonicConfig
|
||||||
) {
|
) {
|
||||||
super(elementRef, ionicConfig);
|
super(elementRef, ionicConfig);
|
||||||
|
this.onChange = (_) => {};
|
||||||
|
this.onTouched = (_) => {};
|
||||||
|
this.renderer = renderer;
|
||||||
|
this.elementRef = elementRef;
|
||||||
|
|
||||||
|
cd.valueAccessor = this;
|
||||||
|
|
||||||
|
this.value = "";
|
||||||
this.list = true;
|
this.list = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,17 +42,29 @@ export class RadioGroup extends Ion {
|
|||||||
if (!inputEl.hasAttribute('name')) {
|
if (!inputEl.hasAttribute('name')) {
|
||||||
radioButton.input.name = this._name;
|
radioButton.input.name = this._name;
|
||||||
}
|
}
|
||||||
// if (radioButton && !radioButton.hasAttribute('name')){
|
|
||||||
// radioButton.setAttribute('name', this._name);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(input) {
|
update(input) {
|
||||||
for (let button of this.buttons) {
|
for (let button of this.buttons) {
|
||||||
button.input.checked = button.input.elementRef.nativeElement.checked;
|
button.input.checked = false;
|
||||||
|
}
|
||||||
|
input.checked = true;
|
||||||
|
this.onChange(input.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called by the model (Control) to update the view
|
||||||
|
writeValue(value) {
|
||||||
|
this.value = value;
|
||||||
|
for (let button of this.buttons) {
|
||||||
|
button.input.checked = button.input.value == value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used by the view to update the model (Control)
|
||||||
|
// Up to us to call it in update()
|
||||||
|
registerOnChange(fn) { this.onChange = fn; }
|
||||||
|
|
||||||
|
registerOnTouched(fn) { this.onTouched = fn; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@IonicComponent({
|
@IonicComponent({
|
||||||
@ -51,7 +73,6 @@ export class RadioGroup extends Ion {
|
|||||||
'[class.item]': 'item',
|
'[class.item]': 'item',
|
||||||
'[class.active]': 'input.checked',
|
'[class.active]': 'input.checked',
|
||||||
'[attr.aria-checked]': 'input.checked',
|
'[attr.aria-checked]': 'input.checked',
|
||||||
// '(^click)': 'onClick($event)'
|
|
||||||
},
|
},
|
||||||
defaultProperties: {
|
defaultProperties: {
|
||||||
'iconOff': 'ion-ios-circle-outline',
|
'iconOff': 'ion-ios-circle-outline',
|
||||||
@ -84,30 +105,15 @@ export class RadioButton extends IonInputItem {
|
|||||||
this.group.registerButton(this);
|
this.group.registerButton(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// onClick(ev) {
|
//from clicking the label
|
||||||
// // switching between radio buttons with arrow keys fires a MouseEvent
|
//view -> model (Control)
|
||||||
// if (ev.target.tagName === "INPUT") return;
|
|
||||||
// this.input.checked = !this.input.checked;
|
|
||||||
//
|
|
||||||
// //let bindings update first
|
|
||||||
// setTimeout(() => this.group.update(this.input));
|
|
||||||
//
|
|
||||||
// //TODO figure out a way to trigger change on the actual input to trigger
|
|
||||||
// // form updates
|
|
||||||
//
|
|
||||||
// // this._checkbox.dispatchEvent(e);
|
|
||||||
// //this._checkboxDir.control.valueAccessor.writeValue(val);
|
|
||||||
// }
|
|
||||||
|
|
||||||
focus() {
|
focus() {
|
||||||
let mouseClick = new MouseEvent("click", {
|
this.group.update(this.input);
|
||||||
bubbles: true,
|
|
||||||
cancelable: true,
|
|
||||||
});
|
|
||||||
this.input && this.input.elementRef.nativeElement.dispatchEvent(mouseClick);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onChangeEvent(input) {
|
//from switching inputs with the keyboard
|
||||||
this.group.update(input);
|
//view -> model (Control)
|
||||||
|
onChangeEvent() {
|
||||||
|
this.group.update(this.input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,29 +18,23 @@ import {
|
|||||||
})
|
})
|
||||||
class IonicApp {
|
class IonicApp {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.fruitsGroup1 = new ControlGroup({
|
this.fruits = new Control("");
|
||||||
"appleCtrl": new Control("", isChecked),
|
|
||||||
"bananaCtrl": new Control("", isChecked)
|
|
||||||
});
|
|
||||||
this.fruitsGroup2 = new ControlGroup({
|
|
||||||
"grapeCtrl": new Control("", isChecked),
|
|
||||||
"cherryCtrl": new Control("", isChecked)
|
|
||||||
});
|
|
||||||
|
|
||||||
this.fruitsForm = new ControlGroup({
|
this.fruitsForm = new ControlGroup({
|
||||||
"fruitGroup1": this.fruitsGroup1,
|
"fruits": this.fruits
|
||||||
"fruitGroup2": this.fruitsGroup2
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function isChecked(ctrl) {
|
setApple() {
|
||||||
if (ctrl.checked) {
|
this.fruits.updateValue("apple");
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
'notChecked': true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setBanana() {
|
||||||
|
this.fruits.updateValue("banana");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setCherry() {
|
||||||
|
this.fruits.updateValue("cherry");
|
||||||
}
|
}
|
||||||
|
|
||||||
doSubmit(event) {
|
doSubmit(event) {
|
||||||
|
@ -5,26 +5,20 @@
|
|||||||
|
|
||||||
<form (^submit)="doSubmit($event)" [ng-form-model]="fruitsForm">
|
<form (^submit)="doSubmit($event)" [ng-form-model]="fruitsForm">
|
||||||
<div class="list-header">
|
<div class="list-header">
|
||||||
Fruit Group 1
|
Fruits
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ion-radio-group ng-control-group="fruitGroup1">
|
<ion-radio-group ng-control="fruits">
|
||||||
<ion-radio><label>APPLE</label><input #apple type="radio" ng-control="appleCtrl"></ion-radio>
|
<ion-radio><label>APPLE</label><input value="apple" type="radio"></ion-radio>
|
||||||
<ion-radio><label>BANANA</label><input #banana type="radio" ng-control="bananaCtrl"></ion-radio>
|
<ion-radio><label>BANANA</label><input value="banana" type="radio"></ion-radio>
|
||||||
|
<ion-radio><label>CHERRY</label><input value="cherry" type="radio"></ion-radio>
|
||||||
</ion-radio-group>
|
</ion-radio-group>
|
||||||
|
|
||||||
appleCtrl.dirty: {{fruitsForm.controls.fruitGroup1.controls.appleCtrl.dirty}}<br>
|
fruits.dirty: {{fruitsForm.controls.fruits.dirty}}<br>
|
||||||
|
fruits.value: {{fruitsForm.controls.fruits.value}}<br>
|
||||||
<div class="list-header">
|
|
||||||
Fruit Group 2
|
|
||||||
</div>
|
|
||||||
<ion-radio-group ng-control-group="fruitGroup2">
|
|
||||||
<ion-radio><label>GRAPE</label><input #grape name="test" type="radio" ng-control="grapeCtrl"></ion-radio>
|
|
||||||
<ion-radio><label>CHERRY</label><input #cherry name="test" type="radio" ng-control="cherryCtrl"></ion-radio>
|
|
||||||
</ion-radio-group>
|
|
||||||
|
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
<button (click)="setApple()">Select Apple</button>
|
||||||
|
<button (click)="setBanana()">Select Banana</button>
|
||||||
|
<button (click)="setCherry()">Select Cherry</button>
|
||||||
</ion-content>
|
</ion-content>
|
||||||
</ion-view>
|
</ion-view>
|
||||||
|
Reference in New Issue
Block a user