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: handle nullish dynamic event name with event modifiers #13060

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`compiler-dom: transform v-on > should wrap both for dynamic key event w/ event modifiers 1`] = `
"const _Vue = Vue

return function render(_ctx, _cache) {
with (_ctx) {
const { toHandlerKey: _toHandlerKey, withDynamicEventModifiers: _withDynamicEventModifiers, createElementVNode: _createElementVNode, Fragment: _Fragment, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue

return (_openBlock(), _createElementBlock(_Fragment, null, [
_createElementVNode("div", { [_withDynamicEventModifiers(_toHandlerKey(e), "Once")]: test }, null, 16 /* FULL_PROPS */),
_createElementVNode("div", { [_withDynamicEventModifiers(_toHandlerKey(e), "Passive")]: test }, null, 16 /* FULL_PROPS */),
_createElementVNode("div", { [_withDynamicEventModifiers(_toHandlerKey(e), "Capture")]: test }, null, 16 /* FULL_PROPS */)
], 64 /* STABLE_FRAGMENT */))
}
}"
`;
9 changes: 9 additions & 0 deletions packages/compiler-dom/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type ObjectExpression,
TO_HANDLER_KEY,
type VNodeCall,
generate,
helperNameMap,
baseParse as parse,
transform,
Expand Down Expand Up @@ -168,6 +169,14 @@ describe('compiler-dom: transform v-on', () => {
})
})

it('should wrap both for dynamic key event w/ event modifiers', () => {
const { root: ast } = parseWithVOn(
`<div @[e].once="test"/><div @[e].passive="test"/><div @[e].capture="test"/>`,
)

expect(generate(ast).code).toMatchSnapshot()
})

it('should not wrap normal guard if there is only keys guard', () => {
const {
props: [prop],
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler-dom/src/runtimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const V_ON_WITH_MODIFIERS: unique symbol = Symbol(
export const V_ON_WITH_KEYS: unique symbol = Symbol(
__DEV__ ? `vOnKeysGuard` : ``,
)
export const V_ON_WITH_DYNAMIC_EVENT_MODIFIERS: unique symbol = Symbol(
__DEV__ ? `vOnDynamicEventModifiers` : ``,
)

export const V_SHOW: unique symbol = Symbol(__DEV__ ? `vShow` : ``)

Expand All @@ -34,6 +37,7 @@ registerRuntimeHelpers({
[V_MODEL_DYNAMIC]: `vModelDynamic`,
[V_ON_WITH_MODIFIERS]: `withModifiers`,
[V_ON_WITH_KEYS]: `withKeys`,
[V_ON_WITH_DYNAMIC_EVENT_MODIFIERS]: `withDynamicEventModifiers`,
[V_SHOW]: `vShow`,
[TRANSITION]: `Transition`,
[TRANSITION_GROUP]: `TransitionGroup`,
Expand Down
12 changes: 10 additions & 2 deletions packages/compiler-dom/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
createSimpleExpression,
isStaticExp,
} from '@vue/compiler-core'
import { V_ON_WITH_KEYS, V_ON_WITH_MODIFIERS } from '../runtimeHelpers'
import {
V_ON_WITH_DYNAMIC_EVENT_MODIFIERS,
V_ON_WITH_KEYS,
V_ON_WITH_MODIFIERS,
} from '../runtimeHelpers'
import { capitalize, makeMap } from '@vue/shared'

const isEventOptionModifier = /*@__PURE__*/ makeMap(`passive,once,capture`)
Expand Down Expand Up @@ -144,7 +148,11 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
const modifierPostfix = eventOptionModifiers.map(capitalize).join('')
key = isStaticExp(key)
? createSimpleExpression(`${key.content}${modifierPostfix}`, true)
: createCompoundExpression([`(`, key, `) + "${modifierPostfix}"`])
: createCompoundExpression([
`${context.helperString(V_ON_WITH_DYNAMIC_EVENT_MODIFIERS)}(`,
key,
`, "${modifierPostfix}")`,
])
}

return {
Expand Down
11 changes: 11 additions & 0 deletions packages/runtime-dom/src/directives/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,15 @@ export const withKeys = <T extends (event: KeyboardEvent) => any>(
)
}

/**
* @private
*/
export function withDynamicEventModifiers(
eventName: string,
modifierPostfix: string,
): string {
if (eventName != null && eventName !== '') return eventName + modifierPostfix
return ''
}

export type VOnDirective = Directive<any, any, VOnModifiers>
6 changes: 5 additions & 1 deletion packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ export {
vModelSelect,
vModelDynamic,
} from './directives/vModel'
export { withModifiers, withKeys } from './directives/vOn'
export {
withModifiers,
withKeys,
withDynamicEventModifiers,
} from './directives/vOn'
export { vShow } from './directives/vShow'

import { initVModelForSSR } from './directives/vModel'
Expand Down