fix(custom-element): avoid triggering mutationObserver when relecting props

close #12214
close #12215
This commit is contained in:
Evan You 2024-11-15 17:20:59 +08:00
parent 10ab8c0e7b
commit 352bc88c1b
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 35 additions and 0 deletions

View File

@ -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 => {

View File

@ -505,6 +505,8 @@ 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') {
@ -512,6 +514,7 @@ export class VueElement
} else if (!val) {
this.removeAttribute(hyphenate(key))
}
ob && ob.observe(this, { attributes: true })
}
}
}