From eb0d902c90bca5804fa73adf4f65055d1470fc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Fatay=20=C3=87ivici?= Date: Tue, 26 Apr 2016 18:25:18 +0300 Subject: [PATCH] Refactor and add change detection --- components/inputswitch/inputswitch.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/components/inputswitch/inputswitch.ts b/components/inputswitch/inputswitch.ts index 6f1e729bd2b..7dfa0d91601 100644 --- a/components/inputswitch/inputswitch.ts +++ b/components/inputswitch/inputswitch.ts @@ -67,6 +67,8 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit { private offLabelChild: any; private offset: any; + + initialized: boolean = false; constructor(private el: ElementRef, private domHandler: DomHandler) {} @@ -110,19 +112,27 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit { this.onContainer.style.width = 0 + 'px'; this.onLabelChild.style.marginLeft = -this.offset + 'px'; } + + this.initialized = true; } toggle(event,checkbox) { if(!this.disabled) { - checkbox.focus(); - this.checked = !this.checked; - this.checked ? this.checkUI() : this.uncheckUI(); + if(this.checked) { + this.checked = false; + this.uncheckUI() + } + else { + this.checked = true; + this.checkUI(); + } + this.onModelChange(this.checked); - this.onModelTouched(); this.onChange.emit({ originalEvent: event, checked: this.checked - }) + }); + checkbox.focus(); } } @@ -151,6 +161,13 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit { writeValue(value: any) : void { this.value = value; + + if(this.initialized) { + if(this.value === true) + this.checkUI(); + else + this.uncheckUI(); + } } registerOnChange(fn: Function): void {