diff --git a/projects/components/src/datetime-picker/datetime-picker.component.ts b/projects/components/src/datetime-picker/datetime-picker.component.ts index f5486ff36..46c4c40d6 100644 --- a/projects/components/src/datetime-picker/datetime-picker.component.ts +++ b/projects/components/src/datetime-picker/datetime-picker.component.ts @@ -1,4 +1,5 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core'; +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { Time, TypedSimpleChanges } from '@hypertrace/common'; import { InputAppearance } from '../input/input-appearance'; @@ -6,6 +7,13 @@ import { InputAppearance } from '../input/input-appearance'; selector: 'ht-datetime-picker', styleUrls: ['./datetime-picker.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, + providers: [ + { + provide: NG_VALUE_ACCESSOR, + multi: true, + useExisting: DatetimePickerComponent + } + ], template: `
@@ -21,17 +29,19 @@ import { InputAppearance } from '../input/input-appearance'; > - +
` }) -export class DatetimePickerComponent implements OnChanges { +export class DatetimePickerComponent implements ControlValueAccessor, OnChanges { @Input() public label?: string; @@ -41,6 +51,9 @@ export class DatetimePickerComponent implements OnChanges { @Input() public date?: Date = new Date(); + @Input() + public showDateOnly: boolean = false; + @Output() public readonly dateChange: EventEmitter = new EventEmitter(); @@ -52,10 +65,30 @@ export class DatetimePickerComponent implements OnChanges { } } + private propagateControlValueChange?: (value?: Date) => void; + private propagateControlValueChangeOnTouch?: (value?: Date) => void; + public getInputDate(): string { return this.date?.toISOString().slice(0, 10) ?? ''; } + public writeValue(value?: Date): void { + this.date = value; + } + + public registerOnChange(onChange: (value?: Date) => void): void { + this.propagateControlValueChange = onChange; + } + + public registerOnTouched(onTouch: (value?: Date) => void): void { + this.propagateControlValueChangeOnTouch = onTouch; + } + + private propagateValueChangeToFormControl(value?: Date): void { + this.propagateControlValueChange?.(value); + this.propagateControlValueChangeOnTouch?.(value); + } + private getInputTime(date: Date): Time { return new Time(date.getHours(), date.getMinutes()); } @@ -67,11 +100,13 @@ export class DatetimePickerComponent implements OnChanges { d.setFullYear(Number(yearMonthDay[0]), Number(yearMonthDay[1]) - 1, Number(yearMonthDay[2])); this.date = d; this.dateChange.emit(d); + this.propagateValueChangeToFormControl(d); } public onTimeChange(time: Time): void { this.time = time; this.date?.setUTCHours(time.hours, time.minutes, time.seconds, time.milliseconds); this.dateChange.emit(this.date); + this.propagateValueChangeToFormControl(this.date); } } diff --git a/projects/components/src/textarea/textarea.component.ts b/projects/components/src/textarea/textarea.component.ts index 828b7c8ee..edf4006a3 100644 --- a/projects/components/src/textarea/textarea.component.ts +++ b/projects/components/src/textarea/textarea.component.ts @@ -1,10 +1,18 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { LoggerService } from '@hypertrace/common'; @Component({ selector: 'ht-textarea', styleUrls: ['./textarea.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, + providers: [ + { + provide: NG_VALUE_ACCESSOR, + multi: true, + useExisting: TextareaComponent + } + ], template: `