Closed jd-solanki closed 1 month ago
Vue still not support forwardRef
.
You can do something like
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { Textarea } from '@/components/ui/textarea';
import { unrefElement } from '@vueuse/core'
const refTextarea = ref();
onMounted(() => {
refTextarea.value.$el.focus()
// or
unrefElement(refTextarea).focus()
});
</script>
<template>
<Textarea ref="refTextarea" placeholder="Type your message here." />
</template>
Related resources
Thanks. I was aware of this but I didn't tried this because my console log had empty ref value, Check below image:
I think when you use ref on vue component it doesn't give empty ref like above image it logs the component instance and we can inspect the underlying element $el
:
I've updated the reproduction example I guess
Anyways, It indeed solves my query hence closing the issue, Thanks 😇
Reproduction
https://stackblitz.com/edit/t6hymg?file=src%2FApp.vue
Describe the bug
Hi 👋🏻
I want to manually focus textarea when espace key is pressed. However, When I use template ref it doesn't give textarea element 🤷🏻♂️
System Info
Contributes