[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) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2023-10-04 08:39:15 +00:00
committed by Aaron Bohy
parent 8b885efa03
commit c3e6f4d8a6
2 changed files with 37 additions and 1 deletions
@@ -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 {
@@ -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`<span t-esc="value"/>`;
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: `
<form>
<field name="bar"/>
<widget name="test_widget" readonly="bar"/>
</form>`,
});
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: () => ({}),