Skip to content

Commit

Permalink
fix(template-ref): preserve ref removal behavior in non-composition-a…
Browse files Browse the repository at this point in the history
…pi usage

close #12554
  • Loading branch information
yyx990803 committed Jun 20, 2022
1 parent 0fabda7 commit 2533a36
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core/vdom/modules/template-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function registerRef(vnode: VNodeWithData, isRemoval?: boolean) {
const vm = vnode.context
const refValue = vnode.componentInstance || vnode.elm
const value = isRemoval ? null : refValue
const $refsValue = isRemoval ? undefined : refValue

if (isFunction(ref)) {
invokeWithErrorHandling(ref, vm, [value], vm, `template ref function`)
Expand Down Expand Up @@ -67,14 +68,14 @@ export function registerRef(vnode: VNodeWithData, isRemoval?: boolean) {
if (isRemoval && refs[ref] !== refValue) {
return
}
refs[ref] = value
refs[ref] = $refsValue
setSetupRef(vm, ref, value)
} else if (_isRef) {
if (isRemoval && ref.value !== refValue) {
return
}
ref.value = value
if (setupRefKey) refs[setupRefKey] = value
if (setupRefKey) refs[setupRefKey] = $refsValue
} else if (__DEV__) {
warn(`Invalid template ref type: ${typeof ref}`)
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/features/template-ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('ref', () => {
expect(vm.$refs.foo).toBe(vm.$el)
vm.value = 'bar'
waitForUpdate(() => {
expect(vm.$refs.foo).toBe(null)
expect(vm.$refs.foo).toBe(undefined)
expect(vm.$refs.bar).toBe(vm.$el)
}).then(done)
})
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('ref', () => {
vm.test = ''
})
.then(() => {
expect(vm.$refs.test).toBe(null)
expect(vm.$refs.test).toBe(undefined)
})
.then(done)
})
Expand Down

0 comments on commit 2533a36

Please sign in to comment.