[FIX] blockdom: undefined properties are treated as empty strings

This commit is contained in:
Géry Debongnie
2022-02-15 08:58:07 +01:00
committed by Samuel Degueldre
parent 804ad3c35e
commit 3536f41f00
4 changed files with 41 additions and 1 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ export function updateClass(this: HTMLElement, val: any, oldVal: any) {
export function makePropSetter(name: string): Setter<HTMLElement> { export function makePropSetter(name: string): Setter<HTMLElement> {
return function setProp(this: HTMLElement, value: any) { return function setProp(this: HTMLElement, value: any) {
(this as any)[name] = value; (this as any)[name] = value || "";
}; };
} }
+9
View File
@@ -169,6 +169,15 @@ describe("properties", () => {
expect(input.value).toBe("potato"); expect(input.value).toBe("potato");
}); });
test("input with value attribute, and undefined given", () => {
const block = createBlock(`<input block-attribute-0="value"/>`);
const tree = block([undefined]);
mount(tree, fixture);
const input = fixture.querySelector("input")!;
expect(input.value).toBe("");
});
test("input type=checkbox with checked attribute", () => { test("input type=checkbox with checked attribute", () => {
// render input with initial value // render input with initial value
const block = createBlock(`<input type="checkbox" block-attribute-0="checked"/>`); const block = createBlock(`<input type="checkbox" block-attribute-0="checked"/>`);
@@ -410,6 +410,24 @@ exports[`t-model directive on an textarea 1`] = `
}" }"
`; `;
exports[`t-model directive t-model on an input with an undefined value 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let { toNumber } = helpers;
let block1 = createBlock(\`<input block-attribute-0=\\"value\\" block-handler-1=\\"input\\"/>\`);
return function template(ctx, node, key = \\"\\") {
const bExpr1 = ctx['state'];
const expr1 = 'text';
let attr1 = bExpr1[expr1];
let hdlr1 = [(ev) => { bExpr1[expr1] = ev.target.value; }];
return block1([attr1, hdlr1]);
}
}"
`;
exports[`t-model directive t-model on select with static options 1`] = ` exports[`t-model directive t-model on select with static options 1`] = `
"function anonymous(bdom, helpers "function anonymous(bdom, helpers
) { ) {
+13
View File
@@ -30,6 +30,19 @@ describe("t-model directive", () => {
expect(fixture.innerHTML).toBe("<div><input><span>test</span></div>"); expect(fixture.innerHTML).toBe("<div><input><span>test</span></div>");
}); });
test("t-model on an input with an undefined value", async () => {
class SomeComponent extends Component {
static template = xml`<input t-model="state.text"/>`;
state = useState({ text: undefined });
}
await mount(SomeComponent, fixture);
expect(fixture.innerHTML).toBe("<input>");
const input = fixture.querySelector("input")!;
expect(input.value).toBe("");
});
test("basic use, on an input with bracket expression", async () => { test("basic use, on an input with bracket expression", async () => {
class SomeComponent extends Component { class SomeComponent extends Component {
static template = xml` static template = xml`