app-input.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="app-view"
  3. :style="{'height': `${setHeight}rpx`, 'width': `${width}rpx`}"
  4. >
  5. <template v-if="type !== 'textarea'">
  6. <!-- #ifndef MP-TOUTIAO -->
  7. <input :disabled="disabled"
  8. :value="value ? value : defaultValue"
  9. :class="{'is-disabled': disabled, 'is-round': round, 'is-border': border}"
  10. class="app-input"
  11. :type="type"
  12. :password="password"
  13. :placeholder-style="placeholderStyle"
  14. :placeholder="placeholder"
  15. :focus="focus"
  16. :style="{
  17. 'padding-left': !center && icon ? '30rpx' : `${paddingLeft}rpx`,
  18. 'text-align': center ? 'center' : 'none',
  19. 'height': `${setHeight}rpx`,
  20. 'width': `${width}rpx`,
  21. 'color': `${color}`,
  22. backgroundColor: backgroundColor,
  23. borderRadius: `${radius}rpx`,
  24. borderColor: borderColor,
  25. }"
  26. @input="changeValue"
  27. @blur="blur"
  28. @confirm="confirm"
  29. />
  30. <!-- #endif -->
  31. <!-- MP-TOUTIAO focus属性有问题,去除。2020-02-24 -->
  32. <!-- #ifdef MP-TOUTIAO -->
  33. <input :disabled="disabled"
  34. :value="value || value === 0 ? value : defaultValue"
  35. :class="{'is-disabled': disabled, 'is-round': round, 'is-border': border}"
  36. class="app-input"
  37. :type="type"
  38. :password="password"
  39. :placeholder-style="placeholderStyle"
  40. :placeholder="placeholder"
  41. :style="{
  42. 'padding-left': !center && icon ? '30rpx' : `${paddingLeft}rpx`,
  43. 'text-align': center ? 'center' : 'none',
  44. 'height': `${setHeight}rpx`,
  45. 'width': `${width}rpx`,
  46. 'color': `${color}`,
  47. backgroundColor: backgroundColor,
  48. borderRadius: `${radius}rpx`,
  49. borderColor: borderColor,
  50. }"
  51. @input="changeValue"
  52. @blur="blur"
  53. @confirm="confirm"
  54. />
  55. <!-- #endif -->
  56. <icon v-if="icon" class="app-icon"
  57. :style="{'background-image': `url(${icon})`, 'left': center ? '42%': '1%'}" type></icon>
  58. </template>
  59. <template v-else>
  60. <textarea :value="value ? value : defaultValue"
  61. :maxlength="maxLength ? maxLength : -1"
  62. @blur="blur"
  63. @confirm="confirm"
  64. :placeholder-style="placeholderStyle"
  65. :show-confirm-bar="showConfirmBar"
  66. :placeholder="placeholder"
  67. :auto-height="autoHeight"
  68. :disabled="disabled"
  69. @input="changeValue"
  70. class="app-input-textarea"
  71. :class="{'is-disabled': disabled, 'is-round': round, 'is-border': border}"
  72. :style="{
  73. 'color': `${color}`,
  74. backgroundColor: backgroundColor,
  75. borderRadius: `${radius}rpx`,
  76. padding: `24rpx ${paddingLeft}rpx`,
  77. borderColor: borderColor,
  78. }"
  79. >
  80. </textarea>
  81. </template>
  82. </view>
  83. </template>
  84. <script>
  85. export default {
  86. name: 'app-input',
  87. props: {
  88. type: String,
  89. password: Boolean,
  90. disabled: Boolean,
  91. placeholder: {
  92. default: '',
  93. type: String,
  94. },
  95. autoHeight: Boolean,
  96. showConfirmBar: Boolean,
  97. placeholderStyle: {
  98. type: String,
  99. default: function () {
  100. return 'color: #999999';
  101. }
  102. },
  103. maxLength: String,
  104. value: {
  105. default: '',
  106. },
  107. round: Boolean,
  108. border: {
  109. type: [Number, Boolean]
  110. },
  111. borderColor: {
  112. default: '#c0c4cc',
  113. },
  114. icon: String,
  115. center: Boolean,
  116. size: String,
  117. width: String,
  118. color: {
  119. default: '#353535',
  120. type: String,
  121. },
  122. height: String,
  123. backgroundColor: String,
  124. radius: Number,
  125. focus: {
  126. type: Boolean,
  127. default: false,
  128. },
  129. paddingLeft: {
  130. default: 12,
  131. },
  132. defaultValue: {
  133. default: '',
  134. },
  135. },
  136. data() {
  137. return {
  138. newValue: this.value
  139. }
  140. },
  141. methods: {
  142. blur(event) {
  143. this.$emit('blur', event.detail);
  144. },
  145. confirm(event) {
  146. this.$emit('confirm', event.detail);
  147. },
  148. changeValue(value) {
  149. this.$emit('input', value.detail.value);
  150. }
  151. },
  152. computed: {
  153. setHeight: function () {
  154. if (this.height) return this.height;
  155. switch (this.size) {
  156. case 'large':
  157. return 100;
  158. case 'small':
  159. return 60;
  160. case 'medium':
  161. return 80;
  162. default:
  163. return 80;
  164. }
  165. }
  166. }
  167. }
  168. </script>
  169. <style scoped lang="scss">
  170. .app-input {
  171. border-radius: #{10rpx};
  172. box-sizing: border-box;
  173. outline: none;
  174. height: #{80rpx};
  175. width: 100%;
  176. line-height: #{80rpx};
  177. color: $uni-general-color-three;
  178. }
  179. .is-border {
  180. border: #{2rpx} solid #dcdfe6;
  181. }
  182. .app-input:active {
  183. border-color: #c0c4cc
  184. }
  185. .app-input.is-disabled {
  186. background-color: #f5f7fa;
  187. border-color: #e4e7ed;
  188. color: #c0c4cc;
  189. cursor: not-allowed;
  190. }
  191. .is-round {
  192. border-radius: #{50rpx};
  193. }
  194. .app-view {
  195. position: relative;
  196. }
  197. .app-icon {
  198. width: #{25rpx};
  199. height: #{25rpx};
  200. position: absolute;
  201. top: 50%;
  202. transform: translateY(-50%);
  203. background-repeat: no-repeat;
  204. background-size: 100% 100%;
  205. }
  206. .app-input-textarea {
  207. max-height: 100%;
  208. width: 100%;
  209. word-wrap: break-word;
  210. }
  211. /* #ifdef MP-ALIPAY */
  212. .app-input,
  213. .app-input-textarea {
  214. overflow: hidden;
  215. }
  216. /* #endif */
  217. </style>