Skip to content

Commit cd2afb3

Browse files
committed
fix(toggle): do not fire change when initializing
Closes #6144
1 parent 643d426 commit cd2afb3

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

ionic/components/checkbox/checkbox.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const CHECKBOX_VALUE_ACCESSOR = new Provider(
7070
})
7171
export class Checkbox {
7272
private _checked: boolean = false;
73+
private _init: boolean;
7374
private _disabled: boolean = false;
7475
private _labelId: string;
7576
private _fn: Function;
@@ -127,7 +128,9 @@ export class Checkbox {
127128
private _setChecked(isChecked: boolean) {
128129
if (isChecked !== this._checked) {
129130
this._checked = isChecked;
130-
this.change.emit(this);
131+
if (this._init) {
132+
this.change.emit(this);
133+
}
131134
this._item && this._item.setCssClass('item-checkbox-checked', isChecked);
132135
}
133136
}
@@ -185,6 +188,13 @@ export class Checkbox {
185188
*/
186189
onTouched() {}
187190

191+
/**
192+
* @private
193+
*/
194+
ngAfterContentInit() {
195+
this._init = true;
196+
}
197+
188198
/**
189199
* @private
190200
*/

ionic/components/toggle/test/basic/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class E2EApp {
4242
this.grapeDisabled = !this.grapeDisabled;
4343
}
4444

45+
appleChange(ev) {
46+
console.log('appleChange', ev);
47+
}
48+
49+
bananaChange(ev) {
50+
console.log('bananaChange', ev);
51+
}
52+
4553
kiwiChange(ev) {
4654
console.log('kiwiChange', ev);
4755
this.kiwiValue = ev.checked;

ionic/components/toggle/test/basic/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
<ion-item>
2121
<ion-label>Apple, ngControl</ion-label>
22-
<ion-toggle ngControl="appleCtrl"></ion-toggle>
22+
<ion-toggle ngControl="appleCtrl" (change)="appleChange($event)"></ion-toggle>
2323
</ion-item>
2424

2525
<ion-item>
2626
<ion-label>Banana, ngControl</ion-label>
27-
<ion-toggle ngControl="bananaCtrl"></ion-toggle>
27+
<ion-toggle ngControl="bananaCtrl" (change)="bananaChange($event)"></ion-toggle>
2828
</ion-item>
2929

3030
<ion-item>

ionic/components/toggle/toggle.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const TOGGLE_VALUE_ACCESSOR = new Provider(
8080
})
8181
export class Toggle implements ControlValueAccessor {
8282
private _checked: boolean = false;
83+
private _init: boolean;
8384
private _disabled: boolean = false;
8485
private _labelId: string;
8586
private _activated: boolean = false;
@@ -191,9 +192,12 @@ export class Toggle implements ControlValueAccessor {
191192
* @private
192193
*/
193194
private _setChecked(isChecked: boolean) {
195+
console.debug('_setChecked')
194196
if (isChecked !== this._checked) {
195197
this._checked = isChecked;
196-
this.change.emit(this);
198+
if (this._init) {
199+
this.change.emit(this);
200+
}
197201
this._item && this._item.setCssClass('item-toggle-checked', isChecked);
198202
}
199203
}
@@ -248,6 +252,13 @@ export class Toggle implements ControlValueAccessor {
248252
*/
249253
onTouched() {}
250254

255+
/**
256+
* @private
257+
*/
258+
ngAfterContentInit() {
259+
this._init = true;
260+
}
261+
251262
/**
252263
* @private
253264
*/

0 commit comments

Comments
 (0)