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(customElement): handle hyphenated prop pass to custom element #12032

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
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
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DeprecationTypes, compatUtils, warn } from '@vue/runtime-core'
import { includeBooleanAttr } from '@vue/shared'
import { camelize, includeBooleanAttr } from '@vue/shared'
import { unsafeToTrustedHTML } from '../nodeOps'

// functions. The user is responsible for using them with only trusted content.
Expand Down Expand Up @@ -95,7 +95,7 @@ export function patchDOMProp(
// some properties has getter, no setter, will error in 'use strict'
// eg. <select :type="null"></select> <select :willValidate="null"></select>
try {
el[key] = value
el[camelize(key)] = value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the proper change. because the normalized prop key is always camelized

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't do it here as it affects all elements. This should only apply to Vue custom elements. See ea3efa0 (reused test from this PR)

} catch (e: any) {
// do not warn if value is auto-coerced from nullish values
if (__DEV__ && !needRemove) {
Expand Down
12 changes: 9 additions & 3 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 @@ -127,8 +133,8 @@ function shouldSetAsProp(
if (isNativeOn(key) && isString(value)) {
return false
}

if (key in el) {
// if el is a Vue custom element, it should be passed as a prop.
if (camelize(key) in el) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is unnecessary. see line 142

return true
}

Expand Down