[AAE-5727] Add 'today' and 'now' as default values for date and datetime widgets (#7229)

This commit is contained in:
Pablo Martinez Garcia 2021-09-01 16:10:25 +02:00 committed by GitHub
parent 86d9a09c63
commit c54587d90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 0 deletions

View File

@ -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, {

View File

@ -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 */