Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-dom): When the prop of a custom element is undefined, the property should be deleted #11278

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,5 +771,43 @@ describe('defineCustomElement', () => {
`<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
)
})
test('with undefined prop', async () => {
const toggleBoo = () => {
const elem = document.getElementById('mve') as any
if (elem.boo) {
elem.removeAttribute('boo')
} else {
elem.setAttribute('boo', 'boo')
}
}
const Foo = defineCustomElement({
props: {
boo: {
type: Boolean,
},
},
render(ctx: any) {
return h(
'div',
null,
`BOO: ${JSON.stringify(ctx.boo)}; typeof: ${typeof ctx.boo}`,
)
},
})

customElements.define('my-vue-element', Foo)
container.innerHTML = `<my-vue-element id="mve"></my-vue-element>`
const e = container.childNodes[0] as VueElement
toggleBoo()
await nextTick()
expect(e.shadowRoot!.innerHTML).toBe(
`<div id="mve">BOO: true; typeof: boolean</div>`,
)
toggleBoo()
await nextTick()
expect(e.shadowRoot!.innerHTML).toBe(
`<div id="mve">BOO: false; typeof: boolean</div>`,
)
})
})
})
1 change: 1 addition & 0 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export class VueElement extends BaseClass {
) {
if (val !== this._props[key]) {
this._props[key] = val
if (val === undefined) delete this._props[key]
if (shouldUpdate && this._instance) {
this._update()
}
Expand Down