vue--自定义带clearable属性的文本域input组件

今天写页面时遇到一个需求:input框类型是textarea,且支持清除。兴冲冲的直接用以下方式试了一下:

<el-input v-model="val" clearable :rows="5" type="textarea"/>

发现输入框右侧并不会出现清除符号,查了半天发现clearable属性和在type="textarea"时会失效,于是自己封装了一个支持清空的文本域输入框组件,拿来即用!

<template>
  <div class="textInput">
    <el-input
      :rows="rows"
      :placeholder="placeholder"
      v-model="localValue"
      @change="inputChange"
      type="textarea"/>
    <i
      v-show="clearable && localValue"
      class="clearButton el-input__icon el-icon-circle-close el-input__clear"
      @click.prevent="localValue=''"
    />
  </div>
</template>

<script>
export default {
name: "ClearableTextInput",
  props: {
    values: {
      type: String,
      default: function() {
        return ''
      }
    },
    placeholder: {
      type: String,
      default: ''
    },
    clearable: {
      type: Boolean,
      default: false
    },
    rows: {
      type: Number,
      default: 1
    },
    inputChange: {
      type: Function,
      default: null
    },
  },
  computed: {
    localValue: {
      get() {
        return this.values;
      },
      set(value) {
        this.$emit('input', value)
      }
    }
  },

}
</script>

<style lang="scss" scoped>
  .textInput {
    position: relative;

    .clearButton {
      position: absolute;
      border-radius: 5px;
      right: 3px;
      z-index: 2;
      border: none;
      top: -3px;
      height: 40px;
      cursor: pointer;
      color: #ccc;
      transform: translateX(2px);
    }

    .clearButton:hover {
      color: #878d99;
    }
  }
</style>

使用示例:

<template>
<div>
    <ClearableTextInput
      :rows="5"
      :placeholder="placeholder"
      v-model="values"
      :values="values"
      :clearable="true"
      :input-change="inputChangeMethod"
    />
</div>
</template>

<script>
export default {
  name: "index",
  components: {
    'ClearableTextInput': resolve => require([('xxxx/ClearableTextInput')], resolve)
  },
  data() {
    return {
      values: '',
      placeholder: 'this is a demo'
    }
  },
  methods: {
   inputChangeMethod() {
     console.log('input changed')
   }
 }   
}
</script>

<style lang="scss" scoped>

</style>

效果:

 

 

  • 19
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值