|
@@ -0,0 +1,58 @@
|
|
|
+import { Component, forwardRef, OnInit, OnChanges } from '@angular/core';
|
|
|
+import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
+import { CustomInputComponent } from './../../_abstract/custom-input.component';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-checkbox',
|
|
|
+ templateUrl: './clr-checkbox.component.html',
|
|
|
+ styleUrls: ['./clr-checkbox.component.scss'],
|
|
|
+ providers: [
|
|
|
+ {
|
|
|
+ provide: NG_VALUE_ACCESSOR,
|
|
|
+ useExisting: forwardRef(() => ClrCheckboxComponent),
|
|
|
+ multi: true
|
|
|
+ }
|
|
|
+ ]
|
|
|
+})
|
|
|
+export class ClrCheckboxComponent extends CustomInputComponent implements OnInit, OnChanges {
|
|
|
+
|
|
|
+ exposeMetaInTemplate: string[] = ['label', 'value', 'disabled', 'checkedValue'];
|
|
|
+
|
|
|
+ value?: string | boolean = true;
|
|
|
+ isChecked: boolean;
|
|
|
+ disabled = false;
|
|
|
+ currentValue: string | boolean;
|
|
|
+ checkedValue: string | boolean = true;
|
|
|
+ onChange: (val) => {};
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ if (this.meta.disabled) {
|
|
|
+ this.control.disable();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnChanges() {
|
|
|
+ this.disabled = this.meta.disabled;
|
|
|
+ }
|
|
|
+
|
|
|
+ private toggleChecked(e): void {
|
|
|
+ e.target.blur();
|
|
|
+ e.preventDefault();
|
|
|
+ if (this.disabled) { return; }
|
|
|
+ this.isChecked = !this.isChecked;
|
|
|
+ this.currentValue = this.isChecked ? this.value || this.checkedValue : false;
|
|
|
+ if (typeof this.onChange === 'function') {
|
|
|
+ try {
|
|
|
+ this.onChange(this.currentValue);
|
|
|
+ } catch (e) {
|
|
|
+ console.log('ERROR in onChange function');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.propagateChange(this.currentValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public writeValue(value: any): void {
|
|
|
+ this.isChecked = value && value !== 0 && value !== 'false' ? value : false;
|
|
|
+ this.currentValue = this.isChecked ? value : false;
|
|
|
+ }
|
|
|
+}
|