Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 2f3627a

Browse files
Jevon617sxzz
authored andcommitted
feat(runtime-vapor): runtime for v-on in component
1 parent 7cacb65 commit 2f3627a

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

packages/runtime-vapor/src/componentEmits.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// NOTE: runtime-core/src/componentEmits.ts
22

33
// TODO WIP
4-
// @ts-nocheck
54

65
import {
76
EMPTY_OBJ,
@@ -11,13 +10,19 @@ import {
1110
hasOwn,
1211
hyphenate,
1312
isArray,
13+
isFunction,
1414
isOn,
1515
isString,
1616
looseToNumber,
1717
toHandlerKey,
1818
} from '@vue/shared'
1919
import type { Component, ComponentInternalInstance } from './component'
2020
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
21+
import {
22+
type NormalizedRawProps,
23+
type StaticProps,
24+
getDynamicPropValue,
25+
} from './componentProps'
2126

2227
export type ObjectEmitsOptions = Record<
2328
string,
@@ -49,20 +54,42 @@ export function emit(
4954
) {
5055
if (instance.isUnmounted) return
5156
// TODO
52-
// @ts-expect-error
53-
const { rawProps } = instance
57+
const { rawProps, emitsOptions } = instance
58+
const hasDynamicProps = rawProps.some(isFunction)
59+
const events: Record<string, (...args: any[]) => any> = {}
60+
61+
if (emitsOptions) {
62+
if (hasDynamicProps) {
63+
for (const key in emitsOptions) {
64+
const rawKey = toHandlerKey(key)
65+
const [handler] = getDynamicPropValue(
66+
rawProps as NormalizedRawProps,
67+
rawKey,
68+
)
69+
events[rawKey] = handler as (...args: any[]) => any
70+
}
71+
} else {
72+
for (const key in emitsOptions) {
73+
const rawKey = toHandlerKey(key)
74+
const handler = (rawProps[0] as StaticProps)[rawKey]
75+
events[rawKey] = handler as (...args: any[]) => any
76+
}
77+
}
78+
}
5479

5580
let args = rawArgs
5681
const isModelListener = event.startsWith('update:')
5782

5883
// for v-model update:xxx events, apply modifiers on args
5984
const modelArg = isModelListener && event.slice(7)
6085

61-
if (modelArg && modelArg in rawProps) {
86+
if (modelArg && modelArg in events) {
6287
const modifiersKey = `${
6388
modelArg === 'modelValue' ? 'model' : modelArg
6489
}Modifiers`
65-
const { number, trim } = rawProps[modifiersKey] || EMPTY_OBJ
90+
91+
// @ts-expect-error: todo
92+
const { number, trim } = events[modifiersKey] || EMPTY_OBJ
6693
if (trim) {
6794
args = rawArgs.map(a => (isString(a) ? a.trim() : a))
6895
}
@@ -75,13 +102,13 @@ export function emit(
75102

76103
let handlerName
77104
let handler =
78-
rawProps[(handlerName = toHandlerKey(event))] ||
105+
events[(handlerName = toHandlerKey(event))] ||
79106
// also try camelCase event handler (#2249)
80-
rawProps[(handlerName = toHandlerKey(camelize(event)))]
107+
events[(handlerName = toHandlerKey(camelize(event)))]
81108
// for v-model update:xxx events, also trigger kebab-case equivalent
82109
// for props passed via kebab-case
83110
if (!handler && isModelListener) {
84-
handler = rawProps[(handlerName = toHandlerKey(hyphenate(event)))]
111+
handler = events[(handlerName = toHandlerKey(hyphenate(event)))]
85112
}
86113

87114
if (handler) {
@@ -93,7 +120,7 @@ export function emit(
93120
)
94121
}
95122

96-
const onceHandler = rawProps[`${handlerName}Once`]
123+
const onceHandler = events[`${handlerName}Once`]
97124
if (onceHandler) {
98125
if (!instance.emitted) {
99126
instance.emitted = {}

packages/runtime-vapor/src/componentProps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export type NormalizedPropsOptions =
7272
| [props: NormalizedProps, needCastKeys: string[]]
7373
| []
7474

75-
type StaticProps = Record<string, () => unknown>
75+
export type StaticProps = Record<string, () => unknown>
7676
type DynamicProps = () => Data
7777
export type NormalizedRawProps = Array<StaticProps | DynamicProps>
7878
export type RawProps = NormalizedRawProps | StaticProps | null
@@ -170,7 +170,7 @@ function getRawKey(obj: Data, key: string) {
170170
}
171171

172172
type DynamicPropResult = [value: unknown, absent: boolean]
173-
function getDynamicPropValue(
173+
export function getDynamicPropValue(
174174
rawProps: NormalizedRawProps,
175175
key: string,
176176
): DynamicPropResult {

0 commit comments

Comments
 (0)