Browse Source

fix(transition): treat null transition keys as absent in child fallback

daiwei 1 month ago
parent
commit
724fd889c9

+ 22 - 0
packages/runtime-vapor/__tests__/components/Transition.spec.ts

@@ -74,4 +74,26 @@ describe('Transition', () => {
     const resolved = resolveTransitionBlock(child)!
     expect(resolved.$key).toBe(0)
   })
+
+  test('treats null component key as absent when resolving child', () => {
+    const Child = defineVaporComponent({
+      setup() {
+        return template(`<div>child</div>`)() as any
+      },
+    })
+
+    let child: any
+    define({
+      setup() {
+        child = createComponent(Child)
+        setBlockKey(child, null)
+        return child
+      },
+    }).render()
+
+    child.block.$key = null
+
+    const resolved = resolveTransitionBlock(child)!
+    expect(resolved.$key).toBe(child.uid)
+  })
 })

+ 2 - 2
packages/runtime-vapor/src/components/Transition.ts

@@ -379,7 +379,7 @@ export function resolveTransitionBlock(
           onFragment,
         )
         if (child) {
-          if (child.$key === undefined) {
+          if (child.$key == null) {
             child.$key = block.$key ?? block.uid
           }
           // align with normal component branches so leaving cache can
@@ -392,7 +392,7 @@ export function resolveTransitionBlock(
       if (getComponentName(block.type) === displayName) return undefined
       child = resolveTransitionBlock(block.block, onFragment)
       if (child) {
-        if (child.$key === undefined) {
+        if (child.$key == null) {
           // prefer explicit component key, otherwise fall back to uid.
           child.$key = block.$key ?? block.uid
         }