0

I have Vue app and I would like to add Facebook inspired buttons in-lined in a comment form. I had plain JS prototype that was working. When I tried to incorporate it into the Vue app I faced an issue with multiple occurrences of the component on the page because it heavily used element id. I would be able to declare dynamic id but this approach was not working well because <style> is static. I found refs later and got inspired by this wonderful article: https://www.telerik.com/blogs/how-to-target-the-dom-in-vue. Long story short: my callback is never called.

Code sandbox

Source code:

<b-form-textarea
  class="textarea" ref="textareaRef"
  rows="1" max-rows="8"
  @oninput.native = "adjustIconsInTextarea"
  :placeholder="$t('comment.write-comment-placeholder')"
  v-model="text"
>

<div class="icons" ref="iconsRef">
  <b-button :id="`emoji_list_${commentId}`" class="mt-2" variant="outline" size="sm">
    &#x1F600;
  </b-button>
</div>

methods: {
adjustIconsInTextarea() {
  const textComment = this.$refs.textareaRef;
  console.log(textComment.value.length);
  const icons = this.$refs.iconsRef;
  if (textComment.value.length > 140) {
    textComment.style.padding = '13px 50px 34px 32px';
    icons.style.top = '-36px';
    icons.style.right = '72px';
  } else {
    textComment.style.padding = '10px 174px 5px 28px';
    icons.style.top = '-45px';
    icons.style.right = '68px';
  }
},

Bootstrap-vue b-form-textarea's events. Where is the error?

2
  • 1
    you should use @input= "adjustIconsInTextarea" Commented Jul 29, 2020 at 20:07
  • That's it, thank you. Commented Jul 29, 2020 at 20:09

1 Answer 1

1

According to the docs:

All native events (other than the custom input and change events) are supported, without the need for the .native modifier.

so you should do :

@input= "adjustIconsInTextarea"

and pass use the event parameter target instead of refs for the textarea :

methods: {
adjustIconsInTextarea(event) {
  const textComment = event.target;
  console.log(textComment.value.length);
  const icons = this.$refs.iconsRef;
  if (textComment.value.length > 140) {
    textComment.style.padding = '13px 50px 34px 32px';
    icons.style.top = '-36px';
    icons.style.right = '72px';
  } else {
    textComment.style.padding = '10px 174px 5px 28px';
    icons.style.top = '-45px';
    icons.style.right = '68px';
  }
},
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your hint. Now the problem is that vue component does not have the style property like DOM element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.