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(templateRef): add Component type #2203

Merged
merged 2 commits into from Sep 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's best to use unmount when you're done.

})
})
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