Skip to content

Commit 026a570

Browse files
Introduce signal for easy retrieval of valid state
1 parent c638460 commit 026a570

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/main/webapp/app/shared/date-time-picker/date-time-picker.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#dateInput="ngModel"
2020
class="form-control position-relative ps-5"
2121
id="date-input-field"
22-
[ngClass]="{ 'is-invalid': error() || dateInput.invalid || (requiredField() && !dateInput.value) || warning(), 'border-warning': warning() }"
23-
[class.ng-invalid]="error() || dateInput.invalid || (requiredField() && !dateInput.value) || warning()"
22+
[ngClass]="{ 'is-invalid': isValid(), 'border-warning': warning() }"
23+
[class.ng-invalid]="!isValid()"
2424
[ngModel]="value"
2525
[disabled]="disabled()"
2626
[min]="minDate()"

src/main/webapp/app/shared/date-time-picker/date-time-picker.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, ViewChild, computed, forwardRef, input, output } from '@angular/core';
1+
import { Component, Input, ViewChild, computed, forwardRef, input, output, signal } from '@angular/core';
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgModel } from '@angular/forms';
33
import { faCalendarAlt, faCircleXmark, faClock, faGlobe, faQuestionCircle, faTriangleExclamation } from '@fortawesome/free-solid-svg-icons';
44
import dayjs from 'dayjs/esm';
@@ -37,13 +37,27 @@ export class FormDateTimePickerComponent implements ControlValueAccessor {
3737
shouldDisplayTimeZoneWarning = input<boolean>(true); // Displays a warning that the current time zone might differ from the participants'.
3838
valueChange = output<void>();
3939

40+
protected isInputValid = signal<boolean>(false);
41+
protected dateInputValue = signal<string>('');
42+
43+
isValid = computed(() => {
44+
const isInvalid = this.error() || !this.isInputValid() || (this.requiredField() && !this.dateInputValue()) || this.warning();
45+
return !isInvalid;
46+
});
47+
48+
private updateSignals(): void {
49+
this.isInputValid.set(!Boolean(this.dateInput.invalid));
50+
this.dateInputValue.set(this.dateInput.value);
51+
}
52+
4053
private onChange?: (val?: dayjs.Dayjs) => void;
4154

4255
/**
4356
* Emits the value change from component.
4457
*/
4558
valueChanged() {
4659
this.valueChange.emit();
60+
this.updateSignals();
4761
}
4862

4963
/**
@@ -57,6 +71,7 @@ export class FormDateTimePickerComponent implements ControlValueAccessor {
5771
} else {
5872
this.value = value;
5973
}
74+
this.updateSignals();
6075
}
6176

6277
/**
@@ -117,5 +132,6 @@ export class FormDateTimePickerComponent implements ControlValueAccessor {
117132
*/
118133
clearDate() {
119134
this.dateInput.reset(undefined);
135+
this.updateSignals();
120136
}
121137
}

0 commit comments

Comments
 (0)