I have been trying to insert emoji in textarea exactly where the curse is at. I looked around how tos
in the web could not find anything specific in VUE JS. Most of them is in plain JS.
I have this Code
<div class="picker" v-show="showPicker">
<click-outside :handler="handleClickOutside">
<picker
set ="messenger"
title="Pick your emoji…"
emoji="point_up"
@click="addEmoji"
:emoji-size="16"
>
</picker>
</click-outside>
</div>
<textarea id="greeting_text_input" class="form-control"
type="text"
v-model="greeting_text"
rows="8"
required
placeholder="Hi {first-name}! Welcome to our bot. Click on the ‘Get
Started’ button to begin
">
</textarea>
My Method
addEmoji(emoji){
this.greeting_text += emoji.native;
this.showPicker = !this.showPicker;
}
Obviously this code will add the character (emoji, in my case) to the last of the string. I need a pure vuejs solution for this. What would be the best practise for this kind of problem in vue? as there are few solutions in web that based either in vanilla JS or Jquery.
I learned about
setSelectionRange
from a different question, and I used it to handle credit card number input. I will show my solution here so a person can perhaps become inspired by it.template:
instance methods:
The goal with the above code is to add spaces into a credit card input, so
1234123412341234
is automatically reformatted to1234 1234 1234 1234
. A person venturing into this territory will notice that problems arise when editing the input value.You can see there are three conditions in my sample above. The last one is the default which simply reformats the current value with a 2-step combo: remove all spaces then adds a space every 4th character.
If you comment out the two
if
blocks, you can watch the problems emerge.The first
if
block handles the backspace event. As you can see, every time the input changes, the value is captured asthis.lastValue
. When you press backspace, the goal of the first condition is to NOT run the regex. In my opinion, this is better UX. If you comment out that condition, you can see.The second
if
block handles the editing events. A good way to test it, is to enter a valid CC but omit the 3rd character, so that everything is off by one. Then add the character in. Everything should be good. Likewise if you backspace multiple characters out. The goal of the second condition is to properly manage the cursor position (or caret position if you prefer that nomenclature).You can safely delete the first condition and all references to
lastValue
and the code will still work. This is arguably simpler but worse UX.Two steps:
1 get
textarea
element using a vue-way:1.1 Add
ref
attrbute totextarea
tag in your template code:1.2 get this element after
mounted
hook of this component:2 get cursor position of
textarea
element.Here is reference: ref