From c3e6f4d8a6b837f97cab7de689d742d1d6d4718c Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Tue, 3 Oct 2023 14:03:14 +0200 Subject: [PATCH] [IMP] web: Adds dynamicProps to Widget As with fields, dynamicProps will be accessible to widgets from extractProps. This commit will make readonly accessible as the first dynamicProps. We'll be able to add more when we need them. DynamicInfo.readonly is true iff the expression given as readonly attrs in the arch is evaluated to true with the current record. closes odoo/odoo#137414 Related: odoo/enterprise#48309 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/views/widgets/widget.js | 7 ++++- .../tests/views/form/form_view_tests.js | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/views/widgets/widget.js b/addons/web/static/src/views/widgets/widget.js index 3b6bdf67b06..4a5e17b2331 100644 --- a/addons/web/static/src/views/widgets/widget.js +++ b/addons/web/static/src/views/widgets/widget.js @@ -47,7 +47,12 @@ export class Widget extends Component { widgetInfo.attrs.readonly, record.evalContextWithVirtualIds ); - propsFromNode = this.widget.extractProps ? this.widget.extractProps(widgetInfo) : {}; + const dynamicInfo = { + readonly: readonlyFromModifiers, + }; + propsFromNode = this.widget.extractProps + ? this.widget.extractProps(widgetInfo, dynamicInfo) + : {}; } return { diff --git a/addons/web/static/tests/views/form/form_view_tests.js b/addons/web/static/tests/views/form/form_view_tests.js index c58cbf9b34a..61f8c6eb912 100644 --- a/addons/web/static/tests/views/form/form_view_tests.js +++ b/addons/web/static/tests/views/form/form_view_tests.js @@ -10623,6 +10623,37 @@ QUnit.module("Views", (hooks) => { assert.containsOnce(target, ".o_widget.my_classname"); }); + QUnit.test("widget with readonly attribute", async function (assert) { + class MyComponent extends owl.Component { + static template = owl.xml``; + get value() { + return this.props.readonly ? "readonly" : "not readonly"; + } + } + const myComponent = { + component: MyComponent, + extractProps(widgetInfo, dynamicInfo) { + return { readonly: dynamicInfo.readonly }; + }, + }; + widgetRegistry.add("test_widget", myComponent); + + await makeView({ + type: "form", + serverData, + resModel: "partner", + arch: ` +
+ + + `, + }); + + assert.strictEqual(target.querySelector(".o_widget").textContent, "not readonly"); + await click(target.querySelector(".o_field_widget[name=bar] input")); + assert.strictEqual(target.querySelector(".o_widget").textContent, "readonly"); + }); + QUnit.test("support header button as widgets on form statusbar", async function (assert) { serviceRegistry.add("http", { start: () => ({}),