app-input.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 === 0 ? 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%'}"></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. default: 'color: #999999',
  99. },
  100. maxLength: String,
  101. value: {
  102. default: '',
  103. },
  104. round: Boolean,
  105. border: Number,
  106. borderColor: {
  107. default: '#c0c4cc',
  108. },
  109. icon: String,
  110. center: Boolean,
  111. size: String,
  112. width: String,
  113. color: {
  114. default: '#353535',
  115. type: String,
  116. },
  117. height: String,
  118. backgroundColor: String,
  119. radius: Number,
  120. focus: {
  121. type: Boolean,
  122. default: false,
  123. },
  124. paddingLeft: {
  125. default: 12,
  126. },
  127. defaultValue: {
  128. default: '',
  129. },
  130. },
  131. data() {
  132. return {
  133. newValue: this.value
  134. }
  135. },
  136. methods: {
  137. blur(event) {
  138. this.$emit('blur', event.detail);
  139. },
  140. confirm(event) {
  141. this.$emit('confirm', event.detail);
  142. },
  143. changeValue(value) {
  144. this.$emit('input', value.detail.value);
  145. }
  146. },
  147. computed: {
  148. setHeight: function () {
  149. if (this.height) return this.height;
  150. switch (this.size) {
  151. case 'large':
  152. return 100;
  153. case 'small':
  154. return 60;
  155. case 'medium':
  156. return 80;
  157. default:
  158. return 80;
  159. }
  160. }
  161. }
  162. }
  163. </script>
  164. <style scoped lang="scss">
  165. .app-input {
  166. border-radius: #{10rpx};
  167. box-sizing: border-box;
  168. outline: none;
  169. height: #{80rpx};
  170. width: 100%;
  171. line-height: #{80rpx};
  172. color: $uni-general-color-three;
  173. }
  174. .is-border {
  175. border: #{2rpx} solid #dcdfe6;
  176. }
  177. .app-input:active {
  178. border-color: #c0c4cc
  179. }
  180. .app-input.is-disabled {
  181. background-color: #f5f7fa;
  182. border-color: #e4e7ed;
  183. color: #c0c4cc;
  184. cursor: not-allowed;
  185. }
  186. .is-round {
  187. border-radius: #{50rpx};
  188. }
  189. .app-view {
  190. position: relative;
  191. }
  192. .app-icon {
  193. width: #{25rpx};
  194. height: #{25rpx};
  195. position: absolute;
  196. top: 50%;
  197. transform: translateY(-50%);
  198. background-repeat: no-repeat;
  199. background-size: 100% 100%;
  200. }
  201. .app-input-textarea {
  202. max-height: 100%;
  203. width: 100%;
  204. word-wrap: break-word;
  205. }
  206. /* #ifdef MP-ALIPAY */
  207. .app-input,
  208. .app-input-textarea {
  209. overflow: hidden;
  210. }
  211. /* #endif */
  212. </style>