index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="jnpf-input">
  3. <template v-if="!detailed">
  4. <view class="input-content" :class="{'input-border':addonBefore||addonAfter}">
  5. <view class="input-left u-line-1" v-if="addonBefore">{{addonBefore}}</view>
  6. <view class="input-center">
  7. <u-input input-align='right' :border="false" v-model="innerValue"
  8. :type="showPassword?'password':'text'" :maxlength="maxlength||maxlength===0?maxlength:9999"
  9. :placeholder="placeholder" :disabled="disabled" :clearable='disabled ? false : clearable'
  10. @input="onInput" @blur="onBlur" :class="{'input-disabled':disabled}" />
  11. </view>
  12. <!-- #ifndef H5 -->
  13. <text class=" icon-ym icon-ym-scanCode1" v-if="useScan" @click="scanCode" />
  14. <!-- #endif -->
  15. <view class="input-right u-line-1" v-if="addonAfter">{{addonAfter}}</view>
  16. <view class="input-count" v-if="showCount&&!addonBefore&&!addonAfter">
  17. <text>{{ innerValue?String(innerValue).length:0 }}</text><text
  18. v-if="maxlength">/{{ maxlength }}</text>
  19. </view>
  20. </view>
  21. </template>
  22. <view class="detail-text" :class="{ ellipsis: showOverflow ,['detail-text-'+align]:true,disabled:disabled }"
  23. v-else>
  24. <text class="detail-text-addon" v-if="addonBefore">{{ addonBefore }}</text>
  25. {{ maskedValue }}
  26. <text class="detail-text-addon" v-if="addonAfter">{{ addonAfter }}</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import {
  32. useTextMask
  33. } from './useTextMask';
  34. export default {
  35. name: 'jnpf-input',
  36. props: {
  37. modelValue: {
  38. type: [String, Number],
  39. default: ''
  40. },
  41. placeholder: {
  42. type: String,
  43. default: '请输入'
  44. },
  45. maxlength: {
  46. type: Number,
  47. default: null
  48. },
  49. showPassword: {
  50. type: Boolean,
  51. default: false
  52. },
  53. disabled: {
  54. type: Boolean,
  55. default: false
  56. },
  57. clearable: {
  58. type: Boolean,
  59. default: false
  60. },
  61. detailed: {
  62. type: Boolean,
  63. default: false
  64. },
  65. showOverflow: {
  66. type: Boolean,
  67. default: false
  68. },
  69. addonBefore: {
  70. type: String,
  71. default: ''
  72. },
  73. addonAfter: {
  74. type: String,
  75. default: ''
  76. },
  77. align: {
  78. type: String,
  79. default: 'right'
  80. },
  81. useScan: {
  82. type: Boolean,
  83. default: false
  84. },
  85. useMask: {
  86. type: Boolean,
  87. default: false
  88. },
  89. maskConfig: {
  90. type: Object,
  91. default: () => {}
  92. },
  93. showCount: {
  94. type: Boolean,
  95. default: false
  96. },
  97. },
  98. data() {
  99. return {
  100. innerValue: '',
  101. maskedValue: '',
  102. }
  103. },
  104. watch: {
  105. modelValue: {
  106. handler(val) {
  107. this.innerValue = val
  108. if (!this.useMask) return (this.maskedValue = val);
  109. const {
  110. getMaskedText
  111. } = useTextMask(this.maskConfig);
  112. this.maskedValue = getMaskedText(val);
  113. },
  114. immediate: true,
  115. }
  116. },
  117. methods: {
  118. onInput(val) {
  119. this.$nextTick(() => {
  120. this.$emit('update:modelValue', val)
  121. this.$emit('change', val)
  122. })
  123. },
  124. onBlur(val) {
  125. this.$emit('blur', val)
  126. },
  127. isJSON(str) {
  128. try {
  129. var obj = JSON.parse(str);
  130. if (typeof obj == 'object' && obj) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. } catch (e) {
  136. return false;
  137. }
  138. },
  139. scanCode() {
  140. uni.scanCode({
  141. success: res => {
  142. if (!res.result || typeof res.result !== 'string') return
  143. this.onInput(res.result)
  144. }
  145. });
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss">
  151. .jnpf-input {
  152. width: 100%;
  153. .input-content {
  154. display: flex;
  155. border-radius: 10rpx;
  156. height: 74rpx;
  157. &.input-border {
  158. border: 1rpx solid rgb(220, 223, 230)
  159. }
  160. .input-center {
  161. flex: 1;
  162. // padding: 0 8rpx;
  163. .input-disabled {
  164. :deep(.uni-input-placeholder) {
  165. color: #9B9B9B !important;
  166. }
  167. :deep(.uni-input-input) {
  168. color: #9B9B9B !important;
  169. }
  170. }
  171. }
  172. .input-left,
  173. .input-right {
  174. flex-shrink: 0;
  175. width: 128rpx;
  176. background-color: #f5f7fa;
  177. color: #909399;
  178. padding: 0 10rpx;
  179. text-align: center;
  180. }
  181. .input-left {
  182. border-right: 1rpx solid #dcdfe6;
  183. border-radius: 10rpx 0 0 10rpx;
  184. }
  185. .input-right {
  186. border-left: 1rpx solid #dcdfe6;
  187. border-radius: 0px 10px 10px 0px;
  188. }
  189. .icon-ym-scanCode1 {
  190. margin-right: 8rpx;
  191. color: #909399;
  192. }
  193. .input-count {
  194. color: #909399;
  195. font-size: 24rpx;
  196. }
  197. }
  198. .detail-text {
  199. word-break: break-all;
  200. text-align: right;
  201. .detail-text-addon {
  202. color: #909399;
  203. }
  204. &.disabled {
  205. background-color: #ebedf0;
  206. }
  207. &.ellipsis {
  208. overflow: hidden;
  209. white-space: nowrap;
  210. text-overflow: ellipsis;
  211. }
  212. &.detail-text-left {
  213. text-align: left;
  214. }
  215. }
  216. }
  217. </style>