]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core/template-ref): named ref in v-for regression fix (#5118)
authorlidlanca <8693091+lidlanca@users.noreply.github.com>
Tue, 12 Apr 2022 07:28:40 +0000 (03:28 -0400)
committerGitHub <noreply@github.com>
Tue, 12 Apr 2022 07:28:40 +0000 (03:28 -0400)
close #5116
close #5447
close #5525

packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
packages/runtime-core/src/rendererTemplateRef.ts

index 198687c06db7677d76239a770a12308e14f4fbe2..4aad6f7edbc705e74eb98143514f5b267d695d36 100644 (file)
@@ -442,4 +442,59 @@ describe('api: template refs', () => {
     await nextTick()
     expect(mapRefs()).toMatchObject(['2', '3', '4'])
   })
+
+   
+
+  test('named ref in v-for', async () => {
+    const show = ref(true);
+    const list = reactive([1, 2, 3])
+    const listRefs = ref([])
+    const mapRefs = () => listRefs.value.map(n => serializeInner(n))
+
+    const App = {
+      setup() {
+        return { listRefs }
+      },
+      render() {
+        return show.value
+          ? h(
+              'ul',
+              list.map(i =>
+                h(
+                  'li',
+                  {
+                    ref: 'listRefs',
+                    ref_for: true
+                  },
+                  i
+                )
+              )
+            )
+          : null
+      }
+    }
+    const root = nodeOps.createElement('div')
+    render(h(App), root)
+
+    expect(mapRefs()).toMatchObject(['1', '2', '3'])
+
+    list.push(4)
+    await nextTick()
+    expect(mapRefs()).toMatchObject(['1', '2', '3', '4'])
+
+    list.shift()
+    await nextTick()
+    expect(mapRefs()).toMatchObject(['2', '3', '4'])
+
+    show.value = !show.value
+    await nextTick()
+
+    expect(mapRefs()).toMatchObject([])
+
+    show.value = !show.value
+    await nextTick()
+    expect(mapRefs()).toMatchObject(['2', '3', '4'])
+  })
+
+
 })
index 249fb9e55e9f19e1468d32a360b6b2c2ef1f62ba..740624c917e7417ad76157c76de7c676d29f640d 100644 (file)
@@ -91,6 +91,9 @@ export function setRef(
             if (!isArray(existing)) {
               if (_isString) {
                 refs[ref] = [refValue]
+                if (hasOwn(setupState, ref)) {
+                  setupState[ref] = refs[ref]
+                }
               } else {
                 ref.value = [refValue]
                 if (rawRef.k) refs[rawRef.k] = ref.value