Skip to content

Commit a697871

Browse files
authored
feat(compiler-vapor): support keys and nonKeys modifier for component event (#13053)
1 parent 436eda7 commit a697871

File tree

6 files changed

+114
-6
lines changed

6 files changed

+114
-6
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,36 @@ export function render(_ctx) {
246246
}"
247247
`;
248248

249+
exports[`compiler: element transform > component event with keys modifier 1`] = `
250+
"import { resolveComponent as _resolveComponent, withKeys as _withKeys, createComponentWithFallback as _createComponentWithFallback } from 'vue';
251+
252+
export function render(_ctx) {
253+
const _component_Foo = _resolveComponent("Foo")
254+
const n0 = _createComponentWithFallback(_component_Foo, { onKeyup: () => _withKeys(_ctx.bar, ["enter"]) }, null, true)
255+
return n0
256+
}"
257+
`;
258+
259+
exports[`compiler: element transform > component event with multiple modifiers and event options 1`] = `
260+
"import { resolveComponent as _resolveComponent, withModifiers as _withModifiers, withKeys as _withKeys, createComponentWithFallback as _createComponentWithFallback } from 'vue';
261+
262+
export function render(_ctx) {
263+
const _component_Foo = _resolveComponent("Foo")
264+
const n0 = _createComponentWithFallback(_component_Foo, { onFooCaptureOnce: () => _withKeys(_withModifiers(_ctx.bar, ["stop","prevent"]), ["enter"]) }, null, true)
265+
return n0
266+
}"
267+
`;
268+
269+
exports[`compiler: element transform > component event with nonKeys modifier 1`] = `
270+
"import { resolveComponent as _resolveComponent, withModifiers as _withModifiers, createComponentWithFallback as _createComponentWithFallback } from 'vue';
271+
272+
export function render(_ctx) {
273+
const _component_Foo = _resolveComponent("Foo")
274+
const n0 = _createComponentWithFallback(_component_Foo, { onFoo: () => _withModifiers(_ctx.bar, ["stop","prevent"]) }, null, true)
275+
return n0
276+
}"
277+
`;
278+
249279
exports[`compiler: element transform > component event with once modifier 1`] = `
250280
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';
251281

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,78 @@ describe('compiler: element transform', () => {
896896
})
897897
})
898898

899+
test('component event with keys modifier', () => {
900+
const { code, ir } = compileWithElementTransform(
901+
`<Foo @keyup.enter="bar" />`,
902+
)
903+
expect(code).toMatchSnapshot()
904+
expect(ir.block.dynamic.children[0].operation).toMatchObject({
905+
type: IRNodeTypes.CREATE_COMPONENT_NODE,
906+
tag: 'Foo',
907+
props: [
908+
[
909+
{
910+
key: { content: 'keyup' },
911+
handler: true,
912+
handlerModifiers: {
913+
keys: ['enter'],
914+
nonKeys: [],
915+
options: [],
916+
},
917+
},
918+
],
919+
],
920+
})
921+
})
922+
923+
test('component event with nonKeys modifier', () => {
924+
const { code, ir } = compileWithElementTransform(
925+
`<Foo @foo.stop.prevent="bar" />`,
926+
)
927+
expect(code).toMatchSnapshot()
928+
expect(ir.block.dynamic.children[0].operation).toMatchObject({
929+
type: IRNodeTypes.CREATE_COMPONENT_NODE,
930+
tag: 'Foo',
931+
props: [
932+
[
933+
{
934+
key: { content: 'foo' },
935+
handler: true,
936+
handlerModifiers: {
937+
keys: [],
938+
nonKeys: ['stop', 'prevent'],
939+
options: [],
940+
},
941+
},
942+
],
943+
],
944+
})
945+
})
946+
947+
test('component event with multiple modifiers and event options', () => {
948+
const { code, ir } = compileWithElementTransform(
949+
`<Foo @foo.enter.stop.prevent.capture.once="bar" />`,
950+
)
951+
expect(code).toMatchSnapshot()
952+
expect(ir.block.dynamic.children[0].operation).toMatchObject({
953+
type: IRNodeTypes.CREATE_COMPONENT_NODE,
954+
tag: 'Foo',
955+
props: [
956+
[
957+
{
958+
key: { content: 'foo' },
959+
handler: true,
960+
handlerModifiers: {
961+
keys: ['enter'],
962+
nonKeys: ['stop', 'prevent'],
963+
options: ['capture', 'once'],
964+
},
965+
},
966+
],
967+
],
968+
})
969+
})
970+
899971
test('component with dynamic event arguments', () => {
900972
const { code, ir } = compileWithElementTransform(
901973
`<Foo @[foo-bar]="bar" @[baz]="qux" />`,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function genProp(prop: IRProp, context: CodegenContext, isStatic?: boolean) {
222222
? genEventHandler(
223223
context,
224224
prop.values[0],
225-
undefined,
225+
prop.handlerModifiers,
226226
true /* wrap handlers passed to components */,
227227
)
228228
: isStatic

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ export function genPropKey(
114114
): CodeFragment[] {
115115
const { helper } = context
116116

117-
const handlerModifierPostfix = handlerModifiers
118-
? handlerModifiers.map(capitalize).join('')
119-
: ''
117+
const handlerModifierPostfix =
118+
handlerModifiers && handlerModifiers.options
119+
? handlerModifiers.options.map(capitalize).join('')
120+
: ''
120121
// static arg was transformed by v-bind transformer
121122
if (node.isStatic) {
122123
// only quote keys if necessary

packages/compiler-vapor/src/transform.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
type IRSlots,
2525
type OperationNode,
2626
type RootIRNode,
27+
type SetEventIRNode,
2728
type VaporDirectiveNode,
2829
} from './ir'
2930
import { isConstantExpression, isStaticExpression } from './utils'
@@ -46,7 +47,7 @@ export interface DirectiveTransformResult {
4647
modifier?: '.' | '^'
4748
runtimeCamelize?: boolean
4849
handler?: boolean
49-
handlerModifiers?: string[]
50+
handlerModifiers?: SetEventIRNode['modifiers']
5051
model?: boolean
5152
modelModifiers?: string[]
5253
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export const transformVOn: DirectiveTransform = (dir, node, context) => {
6565
key: arg,
6666
value: handler,
6767
handler: true,
68-
handlerModifiers: eventOptionModifiers,
68+
handlerModifiers: {
69+
keys: keyModifiers,
70+
nonKeys: nonKeyModifiers,
71+
options: eventOptionModifiers,
72+
},
6973
}
7074
}
7175

0 commit comments

Comments
 (0)