|
@@ -1,4 +1,4 @@
|
|
|
-import { Component, forwardRef, OnChanges } from '@angular/core';
|
|
|
+import { Component, OnChanges, forwardRef, ChangeDetectorRef } from '@angular/core';
|
|
|
import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
import { CustomInputComponent } from './../../_abstract/custom-input.component';
|
|
|
|
|
@@ -18,20 +18,27 @@ export class CheckbuttonComponent extends CustomInputComponent implements OnChan
|
|
|
|
|
|
exposeMetaInTemplate: string[] = ['label', 'value', 'disabled', 'checkedValue', 'onChange'];
|
|
|
|
|
|
+ label: string;
|
|
|
value?: string | boolean = true;
|
|
|
isChecked: boolean;
|
|
|
disabled = false;
|
|
|
- currentValue: string | boolean;
|
|
|
- checkedValue: string | boolean = true;
|
|
|
- onChange: (val) => {};
|
|
|
+ currentValue: string | number | boolean;
|
|
|
+ checkedValue: string | number | boolean = true;
|
|
|
+ onChange: (val) => void;
|
|
|
+
|
|
|
+ constructor(private _cdr: ChangeDetectorRef) {
|
|
|
+ super();
|
|
|
+ }
|
|
|
|
|
|
ngOnChanges() {
|
|
|
this.disabled = this.meta.disabled;
|
|
|
}
|
|
|
|
|
|
- private toggleChecked(e): void {
|
|
|
- e.target.blur();
|
|
|
- e.preventDefault();
|
|
|
+ private toggleChecked(e?): void {
|
|
|
+ if (e) {
|
|
|
+ e.target.blur();
|
|
|
+ e.preventDefault();
|
|
|
+ }
|
|
|
if (this.disabled) { return; }
|
|
|
this.isChecked = !this.isChecked;
|
|
|
this.currentValue = this.isChecked ? this.value || this.checkedValue : false;
|
|
@@ -46,9 +53,9 @@ export class CheckbuttonComponent extends CustomInputComponent implements OnChan
|
|
|
}
|
|
|
|
|
|
public writeValue(value: any): void {
|
|
|
- // this.isChecked = (value && value !== 0 && value !== 'false') ? true : false;
|
|
|
+ value = value ? this.checkedValue : false;
|
|
|
this.isChecked = !!value;
|
|
|
- console.log(this.isChecked);
|
|
|
- this.currentValue = this.isChecked ? this.value || this.checkedValue : false;
|
|
|
+ this.currentValue = this.isChecked ? this.checkedValue : false;
|
|
|
+ this._cdr.markForCheck(); // We have to manually trigger change detection when using setVAlue or patchValue from outside this component
|
|
|
}
|
|
|
}
|