all files / src/textField/ enhancedTextarea.vue

0% Statements 0/23
0% Branches 0/12
0% Functions 0/1
0% Lines 0/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
<template>
  <div class="mu-text-field-multiline">
    <textarea ref="textareaHidden" rows="1" class="mu-text-field-textarea-hide mu-text-field-input" :value="value"></textarea>
    <textarea ref="textarea" class="mu-text-field-input mu-text-field-textarea"
      :class="normalClass" :value="value" @change="handleChange"
      @input="handleInput" @focus="handleFocus" @blur="handleBlur"
      :placeholder="placeholder" :disabled="disabled"></textarea>
  </div>
</template>
 
<script>
export default {
  props: {
    placeholder: {
      type: String
    },
    value: {
      type: String
    },
    rows: {
      type: Number,
      default: 1
    },
    rowsMax: {
      type: Number
    },
    disabled: {
      type: Boolean,
      default: false
    },
    normalClass: {
      type: [String, Array, Object]
    }
  },
  methods: {
    resizeTextarea () {
      let element = this.$refs.textarea
      if (!element) return
      let hiddenEl = this.$refs.textareaHidden
      let lineHeight = window.getComputedStyle(element, null).getPropertyValue('line-height')
      lineHeight = Number(lineHeight.substring(0, lineHeight.indexOf('px')))
      let pt = window.getComputedStyle(element, null).getPropertyValue('padding-top')
      pt = Number(pt.substring(0, pt.indexOf('px')))
      let pd = window.getComputedStyle(element, null).getPropertyValue('padding-bottom')
      pd = Number(pd.substring(0, pd.indexOf('px')))
      let minHeight = pd + pt + lineHeight * this.rows
      let maxHeight = pd + pt + lineHeight * (this.rowsMax || 0)
      let height = hiddenEl.scrollHeight
      element.style.height = (height < minHeight ? minHeight : height > maxHeight && maxHeight > 0 ? maxHeight : height) + 'px'
    },
    handleInput (e) {
      this.$emit('input', e.target.value)
    },
    handleChange (e) {
      this.$emit('change', e)
    },
    handleFocus (e) {
      this.$emit('focus', e)
    },
    handleBlur (e) {
      this.$emit('blur', e)
    }
  },
  mounted () {
    this.resizeTextarea()
  },
  watch: {
    value (val, oldVal) {
      if (val === oldVal) return
      this.$nextTick(() => {
        this.resizeTextarea()
      })
    }
  }
}
</script>
 
<style lang="less">
.mu-text-field-textarea{
  resize: vertical;
  line-height: 1.5;
  position: relative;
  height: 100%;
  resize: none;
}
.mu-text-field-multiline{
  width: 100%;
  position: relative;
}
.mu-text-field-textarea-hide{
  width: 100%;
  height: initial;
  resize: none;
  position: absolute;
  padding: 0;
  overflow: auto;
  visibility: hidden;
}
</style>