mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
fix issue with mounted widget without proper vnode
This commit is contained in:
@@ -133,8 +133,9 @@ export class Widget<T extends WEnv> {
|
||||
return Promise.all(promises).then(() => vnode);
|
||||
}
|
||||
|
||||
_mount(el: HTMLElement) {
|
||||
this.el = el;
|
||||
_mount(vnode: VNode) {
|
||||
this.el = <HTMLElement>vnode.elm;
|
||||
this._.vnode = vnode;
|
||||
if (this._.parent) {
|
||||
if (this._.parent._.isMounted) {
|
||||
this._.isMounted = true;
|
||||
|
||||
@@ -685,7 +685,7 @@ const widgetDirective: Directive = {
|
||||
`let _${widgetID} = new context.widgets['${value}'](context, ${props})`
|
||||
);
|
||||
ctx.addLine(
|
||||
`let def${defID} = _${widgetID}._start().then(() => _${widgetID}._render()).then(vnode=>{Object.assign(_${dummyID}, vnode);_${dummyID}.key="${dummyID}";_${dummyID}.data.hook = {create(_,vn){_${widgetID}._mount(vn.elm)}}})`
|
||||
`let def${defID} = _${widgetID}._start().then(() => _${widgetID}._render()).then(vnode=>{Object.assign(_${dummyID}, vnode);_${dummyID}.key="${dummyID}";_${dummyID}.data.hook = {create(_,vn){_${widgetID}._mount(vn)}}})`
|
||||
);
|
||||
ctx.addLine(`extra.promises.push(def${defID})`);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Widget, WEnv } from "../src/ts/core/widget";
|
||||
import { idGenerator } from "../src/ts/core/utils";
|
||||
import { QWeb } from "../src/ts/core/qweb_vdom";
|
||||
import { idGenerator } from "../src/ts/core/utils";
|
||||
import { WEnv, Widget } from "../src/ts/core/widget";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Setup and helpers
|
||||
@@ -31,6 +31,19 @@ function nextTick(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
// Test widget
|
||||
class Counter extends Widget<WEnv> {
|
||||
name = "counter";
|
||||
template = `<div><t t-esc="state.counter"/><button t-on-click="inc">Inc</button></div>`;
|
||||
state = {
|
||||
counter: 0
|
||||
};
|
||||
|
||||
inc() {
|
||||
this.updateState({ counter: this.state.counter + 1 });
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Tests
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -48,22 +61,6 @@ describe("basic widget properties", () => {
|
||||
});
|
||||
|
||||
test("can be clicked on and updated", async () => {
|
||||
const template = `
|
||||
<div><t t-esc="state.counter"/><button t-on-click="inc">Inc</button></div>
|
||||
`;
|
||||
|
||||
class Counter extends Widget<WEnv> {
|
||||
name = "counter";
|
||||
template = template;
|
||||
state = {
|
||||
counter: 0
|
||||
};
|
||||
|
||||
inc() {
|
||||
this.updateState({ counter: this.state.counter + 1 });
|
||||
}
|
||||
}
|
||||
|
||||
const counter = new Counter(env);
|
||||
const target = document.createElement("div");
|
||||
await counter.mount(target);
|
||||
@@ -280,4 +277,22 @@ describe("composition", () => {
|
||||
await widget.mount(fixture);
|
||||
expect(widget.refs.mywidgetb instanceof WidgetB).toBe(true);
|
||||
});
|
||||
|
||||
test("modifying a sub widget", async () => {
|
||||
class ParentWidget extends Widget<WEnv> {
|
||||
template = `<div><t t-widget="Counter"/></div>`;
|
||||
widgets = { Counter };
|
||||
}
|
||||
const widget = new ParentWidget(env);
|
||||
await widget.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div><div>0<button>Inc</button></div></div>"
|
||||
);
|
||||
const button = fixture.getElementsByTagName("button")[0];
|
||||
await button.click();
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div><div>1<button>Inc</button></div></div>"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user