[FIX] web: Prevent datetime field from changing when pressing enter

When entering a date in a datetime field, it will be in the format
`08/01/2022 00:00:00`. If we place our cursor at the end of the
entry (after the hour) and each time we press enter, the timezone
offset is applied (so if offset is +1 it does +1 hour each time)

opw-2754116

closes odoo/odoo#87655

X-original-commit: 33e8540d64bb6e21debb7da9a121b706f551ab2e
Signed-off-by: Achraf <abz@odoo.com>
This commit is contained in:
Achraf (abz)
2022-04-04 10:25:26 +02:00
parent 8f9602c3a1
commit 4f79f80e51
2 changed files with 59 additions and 1 deletions
@@ -989,6 +989,7 @@ var FieldDate = InputField.extend({
/**
* Confirm the value on hit enter and re-render
* It will also remove the offset to get the UTC value
*
* @private
* @override
@@ -997,7 +998,12 @@ var FieldDate = InputField.extend({
async _onKeydown(ev) {
this._super(...arguments);
if (ev.which === $.ui.keyCode.ENTER) {
await this._setValue(this.$input.val());
let value = this.$input.val();
try {
value = this._parseValue(value);
value.add(-this.getSession().getTZOffset(value), "minutes");
} catch (_err) {}
await this._setValue(value);
this._render();
}
},
@@ -5136,6 +5136,58 @@ QUnit.module('basic_fields', {
form.destroy();
});
QUnit.test('datetime field: hit enter should update value', async function (assert) {
/*
This test verifies that the field datetime is correctly computed when:
- we press enter to validate our entry
- we click outside the field to validate our entry
- we save
*/
assert.expect(3);
const form = await createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form string="Partners"><field name="datetime"/></form>',
res_id: 1,
translateParameters: { // Avoid issues due to localization formats
date_format: '%m/%d/%Y',
time_format: '%H:%M:%S',
},
viewOptions: {
mode: 'edit',
},
session: {
getTZOffset: function () {
return 120;
},
},
});
const datetime = form.el.querySelector('input[name="datetime"]');
// Enter a beginning of date and press enter to validate
await testUtils.fields.editInput(datetime, '01/08/22 14:30:40');
await testUtils.fields.triggerKeydown(datetime, 'enter');
const datetimeValue = `01/08/2022 14:30:40`;
assert.strictEqual(datetime.value, datetimeValue);
// Click outside the field to check that the field is not changed
await testUtils.dom.click(form.$el);
assert.strictEqual(datetime.value, datetimeValue);
// Save and check that it's still ok
await testUtils.form.clickSave(form);
const { textContent } = form.el.querySelector('span[name="datetime"]')
assert.strictEqual(textContent, datetimeValue);
form.destroy();
});
QUnit.module('RemainingDays');
QUnit.test('remaining_days widget on a date field in list view', async function (assert) {