/
checkbox.component.ts
51 lines (44 loc) · 1.41 KB
/
checkbox.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { NgZone, Component, Input, HostBinding, HostListener } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
/** @hidden */
@Component({
selector: 'checkbox',
template: `
<div class="form-check form-checkbox">
<input type="checkbox" class="form-check-input" [(ngModel)]='model'>
<label class="form-check-label">{{text}}</label>
</div>
`,
providers: [
{ provide: NG_VALUE_ACCESSOR, useExisting: CheckboxComponent, multi: true },
],
})
export class CheckboxComponent implements ControlValueAccessor {
@HostBinding('class.active') @Input() model: boolean
@HostBinding('class.disabled') @Input() disabled: boolean
@Input() text: string
private changed = new Array<(val: boolean) => void>()
@HostListener('click') click () {
NgZone.assertInAngularZone()
if (this.disabled) {
return
}
this.model = !this.model
for (const fx of this.changed) {
fx(this.model)
}
}
writeValue (obj: any) {
this.model = obj
}
registerOnChange (fn: any): void {
this.changed.push(fn)
}
registerOnTouched (fn: any): void {
this.changed.push(fn)
}
setDisabledState (isDisabled: boolean) {
this.disabled = isDisabled
}
}