Skip to content

Commit

Permalink
fix(runtime-core): fix inconsistent event casing for render functions(v…
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowings-zy committed Oct 2, 2020
1 parent 5d825f3 commit b6a4765
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ describe('component: emit', () => {
expect(onBaz).toHaveBeenCalled()
})

test('trigger camelize event', () => {
const Foo = defineComponent({
render() {},
created() {
this.$emit('test-event')
}
})

const fooSpy = jest.fn()
const Comp = () =>
h(Foo, {
onTestEvent: fooSpy
})
render(h(Comp), nodeOps.createElement('div'))

expect(fooSpy).toHaveBeenCalled()
})

// for v-model:foo-bar usage in DOM templates
test('trigger hyphenated events for update:xxx events', () => {
const Foo = defineComponent({
Expand Down
25 changes: 22 additions & 3 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
capitalize,
hyphenate,
isFunction,
extend
extend,
camelize
} from '@vue/shared'
import {
ComponentInternalInstance,
ComponentOptions,
ConcreteComponent
ConcreteComponent,
formatComponentName
} from './component'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { warn } from './warning'
Expand Down Expand Up @@ -78,7 +80,24 @@ export function emit(
devtoolsComponentEmit(instance, event, args)
}

let handlerName = `on${capitalize(event)}`
if (__DEV__) {
const lowerCaseEvent = event.toLowerCase()
if (lowerCaseEvent !== event && props[`on` + capitalize(lowerCaseEvent)]) {
warn(
`Event "${lowerCaseEvent}" is emitted in component ` +
`${formatComponentName(
instance,
instance.type
)} but the handler is registered for "${event}". ` +
`Note that HTML attributes are case-insensitive and you cannot use ` +
`v-on to listen to camelCase events when using in-DOM templates. ` +
`You should probably use "${hyphenate(event)}" instead of "${event}".`
)
}
}

// convert handler name to camelCase. See issue #2249
let handlerName = `on${capitalize(camelize(event))}`
let handler = props[handlerName]
// for v-model update:xxx events, also trigger kebab-case equivalent
// for props passed via kebab-case
Expand Down

0 comments on commit b6a4765

Please sign in to comment.