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(compiler-vapor): once modifier work with component event #12606

Open
wants to merge 1 commit into
base: vapor
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
Expand Up @@ -186,6 +186,28 @@ export function render(_ctx) {
}"
`;

exports[`compiler: element transform > component dynamic event with once modifier 1`] = `
"import { resolveComponent as _resolveComponent, toHandlerKey as _toHandlerKey, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _component_Foo = _resolveComponent("Foo")
const n0 = _createComponentWithFallback(_component_Foo, { $: [
() => ({ [_toHandlerKey(_ctx.foo) + "Once"]: () => _ctx.bar })
] }, null, true)
return n0
}"
`;

exports[`compiler: element transform > component event with once modifier 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _component_Foo = _resolveComponent("Foo")
const n0 = _createComponentWithFallback(_component_Foo, { onFooOnce: () => _ctx.bar }, null, true)
return n0
}"
`;

exports[`compiler: element transform > component with dynamic event arguments 1`] = `
"import { resolveComponent as _resolveComponent, toHandlerKey as _toHandlerKey, createComponentWithFallback as _createComponentWithFallback } from 'vue';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,16 @@ describe('compiler: element transform', () => {
])
})

test('component event with once modifier', () => {
const { code } = compileWithElementTransform(`<Foo @foo.once="bar" />`)
expect(code).toMatchSnapshot()
})

test('component dynamic event with once modifier', () => {
const { code } = compileWithElementTransform(`<Foo @[foo].once="bar" />`)
expect(code).toMatchSnapshot()
})

test('invalid html nesting', () => {
const { code, ir } = compileWithElementTransform(
`<p><div>123</div></p>
Expand Down
20 changes: 17 additions & 3 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './utils'
import {
canSetValueDirectly,
capitalize,
isSVGTag,
shouldSetAsAttr,
toHandlerKey,
Expand Down Expand Up @@ -108,15 +109,20 @@ function genLiteralObjectProps(
}

export function genPropKey(
{ key: node, modifier, runtimeCamelize, handler }: IRProp,
{ key: node, modifier, runtimeCamelize, handler, handlerModifiers }: IRProp,
context: CodegenContext,
): CodeFragment[] {
const { helper } = context

const handlerModifierPostfix = handlerModifiers
? handlerModifiers.map(capitalize).join('')
: ''
// static arg was transformed by v-bind transformer
if (node.isStatic) {
// only quote keys if necessary
const keyName = handler ? toHandlerKey(node.content) : node.content
const keyName =
(handler ? toHandlerKey(node.content) : node.content) +
handlerModifierPostfix
return [
[
isSimpleIdentifier(keyName) ? keyName : JSON.stringify(keyName),
Expand All @@ -133,7 +139,15 @@ export function genPropKey(
if (handler) {
key = genCall(helper('toHandlerKey'), key)
}
return ['[', modifier && `${JSON.stringify(modifier)} + `, ...key, ']']
return [
'[',
modifier && `${JSON.stringify(modifier)} + `,
...key,
handlerModifierPostfix
? ` + ${JSON.stringify(handlerModifierPostfix)}`
: undefined,
']',
]
}

export function genPropValue(
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface DirectiveTransformResult {
modifier?: '.' | '^'
runtimeCamelize?: boolean
handler?: boolean
handlerModifiers?: string[]
model?: boolean
modelModifiers?: string[]
}
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const transformVOn: DirectiveTransform = (dir, node, context) => {
key: arg,
value: handler,
handler: true,
handlerModifiers: eventOptionModifiers,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function propGetter(rawProps: Record<string, any>, key: string) {
let i = dynamicSources.length
while (i--) {
const source = resolveSource(dynamicSources[i])
if (hasOwn(source, key)) return source[key]
if (hasOwn(source, key)) return resolveSource(source[key])
}
}
return rawProps[key] && resolveSource(rawProps[key])
Expand Down
Loading