From a449b05221d454afc7254be0fc8b84d7ff7ffc2f Mon Sep 17 00:00:00 2001 From: Pierre Rousseau Date: Thu, 30 Mar 2023 08:25:32 +0000 Subject: [PATCH] [FIX] web: allow to disable autosave on boolean toggle Since https://github.com/odoo/odoo/commit/28e6b7eb83e4309c4e70974d947194bdca240865, the boolean toggle widget trigger a save when the user clicks on it. This is a problem when the widget is used in a form view opened from a o2m list, because the record updated is saved immediately, even if the user clicks on the discard button. This commit adds a new option to disable the autosave on the boolean toggle widget. To disable the autosave, add `autosave: false` in the options on the field definition. Task-id 3235962 closes odoo/odoo#120000 X-original-commit: b56e58dff3258698dbbf173bc6790a35a47131b0 Signed-off-by: Aaron Bohy (aab) --- .../boolean_toggle/boolean_toggle_field.js | 11 ++++++++-- .../list_boolean_toggle_field.js | 4 +++- .../fields/boolean_toggle_field_tests.js | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/views/fields/boolean_toggle/boolean_toggle_field.js b/addons/web/static/src/views/fields/boolean_toggle/boolean_toggle_field.js index 8f199b22bf5..77b0cd4fece 100644 --- a/addons/web/static/src/views/fields/boolean_toggle/boolean_toggle_field.js +++ b/addons/web/static/src/views/fields/boolean_toggle/boolean_toggle_field.js @@ -6,10 +6,16 @@ import { booleanField, BooleanField } from "../boolean/boolean_field"; export class BooleanToggleField extends BooleanField { static template = "web.BooleanToggleField"; + static props = { + ...BooleanField.props, + autosave: { type: Boolean, optional: true }, + }; async onChange(newValue) { await this.props.record.update({ [this.props.name]: newValue }); - return this.props.record.save(); + if (this.props.autosave) { + return this.props.record.save(); + } } } @@ -17,8 +23,9 @@ export const booleanToggleField = { ...booleanField, component: BooleanToggleField, displayName: _lt("Toggle"), - extractProps(fieldInfo, dynamicInfo) { + extractProps({ options }, dynamicInfo) { return { + autosave: "autosave" in options ? Boolean(options.autosave) : true, readonly: dynamicInfo.readonly, }; }, diff --git a/addons/web/static/src/views/fields/boolean_toggle/list_boolean_toggle_field.js b/addons/web/static/src/views/fields/boolean_toggle/list_boolean_toggle_field.js index 3da22ac6477..cb62d2766bc 100644 --- a/addons/web/static/src/views/fields/boolean_toggle/list_boolean_toggle_field.js +++ b/addons/web/static/src/views/fields/boolean_toggle/list_boolean_toggle_field.js @@ -11,7 +11,9 @@ export class ListBooleanToggleField extends BooleanToggleField { await this.props.record.update({ [this.props.name]: !this.props.record.data[this.props.name], }); - return this.props.record.save(); + if (this.props.autosave) { + return this.props.record.save(); + } } } } diff --git a/addons/web/static/tests/views/fields/boolean_toggle_field_tests.js b/addons/web/static/tests/views/fields/boolean_toggle_field_tests.js index 24a89b62ad2..9c8eed49acd 100644 --- a/addons/web/static/tests/views/fields/boolean_toggle_field_tests.js +++ b/addons/web/static/tests/views/fields/boolean_toggle_field_tests.js @@ -257,4 +257,24 @@ QUnit.module("Fields", (hooks) => { await click(target, ".o_field_widget[name='bar'] input"); assert.verifySteps(["write"]); }); + + QUnit.test("BooleanToggleField - autosave option set to false", async function (assert) { + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: ` +
+ + `, + resId: 1, + mockRPC(_route, { method }) { + if (method === "write") { + assert.step("write"); + } + }, + }); + await click(target, ".o_field_widget[name='bar'] input"); + assert.verifySteps([]); + }); });