1
- import { ChangeDetectionStrategy , Component , EventEmitter , Input , Output } from '@angular/core' ;
1
+ import { ChangeDetectionStrategy , ChangeDetectorRef , Component , EventEmitter , Input , Output } from '@angular/core' ;
2
+ import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
2
3
import { MatCheckboxChange } from '@angular/material/checkbox' ;
3
4
4
5
@Component ( {
@@ -8,31 +9,78 @@ import { MatCheckboxChange } from '@angular/material/checkbox';
8
9
template : `
9
10
<mat-checkbox
10
11
labelPosition="after"
11
- [checked]="this.checked "
12
- [disabled]="this.disabled "
13
- (change)="onCheckboxChange($event)"
12
+ [checked]="this.isChecked "
13
+ [disabled]="this.isDisabled "
14
+ (change)="this. onCheckboxChange($event)"
14
15
class="ht-checkbox"
15
- [ngClass]="{ disabled: this.disabled }"
16
+ [ngClass]="{ disabled: this.isDisabled }"
16
17
>
17
18
<ht-label class="label" *ngIf="this.label !== undefined && this.label !== ''" [label]="this.label"></ht-label>
18
19
</mat-checkbox>
19
- `
20
+ ` ,
21
+ providers : [
22
+ {
23
+ provide : NG_VALUE_ACCESSOR ,
24
+ useExisting : CheckboxComponent ,
25
+ multi : true
26
+ }
27
+ ]
20
28
} )
21
- export class CheckboxComponent {
29
+ export class CheckboxComponent implements ControlValueAccessor {
22
30
@Input ( )
23
31
public label ?: string ;
24
32
25
33
@Input ( )
26
- public checked : boolean | undefined ;
34
+ public set checked ( checked : boolean | undefined ) {
35
+ this . isChecked = checked ?? false ;
36
+ }
37
+
38
+ public get checked ( ) : boolean {
39
+ return this . isChecked ;
40
+ }
27
41
28
42
@Input ( )
29
- public disabled : boolean | undefined ;
43
+ public set disabled ( disabled : boolean | undefined ) {
44
+ this . isDisabled = disabled ?? false ;
45
+ }
46
+
47
+ public get disabled ( ) : boolean {
48
+ return this . isDisabled ;
49
+ }
30
50
31
51
@Output ( )
32
52
public readonly checkedChange : EventEmitter < boolean > = new EventEmitter ( ) ;
33
53
54
+ public isChecked : boolean = false ;
55
+ public isDisabled : boolean = false ;
56
+
57
+ private onTouched ! : ( ) => void ;
58
+ private onChanged ! : ( value : boolean ) => void ;
59
+
60
+ public constructor ( private readonly cdr : ChangeDetectorRef ) { }
61
+
34
62
public onCheckboxChange ( event : MatCheckboxChange ) : void {
35
- this . checked = event . checked ;
36
- this . checkedChange . emit ( this . checked ) ;
63
+ this . isChecked = event . checked ;
64
+ this . checkedChange . emit ( this . isChecked ) ;
65
+ this . onChanged ( this . isChecked ) ;
66
+ this . onTouched ( ) ;
67
+ }
68
+
69
+ public registerOnChange ( fn : ( value : boolean ) => void ) : void {
70
+ this . onChanged = fn ;
71
+ }
72
+
73
+ public registerOnTouched ( fn : ( ) => void ) : void {
74
+ this . onTouched = fn ;
75
+ }
76
+
77
+ public setDisabledState ( isDisabled : boolean ) : void {
78
+ this . isDisabled = isDisabled ;
79
+ this . cdr . markForCheck ( ) ;
80
+ }
81
+
82
+ public writeValue ( isChecked : boolean | undefined ) : void {
83
+ this . isChecked = isChecked ?? false ;
84
+ this . cdr . markForCheck ( ) ;
37
85
}
38
86
}
0 commit comments