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): apiCustomElement fix #5687 and #5793 #5794

Closed
wants to merge 3 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
39 changes: 39 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,45 @@ describe('defineCustomElement', () => {
customElements.define('my-el-upgrade', E)
expect(el.shadowRoot.innerHTML).toBe(`foo: hello`)
})

// #5687
test('using .xx-xx underlined naming conventions when used by WebComponent is reactive', () => {
const E = defineCustomElement({
props: {
maxAge: Number
},
render() {
return `max age: ${this.maxAge}`
}
})
customElements.define('my-element-hyphenate', E)
const el = document.createElement('my-element-hyphenate') as any
el.setAttribute('max-age', 0)
container.appendChild(el)
expect(el.maxAge).toBe(0)
el.maxAge = 50
expect(el.maxAge).toBe(50)
el['max-age'] = 100
expect(el.maxAge).toBe(100)
})

// # 5793
test('set number value in dom property', () => {
const E = defineCustomElement({
props: {
maxAge: Number
},
render() {
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('emits', () => {
Expand Down
34 changes: 23 additions & 11 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ export class VueElement extends BaseClass {
// cast Number-type props set before resolve
let numberProps
if (hasOptions) {
for (const key in this._props) {
for (const key in props) {
const opt = props[key]
if (opt === Number || (opt && opt.type === Number)) {
this._props[key] = toNumber(this._props[key])
if (key in this._props) {
this._props[key] = toNumber(this._props[key])
}
;(numberProps || (numberProps = Object.create(null)))[key] = true
}
}
Expand All @@ -241,14 +243,10 @@ export class VueElement extends BaseClass {

// defining getter/setters on prototype
for (const key of rawKeys.map(camelize)) {
Object.defineProperty(this, key, {
get() {
return this._getProp(key)
},
set(val) {
this._setProp(key, val)
}
})
this._defineElementProperty(key)
if (hyphenate(key) !== key) {
this._defineElementProperty(hyphenate(key), key)
}
}

// apply CSS
Expand All @@ -266,9 +264,23 @@ export class VueElement extends BaseClass {
}
}

private _defineElementProperty(key: string, originalKey = key) {
Object.defineProperty(this, key, {
get() {
return this._getProp(originalKey)
},
set(val) {
this._setProp(originalKey, val)
}
})
}

protected _setAttr(key: string) {
let value = this.getAttribute(key)
if (this._numberProps && this._numberProps[key]) {
if (
this._numberProps &&
(this._numberProps[key] || this._numberProps[camelize(key)])
) {
value = toNumber(value)
}
this._setProp(camelize(key), value, false)
Expand Down