Created
December 24, 2020 20:59
-
-
Save AnteaterKit/60dc5a30d67f6754603c476f898fe5c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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