Skip to content

Commit 1f911a5

Browse files
committed
fix(compiler-vapor): once modifier work with component event
1 parent ef6986f commit 1f911a5

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ export function render(_ctx) {
186186
}"
187187
`;
188188

189+
exports[`compiler: element transform > component dynamic event with once modifier 1`] = `
190+
"import { resolveComponent as _resolveComponent, toHandlerKey as _toHandlerKey, createComponentWithFallback as _createComponentWithFallback } from 'vue';
191+
192+
export function render(_ctx) {
193+
const _component_Foo = _resolveComponent("Foo")
194+
const n0 = _createComponentWithFallback(_component_Foo, { $: [
195+
() => ({ [_toHandlerKey(_ctx.foo) + "Once"]: () => _ctx.bar })
196+
] }, null, true)
197+
return n0
198+
}"
199+
`;
200+
201+
exports[`compiler: element transform > component event with once modifier 1`] = `
202+
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';
203+
204+
export function render(_ctx) {
205+
const _component_Foo = _resolveComponent("Foo")
206+
const n0 = _createComponentWithFallback(_component_Foo, { onFooOnce: () => _ctx.bar }, null, true)
207+
return n0
208+
}"
209+
`;
210+
189211
exports[`compiler: element transform > component with dynamic event arguments 1`] = `
190212
"import { resolveComponent as _resolveComponent, toHandlerKey as _toHandlerKey, createComponentWithFallback as _createComponentWithFallback } from 'vue';
191213

packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,16 @@ describe('compiler: element transform', () => {
872872
])
873873
})
874874

875+
test('component event with once modifier', () => {
876+
const { code } = compileWithElementTransform(`<Foo @foo.once="bar" />`)
877+
expect(code).toMatchSnapshot()
878+
})
879+
880+
test('component dynamic event with once modifier', () => {
881+
const { code } = compileWithElementTransform(`<Foo @[foo].once="bar" />`)
882+
expect(code).toMatchSnapshot()
883+
})
884+
875885
test('invalid html nesting', () => {
876886
const { code, ir } = compileWithElementTransform(
877887
`<p><div>123</div></p>

packages/compiler-vapor/src/generators/prop.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from './utils'
2323
import {
2424
canSetValueDirectly,
25+
capitalize,
2526
isSVGTag,
2627
shouldSetAsAttr,
2728
toHandlerKey,
@@ -108,15 +109,20 @@ function genLiteralObjectProps(
108109
}
109110

110111
export function genPropKey(
111-
{ key: node, modifier, runtimeCamelize, handler }: IRProp,
112+
{ key: node, modifier, runtimeCamelize, handler, handlerModifiers }: IRProp,
112113
context: CodegenContext,
113114
): CodeFragment[] {
114115
const { helper } = context
115116

117+
const handlerModifierPostfix = handlerModifiers
118+
? handlerModifiers.map(capitalize).join('')
119+
: ''
116120
// static arg was transformed by v-bind transformer
117121
if (node.isStatic) {
118122
// only quote keys if necessary
119-
const keyName = handler ? toHandlerKey(node.content) : node.content
123+
const keyName =
124+
(handler ? toHandlerKey(node.content) : node.content) +
125+
handlerModifierPostfix
120126
return [
121127
[
122128
isSimpleIdentifier(keyName) ? keyName : JSON.stringify(keyName),
@@ -133,7 +139,15 @@ export function genPropKey(
133139
if (handler) {
134140
key = genCall(helper('toHandlerKey'), key)
135141
}
136-
return ['[', modifier && `${JSON.stringify(modifier)} + `, ...key, ']']
142+
return [
143+
'[',
144+
modifier && `${JSON.stringify(modifier)} + `,
145+
...key,
146+
handlerModifierPostfix
147+
? ` + ${JSON.stringify(handlerModifierPostfix)}`
148+
: undefined,
149+
']',
150+
]
137151
}
138152

139153
export function genPropValue(

packages/compiler-vapor/src/transform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface DirectiveTransformResult {
4747
modifier?: '.' | '^'
4848
runtimeCamelize?: boolean
4949
handler?: boolean
50+
handlerModifiers?: string[]
5051
model?: boolean
5152
modelModifiers?: string[]
5253
}

packages/compiler-vapor/src/transforms/vOn.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const transformVOn: DirectiveTransform = (dir, node, context) => {
6767
key: arg,
6868
value: handler,
6969
handler: true,
70+
handlerModifiers: eventOptionModifiers,
7071
}
7172
}
7273

packages/runtime-vapor/src/componentEmits.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function propGetter(rawProps: Record<string, any>, key: string) {
4646
let i = dynamicSources.length
4747
while (i--) {
4848
const source = resolveSource(dynamicSources[i])
49-
if (hasOwn(source, key)) return source[key]
49+
if (hasOwn(source, key)) return resolveSource(source[key])
5050
}
5151
}
5252
return rawProps[key] && resolveSource(rawProps[key])

0 commit comments

Comments
 (0)