mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
[AAE-5727] Add 'today' and 'now' as default values for date and datetime widgets (#7229)
This commit is contained in:
parent
86d9a09c63
commit
c54587d90d
@ -15,6 +15,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import moment from 'moment';
|
||||||
import { FormFieldTypes } from './form-field-types';
|
import { FormFieldTypes } from './form-field-types';
|
||||||
import { FormFieldModel } from './form-field.model';
|
import { FormFieldModel } from './form-field.model';
|
||||||
import { FormModel } from './form.model';
|
import { FormModel } from './form.model';
|
||||||
@ -249,6 +250,68 @@ describe('FormFieldModel', () => {
|
|||||||
expect(field.value).toBe('28-04-2017');
|
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', () => {
|
it('should parse the checkbox set to "true" when it is readonly', () => {
|
||||||
const form = new FormModel();
|
const form = new FormModel();
|
||||||
const field = new FormFieldModel(form, {
|
const field = new FormFieldModel(form, {
|
||||||
|
@ -380,6 +380,10 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case FormFieldTypes.DATE:
|
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);
|
const dateValue = moment(this.value, this.dateDisplayFormat, true);
|
||||||
if (dateValue && dateValue.isValid()) {
|
if (dateValue && dateValue.isValid()) {
|
||||||
this.form.values[this.id] = `${dateValue.format('YYYY-MM-DD')}T00:00:00.000Z`;
|
this.form.values[this.id] = `${dateValue.format('YYYY-MM-DD')}T00:00:00.000Z`;
|
||||||
@ -389,6 +393,10 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case FormFieldTypes.DATETIME:
|
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();
|
const dateTimeValue = moment(this.value, this.dateDisplayFormat, true).utc();
|
||||||
if (dateTimeValue && dateTimeValue.isValid()) {
|
if (dateTimeValue && dateTimeValue.isValid()) {
|
||||||
/* cspell:disable-next-line */
|
/* cspell:disable-next-line */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user