|
@@ -1,60 +1,17 @@
|
|
|
-import { Component, forwardRef, OnInit, OnChanges } from '@angular/core';
|
|
|
-import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
-import { CustomInputComponent } from './../../_abstract/custom-input.component';
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+import { NativeInputComponent } from '../../_abstract/native-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
|
|
|
- }
|
|
|
- ]
|
|
|
+ styleUrls: ['./clr-checkbox.component.scss']
|
|
|
})
|
|
|
-export class ClrCheckboxComponent extends CustomInputComponent implements OnInit, OnChanges {
|
|
|
+export class ClrCheckboxComponent extends NativeInputComponent {
|
|
|
|
|
|
- exposeMetaInTemplate: string[] = ['label', 'value', 'disabled', 'checkedValue'];
|
|
|
+ exposeMetaInTemplate: string[] = ['label'];
|
|
|
|
|
|
- label: string;
|
|
|
- value?: string | boolean = true;
|
|
|
- isChecked: boolean;
|
|
|
- disabled = false;
|
|
|
- currentValue: string | boolean;
|
|
|
- checkedValue: string | boolean = true;
|
|
|
- onChange: (val) => {};
|
|
|
-
|
|
|
- ngOnInit() {
|
|
|
- this.label = this.meta.label;
|
|
|
- if (this.meta.disabled) {
|
|
|
- this.control.disable();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- ngOnChanges() {
|
|
|
- this.disabled = this.meta.disabled;
|
|
|
+ setValue(cb: HTMLInputElement) {
|
|
|
+ this.control.setValue(cb.checked ? this.meta.checkedValue : false);
|
|
|
}
|
|
|
|
|
|
- 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;
|
|
|
- }
|
|
|
}
|