From c54587d90d2753c79879f57a46f72d727bcb5d49 Mon Sep 17 00:00:00 2001 From: Pablo Martinez Garcia Date: Wed, 1 Sep 2021 16:10:25 +0200 Subject: [PATCH] [AAE-5727] Add 'today' and 'now' as default values for date and datetime widgets (#7229) --- .../widgets/core/form-field.model.spec.ts | 63 +++++++++++++++++++ .../widgets/core/form-field.model.ts | 8 +++ 2 files changed, 71 insertions(+) diff --git a/lib/core/form/components/widgets/core/form-field.model.spec.ts b/lib/core/form/components/widgets/core/form-field.model.spec.ts index 53b079b3a7..3ad1392b4e 100644 --- a/lib/core/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/form/components/widgets/core/form-field.model.spec.ts @@ -15,6 +15,7 @@ * limitations under the License. */ +import moment from 'moment'; import { FormFieldTypes } from './form-field-types'; import { FormFieldModel } from './form-field.model'; import { FormModel } from './form.model'; @@ -249,6 +250,68 @@ describe('FormFieldModel', () => { expect(field.value).toBe('28-04-2017'); }); + it('should set the value to today\'s date when the value is today', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'ddmmyyy', + name: 'DD-MM-YYYY', + type: 'date', + value: 'today', + required: false, + readOnly: false, + params: { + field: { + id: 'ddmmyyy', + name: 'DD-MM-YYYY', + type: 'date', + value: 'today', + required: false, + readOnly: false + } + }, + dateDisplayFormat: 'DD-MM-YYYY' + }); + + const currentDate = moment(new Date()); + const expectedDate = moment(currentDate).format('DD-MM-YYYY'); + const expectedDateFormat = `${currentDate.format('YYYY-MM-DD')}T00:00:00.000Z`; + + expect(field.value).toBe(expectedDate); + expect(form.values['ddmmyyy']).toEqual(expectedDateFormat); + }); + + it('should set the value to now date time when the value is now', () => { + const form = new FormModel(); + const field = new FormFieldModel(form, { + fieldType: 'FormFieldRepresentation', + id: 'datetime', + name: 'date and time', + type: 'datetime', + value: 'now', + required: false, + readOnly: false, + params: { + field: { + id: 'datetime', + name: 'date and time', + type: 'datetime', + value: 'now', + required: false, + readOnly: false + } + }, + dateDisplayFormat: 'YYYY-MM-DD HH:mm' + }); + + const currentDateTime = moment(new Date()); + const expectedDateTime = moment(currentDateTime).format('YYYY-MM-DD HH:mm'); + const expectedDateTimeFormat = `${currentDateTime.utc().format('YYYY-MM-DDTHH:mm:00')}.000Z`; + + expect(field.value).toBe(expectedDateTime); + expect(form.values['datetime']).toEqual(expectedDateTimeFormat); + }); + it('should parse the checkbox set to "true" when it is readonly', () => { const form = new FormModel(); const field = new FormFieldModel(form, { diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index 78185fedb8..9a9f09fcb5 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -380,6 +380,10 @@ export class FormFieldModel extends FormWidgetModel { } break; case FormFieldTypes.DATE: + if (typeof this.value === 'string' && this.value === 'today') { + this.value = moment(new Date()).format(this.dateDisplayFormat); + } + const dateValue = moment(this.value, this.dateDisplayFormat, true); if (dateValue && dateValue.isValid()) { this.form.values[this.id] = `${dateValue.format('YYYY-MM-DD')}T00:00:00.000Z`; @@ -389,6 +393,10 @@ export class FormFieldModel extends FormWidgetModel { } break; case FormFieldTypes.DATETIME: + if (typeof this.value === 'string' && this.value === 'now') { + this.value = moment(new Date()).format(this.dateDisplayFormat); + } + const dateTimeValue = moment(this.value, this.dateDisplayFormat, true).utc(); if (dateTimeValue && dateTimeValue.isValid()) { /* cspell:disable-next-line */