mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] compiler: multiple modifiers on t-custom
Since [1] it's possible to have a custom directive, this directive only supported one modifier. This commit, add the support to have multiple modifiers on a t-custom directive. [1] : https://github.com/odoo/owl/commit/7e687234bf40bdcf5e6598401d8604d66b14ad32
This commit is contained in:
committed by
Géry Debongnie
parent
e6768501cd
commit
7fc552e2f8
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
export type customDirectives = Record<
|
export type customDirectives = Record<
|
||||||
string,
|
string,
|
||||||
(node: Element, value: string, modifier?: string) => void
|
(node: Element, value: string, modifier: string[]) => void
|
||||||
>;
|
>;
|
||||||
|
|||||||
@@ -299,10 +299,10 @@ function parseTCustom(node: Element, ctx: ParsingContext): AST | null {
|
|||||||
throw new OwlError(`Custom directive "${directiveName}" is not defined`);
|
throw new OwlError(`Custom directive "${directiveName}" is not defined`);
|
||||||
}
|
}
|
||||||
const value = node.getAttribute(attr)!;
|
const value = node.getAttribute(attr)!;
|
||||||
const modifier = attr.split(".").length > 1 ? attr.split(".")[1] : undefined;
|
const modifiers = attr.split(".").slice(1);
|
||||||
node.removeAttribute(attr);
|
node.removeAttribute(attr);
|
||||||
try {
|
try {
|
||||||
customDirective(node, value, modifier);
|
customDirective(node, value, modifiers);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new OwlError(
|
throw new OwlError(
|
||||||
`Custom directive "${directiveName}" throw the following error: ${error}`
|
`Custom directive "${directiveName}" throw the following error: ${error}`
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ exports[`t-custom can use t-custom directive on a node 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`t-custom can use t-custom directive with modifier on a node 1`] = `
|
exports[`t-custom can use t-custom directive with modifiers on a node 1`] = `
|
||||||
"function anonymous(app, bdom, helpers
|
"function anonymous(app, bdom, helpers
|
||||||
) {
|
) {
|
||||||
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
||||||
|
|||||||
@@ -31,25 +31,27 @@ describe("t-custom", () => {
|
|||||||
expect(steps).toEqual(["clicked"]);
|
expect(steps).toEqual(["clicked"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("can use t-custom directive with modifier on a node", async () => {
|
test("can use t-custom directive with modifiers on a node", async () => {
|
||||||
const steps: string[] = [];
|
const steps: string[] = [];
|
||||||
class SomeComponent extends Component {
|
class SomeComponent extends Component {
|
||||||
static template = xml`<div t-custom-plop.mouse="click" class="my-div"/>`;
|
static template = xml`<div t-custom-plop.mouse.stop="click" class="my-div"/>`;
|
||||||
click() {
|
click() {
|
||||||
steps.push("clicked");
|
steps.push("clicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const app = new App(SomeComponent, {
|
const app = new App(SomeComponent, {
|
||||||
customDirectives: {
|
customDirectives: {
|
||||||
plop: (node, value, modifier) => {
|
plop: (node, value, modifiers) => {
|
||||||
node.setAttribute("t-on-click", value);
|
node.setAttribute("t-on-click", value);
|
||||||
steps.push(modifier || "");
|
for (let mod of modifiers) {
|
||||||
|
steps.push(mod);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
await app.mount(fixture);
|
await app.mount(fixture);
|
||||||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
||||||
fixture.querySelector("div")!.click();
|
fixture.querySelector("div")!.click();
|
||||||
expect(steps).toEqual(["mouse", "clicked"]);
|
expect(steps).toEqual(["mouse", "stop", "clicked"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user