LoginSignup
5
2

More than 3 years have passed since last update.

[Vue.js]改行された時に改行分の高さをとるテキストエリア

Last updated at Posted at 2019-05-17

やりたいこと

普段何気なくテキストエリアに文字を入れたり、改行したりしますが、
その際、改行分の高さをとってくれるようなテキストエリアのcomponentをつくりたかった

ソース

javascript BaseTextarea.vue

<template>
  <textarea
    ref="adjust_textarea"
    v-model="textareaVal"
    class="c-input-textarea"
    :placeholder="placeholder"
    @keydown="adjustHeight"
  />
</template>

<script>
export default {
  name: 'BaseTextarea',
  props: {
    placeholder: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      textareaVal: '',
    }
  },
  methods: {
    adjustHeight() {
      const textarea = this.$refs.adjust_textarea
      const resetHeight = new Promise(function(resolve) {
        resolve((textarea.style.height = 'auto'))
      })
      resetHeight.then(function() {
        textarea.style.height = textarea.scrollHeight + 'px'
      })
    },
  },
}
</script>


5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2