Skip to content

Commit

Permalink
feat(checkbox): add change event
Browse files Browse the repository at this point in the history
Related #5513
  • Loading branch information
adamdbradley committed Feb 21, 2016
1 parent 5c677dc commit 2596731
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
21 changes: 17 additions & 4 deletions ionic/components/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, Optional, Input, HostListener, Provider, forwardRef} from 'angular2/core';
import {Component, Optional, Input, Output, EventEmitter, HostListener, Provider, forwardRef} from 'angular2/core';
import {NG_VALUE_ACCESSOR} from 'angular2/common';

import {Form} from '../../util/form';
Expand Down Expand Up @@ -72,6 +72,11 @@ export class Checkbox {
*/
id: string;

/**
* @output {Checkbox} expression to evaluate when the checkbox value changes
*/
@Output() change: EventEmitter<Checkbox> = new EventEmitter();

constructor(
private _form: Form,
@Optional() private _item: Item
Expand Down Expand Up @@ -113,8 +118,11 @@ export class Checkbox {
* @private
*/
private _setChecked(isChecked: boolean) {
this._checked = isChecked;
this._item && this._item.setCssClass('item-checkbox-checked', isChecked);
if (isChecked !== this._checked) {
this._checked = isChecked;
this.change.emit(this);
this._item && this._item.setCssClass('item-checkbox-checked', isChecked);
}
}

/**
Expand Down Expand Up @@ -158,7 +166,12 @@ export class Checkbox {
/**
* @private
*/
onChange(_) {}
onChange(isChecked: boolean) {
// used when this input does not have an ngModel or ngControl
console.debug('checkbox, onChange (no ngModel)', isChecked);
this._setChecked(isChecked);
this.onTouched();
}

/**
* @private
Expand Down
17 changes: 12 additions & 5 deletions ionic/components/checkbox/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class E2EApp {
fruitsForm: ControlGroup;
grapeDisabled: boolean;
grapeChecked: boolean;
kiwiModel: boolean;
strawberryModel: boolean;
kiwiValue: boolean;
strawberryValue: boolean;
standAloneChecked: boolean;
formResults: string;

Expand All @@ -35,9 +35,6 @@ class E2EApp {
this.grapeDisabled = true;
this.grapeChecked = true;
this.standAloneChecked = true;

this.kiwiModel = false;
this.strawberryModel = true;
}

toggleGrapeChecked() {
Expand All @@ -48,6 +45,16 @@ class E2EApp {
this.grapeDisabled = !this.grapeDisabled;
}

kiwiChange(ev) {
console.log('kiwiChange', ev);
this.kiwiValue = ev.checked;
}

strawberryChange(ev) {
console.log('strawberryChange', ev);
this.strawberryValue = ev.checked;
}

doSubmit(ev) {
console.log('Submitting form', this.fruitsForm.value);
this.formResults = JSON.stringify(this.fruitsForm.value);
Expand Down
12 changes: 6 additions & 6 deletions ionic/components/checkbox/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
</ion-item>

<ion-item>
<ion-label>Kiwi, NgModel false, Secondary color</ion-label>
<ion-checkbox secondary [(ngModel)]="kiwiModel"></ion-checkbox>
<ion-label>Kiwi, (change) Secondary color</ion-label>
<ion-checkbox secondary (change)="kiwiChange($event)"></ion-checkbox>
</ion-item>

<ion-item>
<ion-label>Strawberry, NgModel true</ion-label>
<ion-checkbox light [(ngModel)]="strawberryModel"></ion-checkbox>
<ion-label>Strawberry, (change) [checked]="true"</ion-label>
<ion-checkbox light (change)="strawberryChange($event)" [checked]="true"></ion-checkbox>
</ion-item>

</ion-list>
Expand All @@ -62,8 +62,8 @@
<code>cherry.value: {{fruitsForm.controls.cherryCtrl.value}}</code><br>
<code>grape.dirty: {{fruitsForm.controls.grapeCtrl.dirty}}</code><br>
<code>grape.value: {{fruitsForm.controls.grapeCtrl.value}}</code><br>
<code>kiwiModel: {{kiwiModel}}</code><br>
<code>strawberryModel: {{strawberryModel}}</code><br>
<code>kiwiValue: {{kiwiValue}}</code><br>
<code>strawberryValue: {{strawberryValue}}</code><br>
</p>

<pre aria-hidden="true" padding>{{formResults}}</pre>
Expand Down
2 changes: 1 addition & 1 deletion ionic/components/segment/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class SegmentButton {
@Input() value: string;

/**
* @output {any} expression to evaluate when a segment button has been clicked
* @output {SegmentButton} expression to evaluate when a segment button has been clicked
*/
@Output() select: EventEmitter<SegmentButton> = new EventEmitter();

Expand Down

0 comments on commit 2596731

Please sign in to comment.