Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: named ref in v-for regression fix(#5116) #5118

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})


})
3 changes: 3 additions & 0 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down