fix(runtime-core/template-ref): named ref in v-for regression fix (#5118)
close #5116 close #5447 close #5525
This commit is contained in:
		
							parent
							
								
									7efb9dba30
								
							
						
					
					
						commit
						cee1eafb4d
					
				@ -442,4 +442,59 @@ describe('api: template refs', () => {
 | 
				
			|||||||
    await nextTick()
 | 
					    await nextTick()
 | 
				
			||||||
    expect(mapRefs()).toMatchObject(['2', '3', '4'])
 | 
					    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'])
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
				
			|||||||
@ -91,6 +91,9 @@ export function setRef(
 | 
				
			|||||||
            if (!isArray(existing)) {
 | 
					            if (!isArray(existing)) {
 | 
				
			||||||
              if (_isString) {
 | 
					              if (_isString) {
 | 
				
			||||||
                refs[ref] = [refValue]
 | 
					                refs[ref] = [refValue]
 | 
				
			||||||
 | 
					                if (hasOwn(setupState, ref)) {
 | 
				
			||||||
 | 
					                  setupState[ref] = refs[ref]
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
              } else {
 | 
					              } else {
 | 
				
			||||||
                ref.value = [refValue]
 | 
					                ref.value = [refValue]
 | 
				
			||||||
                if (rawRef.k) refs[rawRef.k] = ref.value
 | 
					                if (rawRef.k) refs[rawRef.k] = ref.value
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user