11import { ChangeDetectionStrategy , Component , EventEmitter , Input , OnChanges , Output } from '@angular/core' ;
2+ import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
23import { Time , TypedSimpleChanges } from '@hypertrace/common' ;
34import { InputAppearance } from '../input/input-appearance' ;
45
56@Component ( {
67 selector : 'ht-datetime-picker' ,
78 styleUrls : [ './datetime-picker.component.scss' ] ,
89 changeDetection : ChangeDetectionStrategy . OnPush ,
10+ providers : [
11+ {
12+ provide : NG_VALUE_ACCESSOR ,
13+ multi : true ,
14+ useExisting : DatetimePickerComponent
15+ }
16+ ] ,
917 template : `
1018 <div class="datetime-picker">
1119 <ht-label *ngIf="this.label" [label]="this.label" class="datetime-label"></ht-label>
@@ -21,17 +29,19 @@ import { InputAppearance } from '../input/input-appearance';
2129 >
2230 </ht-input>
2331
24- <ht-time-picker
25- class="time-selector"
26- [time]="this.time"
27- [showTimeTriggerIcon]="this.showTimeTriggerIcon"
28- (timeChange)="this.onTimeChange($event)"
29- ></ht-time-picker>
32+ <ng-container *ngIf="!this.showDateOnly"
33+ ><ht-time-picker
34+ class="time-selector"
35+ [time]="this.time"
36+ [showTimeTriggerIcon]="this.showTimeTriggerIcon"
37+ (timeChange)="this.onTimeChange($event)"
38+ ></ht-time-picker
39+ ></ng-container>
3040 </div>
3141 </div>
3242 `
3343} )
34- export class DatetimePickerComponent implements OnChanges {
44+ export class DatetimePickerComponent implements ControlValueAccessor , OnChanges {
3545 @Input ( )
3646 public label ?: string ;
3747
@@ -41,6 +51,9 @@ export class DatetimePickerComponent implements OnChanges {
4151 @Input ( )
4252 public date ?: Date = new Date ( ) ;
4353
54+ @Input ( )
55+ public showDateOnly : boolean = false ;
56+
4457 @Output ( )
4558 public readonly dateChange : EventEmitter < Date > = new EventEmitter ( ) ;
4659
@@ -52,10 +65,30 @@ export class DatetimePickerComponent implements OnChanges {
5265 }
5366 }
5467
68+ private propagateControlValueChange ?: ( value ?: Date ) => void ;
69+ private propagateControlValueChangeOnTouch ?: ( value ?: Date ) => void ;
70+
5571 public getInputDate ( ) : string {
5672 return this . date ?. toISOString ( ) . slice ( 0 , 10 ) ?? '' ;
5773 }
5874
75+ public writeValue ( value ?: Date ) : void {
76+ this . date = value ;
77+ }
78+
79+ public registerOnChange ( onChange : ( value ?: Date ) => void ) : void {
80+ this . propagateControlValueChange = onChange ;
81+ }
82+
83+ public registerOnTouched ( onTouch : ( value ?: Date ) => void ) : void {
84+ this . propagateControlValueChangeOnTouch = onTouch ;
85+ }
86+
87+ private propagateValueChangeToFormControl ( value ?: Date ) : void {
88+ this . propagateControlValueChange ?.( value ) ;
89+ this . propagateControlValueChangeOnTouch ?.( value ) ;
90+ }
91+
5992 private getInputTime ( date : Date ) : Time {
6093 return new Time ( date . getHours ( ) , date . getMinutes ( ) ) ;
6194 }
@@ -67,11 +100,13 @@ export class DatetimePickerComponent implements OnChanges {
67100 d . setFullYear ( Number ( yearMonthDay [ 0 ] ) , Number ( yearMonthDay [ 1 ] ) - 1 , Number ( yearMonthDay [ 2 ] ) ) ;
68101 this . date = d ;
69102 this . dateChange . emit ( d ) ;
103+ this . propagateValueChangeToFormControl ( d ) ;
70104 }
71105
72106 public onTimeChange ( time : Time ) : void {
73107 this . time = time ;
74108 this . date ?. setUTCHours ( time . hours , time . minutes , time . seconds , time . milliseconds ) ;
75109 this . dateChange . emit ( this . date ) ;
110+ this . propagateValueChangeToFormControl ( this . date ) ;
76111 }
77112}
0 commit comments