11// NOTE: runtime-core/src/componentEmits.ts
22
33// TODO WIP
4- // @ts -nocheck
54
65import {
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'
1919import type { Component , ComponentInternalInstance } from './component'
2020import { VaporErrorCodes , callWithAsyncErrorHandling } from './errorHandling'
21+ import {
22+ type NormalizedRawProps ,
23+ type StaticProps ,
24+ getDynamicPropValue ,
25+ } from './componentProps'
2126
2227export 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 = { }
0 commit comments