Skip to content

Commit a06d7af

Browse files
authored
BP-1406: Date time picker > Changing min/max inputs causes date time to be recalculated and moved to next day (#248)
1 parent 2c7ad87 commit a06d7af

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

libs/entry-components/date-time-picker/date-time-picker.component.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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';
22
import { FormControl } from '@angular/forms';
33
import { MAT_DATE_FORMATS, DateAdapter, MatDateFormats } from '@angular/material/core';
44
import { ENTRY_MAT_DATE_TIME_FORMATS, EntryDateTimeAdapter, NgControlAccessorDirective, NoopControlValueAccessorDirective } from '@enigmatry/entry-components/common';
@@ -16,19 +16,30 @@ import { Subject, takeUntil } from 'rxjs';
1616
changeDetection: ChangeDetectionStrategy.OnPush,
1717
standalone: false
1818
})
19-
export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy, OnChanges {
19+
export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy {
2020
@HostBinding('class') class = 'entry-date-time-picker';
2121

2222
@Input() label: string;
2323
@Input() showSeconds: boolean;
24-
@Input() disabled: boolean;
2524
@Input() min: D;
2625
@Input() max: D;
2726
@Input() placeholder: string | undefined;
2827
@Input() hint: string | undefined;
2928
@Input() defaultTime: D | undefined;
3029
@Output() dateTimeChanged = new Subject<D>();
3130

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+
3243
private ngControlAccessor = inject(NgControlAccessorDirective);
3344
private dateTimeAdapter: EntryDateTimeAdapter<D, unknown> = inject(DateAdapter) as EntryDateTimeAdapter<D, unknown>;
3445
private format: MatDateFormats = inject(ENTRY_MAT_DATE_TIME_FORMATS);
@@ -68,7 +79,7 @@ export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy, OnCha
6879

6980
ngOnInit(): void {
7081
this.calendarControl.setValue(this.formControl.value, { emitEvent: false });
71-
82+
this.setDisabled();
7283
this.formControl.statusChanges
7384
.pipe(takeUntil(this.$destroy))
7485
.subscribe(status => {
@@ -99,18 +110,18 @@ export class EntryDateTimePickerComponent<D> implements OnInit, OnDestroy, OnCha
99110
});
100111
}
101112

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-
112113
ngOnDestroy(): void {
113114
this.$destroy.next();
114115
this.$destroy.complete();
115116
}
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+
}
116127
}

0 commit comments

Comments
 (0)