projects/rebirth-ng/src/lib/checkbox-group/checkbox.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
exportAs | checkbox |
host | { |
providers |
{
: , : (() => ), : true
}
|
selector | re-checkbox |
styleUrls | checkbox.component.scss |
templateUrl | ./checkbox.component.html |
Properties |
Methods |
Inputs |
Outputs |
constructor(changeDetectorRef: ChangeDetectorRef)
|
||||||
Parameters :
|
checkedValue
|
Type:
Default value: |
cssClass
|
Type: |
disabled
|
Default value: |
label
|
Type: |
unCheckedValue
|
Type:
Default value: |
valueChange
|
$event type: EventEmitter
|
isChecked |
isChecked()
|
Returns :
boolean
|
onCheckBoxChange | ||||
onCheckBoxChange(checkbox: )
|
||||
Parameters :
Returns :
void
|
registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Parameters :
Returns :
void
|
registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
Parameters :
Returns :
void
|
setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
Parameters :
Returns :
void
|
writeValue | ||||||
writeValue(obj: any)
|
||||||
Parameters :
Returns :
void
|
Private onChange |
onChange:
|
Default value : (_: any) => null
|
Private onTouched |
onTouched:
|
Default value : () => null
|
value |
value:
|
Type : any
|
import {
Component, ChangeDetectionStrategy, Input, ChangeDetectorRef, forwardRef, Output,
EventEmitter
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
@Component({
selector: 're-checkbox',
templateUrl: './checkbox.component.html',
styleUrls: ['./checkbox.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
exportAs: 'checkbox',
host: { '[class]': '(cssClass ? cssClass : "") + " checkbox"' },
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxComponent),
multi: true
}]
})
export class CheckboxComponent implements ControlValueAccessor {
@Input() disabled = false;
@Input() cssClass: string;
@Input() label: string;
@Input() checkedValue: any = true;
@Input() unCheckedValue: any = false;
@Output() valueChange = new EventEmitter<any>();
value: any;
private onChange = (_: any) => null;
private onTouched = () => null;
constructor(private changeDetectorRef: ChangeDetectorRef) {
}
writeValue(obj: any): void {
this.value = obj;
this.changeDetectorRef.markForCheck();
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
onCheckBoxChange(checkbox) {
this.onTouched();
if (checkbox.checked) {
this.value = this.checkedValue;
} else {
this.value = this.unCheckedValue;
}
this.onChange(this.value);
this.valueChange.emit(this.value);
}
isChecked() {
return this.value === this.checkedValue;
}
}
<label>
<input #checkbox type="checkbox" [checked]="isChecked()" [disabled]="disabled"
(change)="onCheckBoxChange(checkbox)">{{label}}
</label>