u-input.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view class="u-input-container" :class="{'has-sumbit-button': sumbitButton}">
  3. <textarea v-if="type==='textarea'" type="digit" style="min-height:100rpx" @blur="blur" auto-height
  4. @focus="isFocus=true" class="u-input" :value="inputValue" :focus="isFocus"
  5. @input="e=>change(e.detail.value)" :placeholder="placeholder">
  6. </textarea>
  7. <input v-else-if="type==='digit'" type="digit" @blur="blur" @focus="isFocus=true" class="u-input"
  8. :value="inputValue" :focus="isFocus" @input="e=>change(e.detail.value)" :placeholder="placeholder">
  9. </input>
  10. <input v-else-if="type==='number'" type="number" @blur="blur" @focus="isFocus=true" class="u-input"
  11. :value="inputValue" :focus="isFocus" @input="e=>change(e.detail.value)" :placeholder="placeholder">
  12. </input>
  13. <input v-else @blur="blur" @focus="isFocus=true" class="u-input" :value="inputValue" :focus="isFocus"
  14. @input="e=>change(e.detail.value)" :placeholder="placeholder">
  15. </input>
  16. <uni-icons v-if="inputValue && isFocus" class="input-clear" @click="clear" type="clear" size="16"
  17. color="#9FA5BA" />
  18. <button v-if="sumbitButton" :disabled="!inputValue" @click="sumbit" class="sumbit-button" size="mini"
  19. type="primary">完成</button>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: "u-input",
  25. model: {
  26. prop: 'inputValue',
  27. event: 'change'
  28. },
  29. props: {
  30. inputValue: {
  31. type: String,
  32. default: ''
  33. },
  34. placeholder: {
  35. type: String,
  36. default: ''
  37. },
  38. sumbitButton: {
  39. type: Boolean,
  40. default: false
  41. },
  42. type: {
  43. type: String,
  44. default: ''
  45. },
  46. },
  47. data() {
  48. return {
  49. value: '',
  50. isFocus: false
  51. }
  52. },
  53. watch: {
  54. value: {
  55. handler() {
  56. this.$emit('change', this.value)
  57. },
  58. deep: false,
  59. immediate: true
  60. },
  61. },
  62. mounted() {
  63. this.value = this.inputValue
  64. },
  65. methods: {
  66. change(value) {
  67. this.$emit('change', value)
  68. },
  69. clear() {
  70. this.change('')
  71. const that = this
  72. setTimeout(function() {
  73. that.isFocus = true
  74. }, 200);
  75. },
  76. blur() {
  77. const that = this
  78. setTimeout(function() {
  79. that.isFocus = false
  80. that.setBlur()
  81. }, 100);
  82. },
  83. setBlur() {
  84. const that = this
  85. setTimeout(function() {
  86. that.isFocus === false && that.$emit('blur')
  87. }, 400);
  88. },
  89. sumbit() {
  90. this.$emit('sumbit', this.inputValue)
  91. const that = this
  92. setTimeout(function() {
  93. that.isFocus = true
  94. }, 200);
  95. }
  96. }
  97. }
  98. </script>
  99. <style lang="scss">
  100. .u-input-container {
  101. width: 100%;
  102. height: 100%;
  103. position: relative;
  104. display: flex;
  105. align-items: center;
  106. .u-input {
  107. width: 100%;
  108. height: 100%;
  109. position: relative;
  110. padding-top: 4rpx;
  111. }
  112. .input-clear {
  113. padding: 12rpx 0 12rpx 12rpx;
  114. cursor: pointer;
  115. }
  116. .sumbit-button {
  117. display: flex;
  118. justify-content: center;
  119. align-items: center;
  120. font-size: 28rpx;
  121. color: #FFFFFF;
  122. padding: 12rpx 16rpx;
  123. white-space: nowrap;
  124. line-height: 1;
  125. margin-left: 16rpx;
  126. }
  127. }
  128. .has-sumbit-button {}
  129. </style>
  130. <style lang="scss">
  131. input,
  132. textarea {
  133. font-size: 28rpx;
  134. font-weight: 400;
  135. color: #333;
  136. }
  137. .uni-input-placeholder {
  138. font-size: 28rpx;
  139. font-weight: 400;
  140. color: #A3ABBF;
  141. }
  142. </style>