<!-- 只能输入纯汉字的input框 -->
<template>
  <el-input v-model="inputVal" :maxlength="maxLen" type="text" :disabled="disabled" :placeholder="placeholder" @input="handleInput" />
</template>

<script>
/**
 * usage: <cy-character-input v-model="ruleForm.testVal" />
 */
export default {
  props: {
    maxLen: {
      type: Number,
      default: 255
    },
    placeholder: {
      type: String,
      default: '请输入文字'
    },
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputVal: ''
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val && val !== '') {
          this.handleInput(String(val))
        }
      },
      immediate: true
    },
    inputVal(val) {
      this.handleInput(val)
    }
  },
  methods: {
    handleInput(val) {
      // 移除非文字字符
      this.inputVal = val.replace(/[^A-Za-z\u4e00-\u9fa5]/g, '')
      this.$emit('input', this.inputVal)
    }
  }
}
</script>

<style lang="scss" scoped></style>
