[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) <aab@odoo.com>
This commit is contained in:
Pierre Rousseau
2023-04-28 07:54:26 +02:00
committed by Aaron Bohy
parent 10c46e2e76
commit a449b05221
3 changed files with 32 additions and 3 deletions
@@ -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,
};
},
@@ -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();
}
}
}
}
@@ -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: `
<form>
<field name="bar" widget="boolean_toggle" options="{'autosave': false}"/>
</form>`,
resId: 1,
mockRPC(_route, { method }) {
if (method === "write") {
assert.step("write");
}
},
});
await click(target, ".o_field_widget[name='bar'] input");
assert.verifySteps([]);
});
});