index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="m-textarea-default">
  3. <textarea
  4. :class="styleClass"
  5. :cols="cols"
  6. :rows="rows"
  7. :placeholder="placeholder"
  8. :value="modelValue"
  9. :disabled="disabled"
  10. :maxlength="maxlength"
  11. :showWordLimit="showWordLimit"
  12. :readonly="readonly"
  13. @input="input"
  14. @focus="focus"
  15. @blur="blur">
  16. </textarea>
  17. <div class="max-length-box" v-if="maxLengthBoxFlag">{{`${n}/${maxlength}`}}</div>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: "mTextarea"
  23. };
  24. </script>
  25. <script setup>
  26. import { ref,computed } from 'vue-demi'
  27. const emit = defineEmits(['update:modelValue','input','focus','blur'])
  28. const props = defineProps({
  29. modelValue: {
  30. type: String,
  31. default: ''
  32. },
  33. placeholder: {
  34. type: String,
  35. default: '请输入'
  36. },
  37. rows:{
  38. type:Number,
  39. default:4
  40. },
  41. cols:{
  42. type:Number,
  43. default:50
  44. },
  45. disabled: {
  46. type: Boolean,
  47. default: false
  48. },
  49. maxlength: Number,
  50. showWordLimit: {
  51. type: Boolean,
  52. default: false,
  53. },
  54. readonly: {
  55. type: Boolean,
  56. default: false
  57. }
  58. })
  59. const n = ref(props.modelValue.length || 0)
  60. const styleClass = computed(() => {
  61. return {
  62. 'm-textarea-disabled': props.disabled,
  63. }
  64. })
  65. const maxLengthBoxFlag = computed(() => {
  66. return props.maxlength != undefined && props.maxlength > 0 && props.showWordLimit
  67. })
  68. const input = (e)=>{
  69. n.value = e.target.value.length
  70. if(n.value >= props.maxlength){
  71. n.value = props.maxlength
  72. }
  73. emit('update:modelValue',e.target.value)
  74. emit('input',e.target.value)
  75. }
  76. const focus = (e)=>{
  77. emit('focus',e)
  78. }
  79. const blur = (e)=>{
  80. emit('blur',e)
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. @import '../../styles/components/textarea.scss';
  85. </style>