textbox.vue 2.0 KB

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