I have an input field that automatically replaces with a text area and the same content based on the number of characters entered by the user:
<textarea v-if="myString.length > 20" v-model="myString"/> <input type="text" v-if="myString.length <= 20" v-model="myString"/>
The problem I have is that the focus is lost when the user enters the 21st character. Therefore, the user gets annoyed because when he types the 22nd character, the character does not appear in the text area (has no focus). How can I set focus on the newly rendered text area? The problem here is that it renders automatically. Otherwise I can set a reference on the textarea and call focus().
Another problem is removing the 21st character and switching back from the textarea to the input element.
(Copy my comment as this may be helpful)
Replacing
input
elements withtextarea
may produce a poor user experience. Why not just usetextarea
from the beginning? If you want the style to change based on the input length, for example increasing the height if it's more than 20 characters, then you can do that using CSS.Demo (forked from @tony19)
You can wrap the
textarea
/input
in a component and use itsmounted
hook to call itsfocus()
, As shown in the following components:Demo
While this is technically possible, this user experience may not be ideal, and you should consider other designs that don't require centralized input like this (as @kien_coi_1997 suggests).