From 4f79f80e519d3974083b141da63e43a54ec43290 Mon Sep 17 00:00:00 2001 From: "Achraf (abz)" Date: Wed, 30 Mar 2022 12:26:51 +0000 Subject: [PATCH] [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 --- .../src/legacy/js/fields/basic_fields.js | 8 ++- .../tests/legacy/fields/basic_fields_tests.js | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/legacy/js/fields/basic_fields.js b/addons/web/static/src/legacy/js/fields/basic_fields.js index 4b96079d8f7..c244adaa118 100644 --- a/addons/web/static/src/legacy/js/fields/basic_fields.js +++ b/addons/web/static/src/legacy/js/fields/basic_fields.js @@ -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(); } }, diff --git a/addons/web/static/tests/legacy/fields/basic_fields_tests.js b/addons/web/static/tests/legacy/fields/basic_fields_tests.js index ec1f1ca287b..b1f5ef2a24a 100644 --- a/addons/web/static/tests/legacy/fields/basic_fields_tests.js +++ b/addons/web/static/tests/legacy/fields/basic_fields_tests.js @@ -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:'
', + 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) {