[AAE-9298] FE ADF Date widget handle dynamic date values (#7689)

* [AAE-9298] Add dynamic date fields to FormFieldModel

* [AAE-9298] Change dynamic date range types from string to integer

* [AAE-9298] If dynamicDateRangeSelection is true, it will evaluate minDateRangeValue and maxDateRangeValue to set min and max date if are set

* [AAE-9298] Check if date range values are numbers
This commit is contained in:
Amedeo Lepore
2022-06-28 09:19:13 +02:00
committed by GitHub
parent b14332d271
commit e529d3bd74
3 changed files with 89 additions and 5 deletions

View File

@@ -54,6 +54,9 @@ export class FormFieldModel extends FormWidgetModel {
maxLength: number = 0; maxLength: number = 0;
minValue: string; minValue: string;
maxValue: string; maxValue: string;
maxDateRangeValue: number = 0;
minDateRangeValue: number = 0;
dynamicDateRangeSelection: boolean;
regexPattern: string; regexPattern: string;
options: FormFieldOption[] = []; options: FormFieldOption[] = [];
restUrl: string; restUrl: string;
@@ -167,6 +170,9 @@ export class FormFieldModel extends FormWidgetModel {
this.maxLength = json.maxLength || 0; this.maxLength = json.maxLength || 0;
this.minValue = json.minValue; this.minValue = json.minValue;
this.maxValue = json.maxValue; this.maxValue = json.maxValue;
this.minDateRangeValue = json.minDateRangeValue;
this.maxDateRangeValue = json.maxDateRangeValue;
this.dynamicDateRangeSelection = json.dynamicDateRangeSelection;
this.regexPattern = json.regexPattern; this.regexPattern = json.regexPattern;
this.options = json.options || []; this.options = json.options || [];
this.hasEmptyValue = json.hasEmptyValue; this.hasEmptyValue = json.hasEmptyValue;

View File

@@ -302,4 +302,72 @@ describe('DateWidgetComponent', () => {
}); });
}); });
describe('Set dynamic dates', () => {
it('should min date equal to the today date minus minimum date range value', async () => {
widget.field = new FormFieldModel(null, {
dynamicDateRangeSelection: true,
minDateRangeValue: 4
});
fixture.detectChanges();
await fixture.whenStable();
const todayDate = moment().format(DATE_FORMAT_CLOUD);
const expected = moment(todayDate).subtract(widget.field.minDateRangeValue, 'days');
expect(widget.minDate).toEqual(expected);
});
it('should min date and max date be undefined if dynamic min and max date are not set', async () => {
widget.field = new FormFieldModel(null, {
dynamicDateRangeSelection: true
});
fixture.detectChanges();
await fixture.whenStable();
expect(widget.minDate).toBeUndefined();
expect(widget.maxDate).toBeUndefined();
});
it('should max date be undefined if only minimum date range value is set', async () => {
widget.field = new FormFieldModel(null, {
dynamicDateRangeSelection: true,
minDateRangeValue: 4
});
fixture.detectChanges();
await fixture.whenStable();
expect(widget.maxDate).toBeUndefined();
});
it('should min date be undefined if only maximum date range value is set', async () => {
widget.field = new FormFieldModel(null, {
dynamicDateRangeSelection: true,
maxDateRangeValue: 4
});
fixture.detectChanges();
await fixture.whenStable();
expect(widget.minDate).toBeUndefined();
});
it('should max date equal to the today date plus maximum date range value', async () => {
widget.field = new FormFieldModel(null, {
dynamicDateRangeSelection: true,
maxDateRangeValue: 5
});
fixture.detectChanges();
await fixture.whenStable();
const todayDate = moment().format(DATE_FORMAT_CLOUD);
const expected = moment(todayDate).add(widget.field.maxDateRangeValue, 'days');
expect(widget.maxDate).toEqual(expected);
});
});
}); });

View File

@@ -74,6 +74,15 @@ export class DateCloudWidgetComponent extends WidgetComponent implements OnInit,
momentDateAdapter.overrideDisplayFormat = this.field.dateDisplayFormat; momentDateAdapter.overrideDisplayFormat = this.field.dateDisplayFormat;
if (this.field) { if (this.field) {
if (this.field.dynamicDateRangeSelection) {
const today = moment().format(DATE_FORMAT_CLOUD);
if (!isNaN(this.field.minDateRangeValue)) {
this.minDate = moment(today).subtract(this.field.minDateRangeValue, 'days');
}
if (!isNaN(this.field.maxDateRangeValue)) {
this.maxDate = moment(today).add(this.field.maxDateRangeValue, 'days');
}
} else {
if (this.field.minValue) { if (this.field.minValue) {
this.minDate = moment(this.field.minValue, DATE_FORMAT_CLOUD); this.minDate = moment(this.field.minValue, DATE_FORMAT_CLOUD);
} }
@@ -83,6 +92,7 @@ export class DateCloudWidgetComponent extends WidgetComponent implements OnInit,
} }
} }
} }
}
ngOnDestroy() { ngOnDestroy() {
this.onDestroy$.next(true); this.onDestroy$.next(true);