radio updates

This commit is contained in:
Adam Bradley
2015-08-06 13:52:12 -05:00
parent 9cf986eefc
commit b8e08a442a
4 changed files with 79 additions and 57 deletions

View File

@ -34,7 +34,7 @@ export class TapDisabled {}
@Directive({ @Directive({
selector: 'button,[button],[tappable],ion-checkbox', selector: 'button,[button],[tappable],ion-checkbox,ion-radio',
host: { host: {
'(^touchstart)': 'touchStart($event)', '(^touchstart)': 'touchStart($event)',
'(^touchend)': 'touchEnd($event)', '(^touchend)': 'touchEnd($event)',

View File

@ -18,7 +18,8 @@ import {TapClick} from '../button/button';
properties: [ properties: [
'value', 'value',
'checked', 'checked',
'disabled' 'disabled',
'id'
], ],
host: { host: {
'class': 'item', 'class': 'item',
@ -28,8 +29,7 @@ import {TapClick} from '../button/button';
'[attr.aria-disabled]': 'disabled', '[attr.aria-disabled]': 'disabled',
'[attr.aria-labelledby]': 'labelId', '[attr.aria-labelledby]': 'labelId',
'(^click)': 'click($event)' '(^click)': 'click($event)'
}, }
exportAs: 'checkbox'
}) })
@IonicView({ @IonicView({
template: template:
@ -42,9 +42,9 @@ import {TapClick} from '../button/button';
}) })
export class Checkbox extends IonInputItem { export class Checkbox extends IonInputItem {
constructor( constructor(
@Optional() cd: NgControl,
elementRef: ElementRef, elementRef: ElementRef,
config: IonicConfig, config: IonicConfig,
@Optional() cd: NgControl,
tapClick: TapClick tapClick: TapClick
) { ) {
super(elementRef, config); super(elementRef, config);
@ -77,8 +77,8 @@ export class Checkbox extends IonInputItem {
} }
} }
writeValue(modelValue) { writeValue(value) {
this.checked = modelValue; this.checked = value;
} }
// Used by the view to update the model (Control) // Used by the view to update the model (Control)

View File

@ -1,63 +1,62 @@
import {ElementRef, Ancestor, NgControl, Renderer} from 'angular2/angular2'; import {ElementRef, Ancestor, Optional, NgControl} 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';
import {Ion} from '../ion'; import {Ion} from '../ion';
import {IonInputItem} from '../form/form'; import {IonInputItem} from '../form/form';
import {TapClick} from '../button/button';
let groupName = -1;
@IonicDirective({ @IonicDirective({
selector: 'ion-radio-group', selector: 'ion-radio-group',
host: { host: {
'class': 'list' 'class': 'list',
'role': 'radiogroup',
'[attr.aria-activedescendant]': 'activeId'
} }
}) })
export class RadioGroup extends Ion { export class RadioGroup extends Ion {
radios: Array<RadioButton> = [];
_name: number = ++groupName;
buttons: Array<RadioButton> = [];
constructor( constructor(
cd: NgControl,
renderer: Renderer,
elementRef: ElementRef, elementRef: ElementRef,
ionicConfig: IonicConfig config: IonicConfig,
@Optional() cd: NgControl
) { ) {
super(elementRef, ionicConfig); super(elementRef, config);
this.id = ++radioGroupIds;
this.radioIds = -1;
this.onChange = (_) => {}; this.onChange = (_) => {};
this.onTouched = (_) => {}; this.onTouched = (_) => {};
this.renderer = renderer;
this.elementRef = elementRef;
cd.valueAccessor = this; if (cd) cd.valueAccessor = this;
this.value = "";
} }
registerButton(radioButton) { registerRadio(radio) {
this.buttons.push(radioButton); radio.id = radio.id || ('radio-' + this.id + '-' + (++this.radioIds));
let inputEl = radioButton.input.elementRef.nativeElement; this.radios.push(radio);
if (!inputEl.hasAttribute('name')) {
radioButton.input.name = this._name; if (radio.checked) {
this.value = radio.value;
this.activeId = radio.id;
} }
} }
//from clicking the label or switching inputs with keyboard update(checkedRadio) {
//view -> model (Control) this.value = checkedRadio.value;
update(input) { this.activeId = checkedRadio.id;
for (let button of this.buttons) {
button.input.checked = false; for (let radio of this.radios) {
} radio.checked = (radio === checkedRadio);
input.checked = true; }
this.onChange(input.value);
this.onChange(this.value);
} }
// Called by the model (Control) to update the view
writeValue(value) { writeValue(value) {
this.value = value; this.value = value;
for (let button of this.buttons) { for (let radio of this.radios) {
button.input.checked = button.input.value == value; radio.checked = (radio.value == value);
} }
} }
@ -68,16 +67,28 @@ export class RadioGroup extends Ion {
registerOnTouched(fn) { this.onTouched = fn; } registerOnTouched(fn) { this.onTouched = fn; }
} }
@IonicComponent({ @IonicComponent({
selector: 'ion-radio', selector: 'ion-radio',
properties: [
'value',
'checked',
'disabled',
'id'
],
host: { host: {
'class': 'item', 'class': 'item',
'[attr.aria-checked]': 'input.checked', 'role': 'radio',
'[attr.tab-index]': 'tabIndex',
'[attr.aria-checked]': 'checked',
'[attr.aria-disabled]': 'disabled',
'[attr.aria-labelledby]': 'labelId',
'(^click)': 'click($event)'
} }
}) })
@IonicView({ @IonicView({
template: template:
'<div class="item-content">' + '<div class="item-content" id="{{labelId}}">' +
'<ng-content></ng-content>' + '<ng-content></ng-content>' +
'</div>' + '</div>' +
'<div class="item-media media-radio">' + '<div class="item-media media-radio">' +
@ -86,22 +97,36 @@ export class RadioGroup extends Ion {
}) })
export class RadioButton extends IonInputItem { export class RadioButton extends IonInputItem {
constructor( constructor(
@Ancestor() group: RadioGroup, @Ancestor() @Optional() group: RadioGroup,
elementRef: ElementRef, elementRef: ElementRef,
config: IonicConfig config: IonicConfig,
tapClick: TapClick
) { ) {
super(elementRef, config); super(elementRef, config);
this.tapClick = tapClick;
this.group = group; this.group = group;
this.tabIndex = 0;
} }
registerInput(input) { onInit() {
this.input = input; super.onInit();
this.group.registerButton(this); this.group.registerRadio(this);
this.labelId = 'label-' + this.id;
} }
//from clicking the label or switching inputs with keyboard click(ev) {
//view -> model (Control) if (this.tapClick.allowClick(ev)) {
toggle() { ev.preventDefault();
this.group.update(this.input); ev.stopPropagation();
this.check();
} }
}
check() {
this.checked = !this.checked;
this.group.update(this);
}
} }
let radioGroupIds = -1;

View File

@ -12,19 +12,16 @@
Fruits Fruits
</div> </div>
<ion-radio> <ion-radio value="apple" checked="true">
<label>Apple</label> Apple
<input value="apple" checked="true" type="radio">
</ion-radio> </ion-radio>
<ion-radio id="e2eBanana"> <ion-radio value="banana" id="e2eBanana">
<label>Banana</label> Banana
<input value="banana" type="radio">
</ion-radio> </ion-radio>
<ion-radio> <ion-radio value="cherry" id="my-id">
<label>Cherry</label> Cherry
<input value="cherry" type="radio">
</ion-radio> </ion-radio>
</ion-radio-group> </ion-radio-group>