Skip to content

Commit

Permalink
fix(templateRef): add Component type (#2203)
Browse files Browse the repository at this point in the history
Co-authored-by: 木荣 <372638156@qq.com>
  • Loading branch information
Holi0317 and murongg committed Sep 16, 2022
1 parent 4a65e57 commit cd7984c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/core/templateRef/index.test.ts
Expand Up @@ -72,4 +72,31 @@ describe('templateRef', () => {

expect(vm.targetEl).toBe(null)
})

it('support vue component as ref', async () => {
const ChildComponent = defineComponent({
name: 'ChildComponent',
render() {
return null
},
})

const vm = mount(defineComponent({
components: {
ChildComponent,
},
setup() {
const targetEl = templateRef<typeof ChildComponent>('target')
return {
targetEl,
}
},
render() {
return h(ChildComponent, { ref: 'target' })
},
}))

expect(vm.targetEl).toBeDefined()
expect(vm.targetEl.$options.name).toBe('ChildComponent')
})
})
4 changes: 2 additions & 2 deletions packages/core/templateRef/index.ts
@@ -1,4 +1,4 @@
import type { Ref } from 'vue-demi'
import type { Component, Ref } from 'vue-demi'
import { customRef, getCurrentInstance, onUpdated } from 'vue-demi'
import { tryOnMounted } from '@vueuse/shared'

Expand All @@ -9,7 +9,7 @@ import { tryOnMounted } from '@vueuse/shared'
* @param key
* @param initialValue
*/
export function templateRef<T extends HTMLElement | SVGElement | null>(
export function templateRef<T extends HTMLElement | SVGElement | Component | null>(
key: string,
initialValue: T | null = null,
): Readonly<Ref<T>> {
Expand Down

0 comments on commit cd7984c

Please sign in to comment.