1
- import { ChangeDetectionStrategy , ChangeDetectorRef , Component , HostBinding , Input , OnChanges , OnDestroy , OnInit , Output , SimpleChanges , ViewChild , inject } from '@angular/core' ;
1
+ import { ChangeDetectionStrategy , ChangeDetectorRef , Component , HostBinding , Input , OnDestroy , OnInit , Output , ViewChild , inject } from '@angular/core' ;
2
2
import { FormControl } from '@angular/forms' ;
3
3
import { MAT_DATE_FORMATS , DateAdapter , MatDateFormats } from '@angular/material/core' ;
4
4
import { ENTRY_MAT_DATE_TIME_FORMATS , EntryDateTimeAdapter , NgControlAccessorDirective , NoopControlValueAccessorDirective } from '@enigmatry/entry-components/common' ;
@@ -16,19 +16,30 @@ import { Subject, takeUntil } from 'rxjs';
16
16
changeDetection : ChangeDetectionStrategy . OnPush ,
17
17
standalone : false
18
18
} )
19
- export class EntryDateTimePickerComponent < D > implements OnInit , OnDestroy , OnChanges {
19
+ export class EntryDateTimePickerComponent < D > implements OnInit , OnDestroy {
20
20
@HostBinding ( 'class' ) class = 'entry-date-time-picker' ;
21
21
22
22
@Input ( ) label : string ;
23
23
@Input ( ) showSeconds : boolean ;
24
- @Input ( ) disabled : boolean ;
25
24
@Input ( ) min : D ;
26
25
@Input ( ) max : D ;
27
26
@Input ( ) placeholder : string | undefined ;
28
27
@Input ( ) hint : string | undefined ;
29
28
@Input ( ) defaultTime : D | undefined ;
30
29
@Output ( ) dateTimeChanged = new Subject < D > ( ) ;
31
30
31
+ _disabled : boolean ;
32
+
33
+ @Input ( )
34
+ get disabled ( ) : boolean {
35
+ return this . _disabled ;
36
+ }
37
+
38
+ set disabled ( value : boolean ) {
39
+ this . _disabled = value ;
40
+ this . setDisabled ( ) ;
41
+ }
42
+
32
43
private ngControlAccessor = inject ( NgControlAccessorDirective ) ;
33
44
private dateTimeAdapter : EntryDateTimeAdapter < D , unknown > = inject ( DateAdapter ) as EntryDateTimeAdapter < D , unknown > ;
34
45
private format : MatDateFormats = inject ( ENTRY_MAT_DATE_TIME_FORMATS ) ;
@@ -68,7 +79,7 @@ export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy, OnCha
68
79
69
80
ngOnInit ( ) : void {
70
81
this . calendarControl . setValue ( this . formControl . value , { emitEvent : false } ) ;
71
-
82
+ this . setDisabled ( ) ;
72
83
this . formControl . statusChanges
73
84
. pipe ( takeUntil ( this . $destroy ) )
74
85
. subscribe ( status => {
@@ -99,18 +110,18 @@ export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy, OnCha
99
110
} ) ;
100
111
}
101
112
102
- ngOnChanges ( _changes : SimpleChanges ) : void {
103
- if ( this . disabled ) {
104
- this . formControl . disable ( ) ;
105
- this . calendarControl . disable ( ) ;
106
- } else {
107
- this . formControl . enable ( ) ;
108
- this . calendarControl . enable ( ) ;
109
- }
110
- }
111
-
112
113
ngOnDestroy ( ) : void {
113
114
this . $destroy . next ( ) ;
114
115
this . $destroy . complete ( ) ;
115
116
}
117
+
118
+ private setDisabled ( ) {
119
+ if ( this . _disabled && this . formControl ?. enabled ) {
120
+ this . formControl ?. disable ( ) ;
121
+ this . calendarControl ?. disable ( { emitEvent : false } ) ;
122
+ } else if ( this . formControl ?. disabled ) {
123
+ this . formControl ?. enable ( ) ;
124
+ this . calendarControl ?. enable ( { emitEvent : false } ) ;
125
+ }
126
+ }
116
127
}
0 commit comments