fix(custom-elements): fix number type props casting check

fix #5793
adapted from #5794
This commit is contained in:
Evan You 2022-11-11 15:18:46 +08:00
parent afe889999c
commit 89f37ceb62
2 changed files with 26 additions and 3 deletions

View File

@ -228,6 +228,25 @@ describe('defineCustomElement', () => {
await nextTick()
expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
})
// # 5793
test('set number value in dom property', () => {
const E = defineCustomElement({
props: {
'max-age': Number
},
render() {
// @ts-ignore
return `max age: ${this.maxAge}/type: ${typeof this.maxAge}`
}
})
customElements.define('my-element-number-property', E)
const el = document.createElement('my-element-number-property') as any
container.appendChild(el)
el.maxAge = 50
expect(el.maxAge).toBe(50)
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
})
})
describe('attrs', () => {

View File

@ -234,11 +234,15 @@ export class VueElement extends BaseClass {
// cast Number-type props set before resolve
let numberProps
if (props && !isArray(props)) {
for (const key in this._props) {
for (const key in props) {
const opt = props[key]
if (opt === Number || (opt && opt.type === Number)) {
if (key in this._props) {
this._props[key] = toNumber(this._props[key])
;(numberProps || (numberProps = Object.create(null)))[key] = true
}
;(numberProps || (numberProps = Object.create(null)))[
camelize(key)
] = true
}
}
}