Skip to content

Instantly share code, notes, and snippets.

@AnteaterKit
Created December 24, 2020 20:59
Show Gist options
  • Save AnteaterKit/60dc5a30d67f6754603c476f898fe5c7 to your computer and use it in GitHub Desktop.
Save AnteaterKit/60dc5a30d67f6754603c476f898fe5c7 to your computer and use it in GitHub Desktop.
export class CheckboxComponent implements OnInit , ControlValueAccessor, OnDestroy, AfterViewInit {
constructor(private cdr: ChangeDetectorRef) { }
checked = false;
disabled = false;
@Output() readonly сheckedChange = new EventEmitter<boolean>();
onChange = (e) => {};
onTouched = () => {};
ngAfterViewInit(): void {
throw new Error('Method not implemented.');
}
ngOnDestroy(): void {
throw new Error('Method not implemented.');
}
writeValue(value: any): void {
this.checked = value;
this.cdr.markForCheck();
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
this.cdr.markForCheck();
}
hostClick(e: MouseEvent): void {
e.preventDefault();
this.updateCheckedChange(!this.checked);
}
updateCheckedChange(checked: boolean): void {
if (!this.disabled) {
this.checked = checked;
this.onChange(this.checked);
this.сheckedChange.emit(this.checked);
}
}
ngOnInit(): void {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment