textbox.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div class="nut-textbox">
  3. <div class="txt-area" :class="{ error: errorState, 'num-none': limitShow == false, disabled: disabled }" :style="{ background: textBgColor }">
  4. <textarea
  5. :placeholder="placeText"
  6. :style="{ height: txtAreaHeight + 'px' }"
  7. v-model="currentValue"
  8. :disabled="disabled"
  9. @input="txtIptLength"
  10. :maxlength="iptMaxlength"
  11. ></textarea>
  12. <span v-show="limitShow">{{ txtNum }}/{{ maxNum }}</span>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'nut-textbox',
  19. props: {
  20. value: {
  21. type: [String, Number],
  22. default: ''
  23. },
  24. disabled: {
  25. type: Boolean,
  26. default: false
  27. },
  28. maxNum: {
  29. type: [String, Number],
  30. default: 50
  31. },
  32. placeText: {
  33. type: String,
  34. default: '请您在此输入'
  35. },
  36. txtAreaH: {
  37. type: [String, Number],
  38. default: '50'
  39. },
  40. switchMax: {
  41. type: Boolean,
  42. default: false
  43. },
  44. textBgColor: {
  45. type: String,
  46. default: '#fff'
  47. },
  48. limitShow: {
  49. type: Boolean,
  50. default: true
  51. }
  52. },
  53. data() {
  54. return {
  55. errorState: false,
  56. txtNum: 0
  57. };
  58. },
  59. mounted() {},
  60. computed: {
  61. currentValue: {
  62. get() {
  63. this.txtNum = this.value.length;
  64. return this.value;
  65. },
  66. set(val) {}
  67. },
  68. txtAreaHeight: function() {
  69. let txtAreaH;
  70. txtAreaH = this.txtAreaH;
  71. return txtAreaH;
  72. },
  73. iptMaxlength() {
  74. let maxlength;
  75. if (this.switchMax) {
  76. maxlength = this.maxNum;
  77. }
  78. return maxlength;
  79. }
  80. },
  81. methods: {
  82. txtIptLength(event) {
  83. const data = event.target.value;
  84. const txtLength = data.length;
  85. this.txtNum = txtLength;
  86. if (txtLength > this.maxNum) {
  87. this.errorState = true;
  88. this.$emit('errorFunc');
  89. } else {
  90. this.errorState = false;
  91. }
  92. this.$emit('inputFunc', data);
  93. this.$emit('input', data);
  94. }
  95. }
  96. };
  97. </script>