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

[Bug]: There's no way to manually focus the textarea #564

Closed
1 of 2 tasks
jd-solanki opened this issue May 22, 2024 · 2 comments
Closed
1 of 2 tasks

[Bug]: There's no way to manually focus the textarea #564

jd-solanki opened this issue May 22, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jd-solanki
Copy link

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

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 83.38 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.11.1/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.6.12 - ~/Library/pnpm/pnpm
    bun: 1.0.29 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 118.1.59.117
    Chrome: 124.0.6367.210
    Safari: 17.4.1
  npmPackages:
    @vueuse/core: ^10.9.0 => 10.9.0 
    nuxt: ^3.11.2 => 3.11.2 
    radix-vue: ^1.7.4 => 1.7.4 
    shadcn-nuxt: ^0.10.4 => 0.10.4 
    vue: ^3.4.27 => 3.4.27

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
@jd-solanki jd-solanki added the bug Something isn't working label May 22, 2024
@sadeghbarati
Copy link
Collaborator

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

@jd-solanki
Copy link
Author

Thanks. I was aware of this but I didn't tried this because my console log had empty ref value, Check below image:
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:
image

I've updated the reproduction example I guess

Anyways, It indeed solves my query hence closing the issue, Thanks πŸ˜‡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants