Skip to content

Commit

Permalink
fix(custom-element): properly set kebab-case props on Vue custom elem…
Browse files Browse the repository at this point in the history
…ents

close #12030
close #12032
  • Loading branch information
yyx990803 committed Sep 27, 2024
1 parent a77b959 commit ea3efa0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
15 changes: 15 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ describe('defineCustomElement', () => {
expect(e.getAttribute('baz-qux')).toBe('four')
})

test('props via hyphen property', async () => {
const Comp = defineCustomElement({
props: {
fooBar: Boolean,
},
render() {
return 'Comp'
},
})
customElements.define('my-el-comp', Comp)
render(h('my-el-comp', { 'foo-bar': true }), container)
const el = container.children[0]
expect((el as any).outerHTML).toBe('<my-el-comp foo-bar=""></my-el-comp>')
})

test('attribute -> prop type casting', async () => {
const E = defineCustomElement({
props: {
Expand Down
25 changes: 14 additions & 11 deletions packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { patchStyle } from './modules/style'
import { patchAttr } from './modules/attrs'
import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events'
import { isFunction, isModelListener, isOn, isString } from '@vue/shared'
import {
camelize,
isFunction,
isModelListener,
isOn,
isString,
} from '@vue/shared'
import type { RendererOptions } from '@vue/runtime-core'
import type { VueElement } from './apiCustomElement'

Expand Down Expand Up @@ -51,6 +57,12 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
) {
patchAttr(el, key, nextValue, isSVG, parentComponent, key !== 'value')
}
} else if (
// #11081 force set props for possible async custom element
(el as VueElement)._isVueCE &&
(/[A-Z]/.test(key) || !isString(nextValue))
) {
patchDOMProp(el, camelize(key), nextValue, parentComponent)
} else {
// special case for <input v-model type="checkbox"> with
// :true-value & :false-value
Expand Down Expand Up @@ -128,14 +140,5 @@ function shouldSetAsProp(
return false
}

if (key in el) {
return true
}

// #11081 force set props for possible async custom element
if ((el as VueElement)._isVueCE && (/[A-Z]/.test(key) || !isString(value))) {
return true
}

return false
return key in el
}

0 comments on commit ea3efa0

Please sign in to comment.