Sfoglia il codice sorgente

fix(runtime-vapor): properly resolve event handler in dynamic props (#14514)

Jack 2 mesi fa
parent
commit
67641706ba

+ 29 - 0
packages/runtime-vapor/__tests__/component.spec.ts

@@ -162,6 +162,35 @@ describe('component', () => {
     expect(host.innerHTML).toBe('<div>1</div><div>1</div>')
   })
 
+  it('events in dynamic props', async () => {
+    const { component: Child } = define({
+      props: ['count'],
+      setup(props: any, { emit }) {
+        emit('update', props.count + 1)
+        const n0 = template('<div></div>')()
+        renderEffect(() => setElementText(n0, props.count))
+        return n0
+      },
+    })
+
+    const count = ref(0)
+    const { host } = define({
+      setup() {
+        const n0 = createComponent(Child, {
+          $: [
+            () => ({
+              count: count.value,
+            }),
+            { onUpdate: () => (val: number) => (count.value = val) },
+          ],
+        })
+        return n0
+      },
+    }).render()
+
+    expect(host.innerHTML).toBe('<div>1</div>')
+  })
+
   it('child only updates once when triggered in multiple ways', async () => {
     const a = ref(0)
     const calls: string[] = []

+ 3 - 2
packages/runtime-vapor/src/componentEmits.ts

@@ -1,6 +1,6 @@
 import { type ObjectEmitsOptions, baseEmit } from '@vue/runtime-dom'
 import type { VaporComponent, VaporComponentInstance } from './component'
-import { EMPTY_OBJ, hasOwn, isArray, isOn } from '@vue/shared'
+import { EMPTY_OBJ, hasOwn, isArray, isFunction, isOn } from '@vue/shared'
 import { type RawProps, resolveSource } from './componentProps'
 import { interopKey } from './vdomInterop'
 
@@ -49,7 +49,8 @@ function propGetter(rawProps: RawProps, key: string) {
       const source = resolveSource(dynamicSources[i])
       if (hasOwn(source, key))
         // for props passed from VDOM component, no need to resolve
-        return dynamicSources[interopKey] || isOn(key)
+        return dynamicSources[interopKey] ||
+          (isOn(key) && isFunction(dynamicSources[i]))
           ? source[key]
           : resolveSource(source[key])
     }