Skip to content

Commit

Permalink
fix(custom-element): avoid triggering mutationObserver when relecting…
Browse files Browse the repository at this point in the history
… props

close #12214
close #12215
  • Loading branch information
yyx990803 committed Nov 15, 2024
1 parent 10ab8c0 commit 352bc88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,38 @@ describe('defineCustomElement', () => {
expect(e.value).toBe('hi')
})

// #12214
test('Boolean prop with default true', async () => {
const E = defineCustomElement({
props: {
foo: {
type: Boolean,
default: true,
},
},
render() {
return String(this.foo)
},
})
customElements.define('my-el-default-true', E)
container.innerHTML = `<my-el-default-true></my-el-default-true>`
const e = container.childNodes[0] as HTMLElement & { foo: any },
shadowRoot = e.shadowRoot as ShadowRoot
expect(shadowRoot.innerHTML).toBe('true')
e.foo = undefined
await nextTick()
expect(shadowRoot.innerHTML).toBe('true')
e.foo = false
await nextTick()
expect(shadowRoot.innerHTML).toBe('false')
e.foo = null
await nextTick()
expect(shadowRoot.innerHTML).toBe('null')
e.foo = ''
await nextTick()
expect(shadowRoot.innerHTML).toBe('true')
})

test('support direct setup function syntax with extra options', () => {
const E = defineCustomElement(
props => {
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,16 @@ export class VueElement
}
// reflect
if (shouldReflect) {
const ob = this._ob
ob && ob.disconnect()
if (val === true) {
this.setAttribute(hyphenate(key), '')
} else if (typeof val === 'string' || typeof val === 'number') {
this.setAttribute(hyphenate(key), val + '')
} else if (!val) {
this.removeAttribute(hyphenate(key))
}
ob && ob.observe(this, { attributes: true })
}
}
}
Expand Down

0 comments on commit 352bc88

Please sign in to comment.