field.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <form action="javascript:return true" :class="['nut-field', { 'nut-filed-disabled': disabled }]">
  3. <span class="nut-require" v-if="requireShow">*</span>
  4. <span class="nut-field-label" v-if="label">{{ label }}</span>
  5. <div v-if="type === 'textarea'" class="nut-text-core">
  6. <textarea
  7. @change="$emit('change', currentValue)"
  8. ref="textarea"
  9. :maxlength="maxLength"
  10. :placeholder="placeholder"
  11. :rows="rows"
  12. :disabled="disabled"
  13. :readonly="readonly"
  14. @input="txtIptLength"
  15. v-model="currentValue">
  16. </textarea>
  17. <span v-if="limitShow">{{ txtNum }}/{{ maxLength }}</span>
  18. </div>
  19. <input
  20. :class="['input-'+state]"
  21. @change="$emit('change', currentValue)"
  22. @focus="focus"
  23. ref="input"
  24. :style="initCssStyle()"
  25. class="nut-field-core"
  26. :placeholder="placeholder"
  27. :number="type === 'number'"
  28. v-else
  29. :type="type"
  30. :disabled="disabled"
  31. :readonly="readonly"
  32. :value="currentValue"
  33. @input="handleInput">
  34. <div
  35. @click="handleClear"
  36. class="nut-textinput-clear"
  37. v-if="!disableClear"
  38. v-show="currentValue && type !== 'textarea' && active">
  39. <svg version="1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" >
  40. <path
  41. d="M8 0C3.6 0 0 3.6 0 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8zm2.8 9.7c.3.3.3.8 0 1.1s-.8.3-1.1 0L8 9.1l-1.7 1.7c-.3.3-.8.3-1.1 0-.3-.3-.3-.8 0-1.1L6.9 8 5.2 6.3c-.3-.3-.3-.8 0-1.1.3-.3.8-.3 1.1 0L8 6.9l1.7-1.7c.3-.3.8-.3 1.1 0 .3.3.3.8 0 1.1L9.1 8l1.7 1.7z"
  42. />
  43. </svg>
  44. </div>
  45. <div class="nut-field-other">
  46. <slot></slot>
  47. </div>
  48. <div class="nut-field-error" v-if="state" :class="['is-'+state]">
  49. </div>
  50. </form>
  51. </template>
  52. <script>
  53. export default {
  54. name: 'nut-field',
  55. data() {
  56. return {
  57. active: false,
  58. txtNum:0,
  59. currentValue: this.value
  60. };
  61. },
  62. props: {
  63. type: {
  64. type: String,
  65. default: 'text'
  66. },
  67. textAlign:{
  68. type: String,
  69. default: 'right'
  70. },
  71. limitShow:{
  72. type: Boolean,
  73. default:true
  74. },
  75. maxLength:{
  76. type:String,
  77. default:"50"
  78. },
  79. state: {
  80. type:String,
  81. default:""
  82. },
  83. requireShow:{
  84. type: Boolean,
  85. default:false
  86. },
  87. rows: String,
  88. label: String,
  89. placeholder: String,
  90. readonly: Boolean,
  91. disabled: Boolean,
  92. disableClear: {
  93. type: Boolean,
  94. default:false
  95. },
  96. autosize:{
  97. type: Boolean,
  98. default:false
  99. },
  100. value: String
  101. },
  102. mounted(){
  103. this.txtNum=this.value.length;
  104. if(this.limitShow==false){
  105. this.maxLength="";
  106. }
  107. },
  108. methods: {
  109. initCssStyle(){
  110. return `text-align:${this.textAlign}`
  111. },
  112. doCloseActive() {
  113. this.active = false;
  114. },
  115. focus(){
  116. if (this.disabled || this.readonly) return;
  117. this.active=true;
  118. },
  119. handleInput(evt) {
  120. this.currentValue = evt.target.value;
  121. this.$emit('input-func', evt.target.value);
  122. this.$emit('input', evt.target.value);
  123. },
  124. handleClear() {
  125. if (this.disabled || this.readonly) return;
  126. this.currentValue = '';
  127. },
  128. txtIptLength(event) {
  129. const data = event.target.value;
  130. const txtLength = data.length;
  131. this.txtNum = txtLength;
  132. if (txtLength > this.maxLength*1) {
  133. this.$emit('errorFunc');
  134. } else {
  135. }
  136. if(this.autosize){
  137. let height = this.$refs.textarea.scrollHeight;
  138. if (height) {
  139. this.$refs.textarea.style.height = height + 'px';
  140. }
  141. }
  142. this.$emit('input-func', data);
  143. this.$emit('input', data);
  144. }
  145. },
  146. watch: {
  147. value(val) {
  148. this.currentValue = val;
  149. },
  150. currentValue(val) {
  151. this.$emit('input', val);
  152. }
  153. }
  154. };
  155. </script>