index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div class="custom-money-tag">
  3. <van-field
  4. type="number"
  5. v-model="inputMap.start"
  6. :maxlength="maxLength.start"
  7. :placeholder="placeholder.start"
  8. :class="{ focus: focus.start && hoverBorderHighlight }"
  9. @click.stop="onClick('start')"
  10. @input="onInput('start')"
  11. @focus="onFocus('start')"
  12. @blur="onBlur('start')"
  13. >
  14. <template #extra>
  15. <span class="extra-text-tip">{{ extra }}</span>
  16. </template>
  17. </van-field>
  18. <div class="line"></div>
  19. <van-field
  20. type="number"
  21. :maxlength="maxLength.end"
  22. v-model="inputMap.end"
  23. :placeholder="placeholder.end"
  24. :class="{ focus: focus.end && hoverBorderHighlight }"
  25. @click.stop="onClick('end')"
  26. @input="onInput('end')"
  27. @focus="onFocus('end')"
  28. @blur="onBlur('end')"
  29. >
  30. <template #extra>
  31. <span class="extra-text-tip">{{ extra }}</span>
  32. </template>
  33. </van-field>
  34. </div>
  35. </template>
  36. <script>
  37. import { Field } from 'vant'
  38. export default {
  39. name: 'customMoney',
  40. components: {
  41. [Field.name]: Field
  42. },
  43. props: {
  44. hoverBorderHighlight: {
  45. type: Boolean,
  46. default: true
  47. },
  48. extra: {
  49. type: String,
  50. default: '万'
  51. },
  52. placeholder: {
  53. type: Object,
  54. default: () => ({
  55. start: '请输入金额',
  56. end: '请输入金额'
  57. })
  58. },
  59. maxLength: {
  60. type: Object,
  61. default: () => ({
  62. start: 10,
  63. end: 10
  64. })
  65. }
  66. },
  67. data() {
  68. return {
  69. focus: {
  70. start: false,
  71. end: false
  72. },
  73. inputMap: {
  74. start: '',
  75. end: ''
  76. }
  77. }
  78. },
  79. methods: {
  80. onClick(type) {
  81. this.$emit('onClick', { type })
  82. },
  83. onInput(type) {
  84. const data = this.getState()
  85. const payload = {
  86. ...data,
  87. type
  88. }
  89. this.$emit('onInput', payload)
  90. },
  91. onFocus(type) {
  92. this.focus[type] = true
  93. },
  94. onBlur(type) {
  95. this.focus[type] = false
  96. this.formatInput()
  97. this.onBlurEmitter()
  98. },
  99. onBlurEmitter() {
  100. const { start, end } = this.inputMap
  101. if (String(start).length && String(end).length) {
  102. this.$emit('onBlur', this.getState())
  103. }
  104. },
  105. formatInput() {
  106. const { start, end } = this.inputMap
  107. if (String(start).length && String(end).length) {
  108. const isEndMax = Number(start) < Number(end)
  109. if (!isEndMax) {
  110. this.inputMap.start = end
  111. this.inputMap.end = start
  112. }
  113. }
  114. },
  115. getState() {
  116. return this.inputMap
  117. },
  118. setState(data) {
  119. if (!data || Object.keys(data).length === 0) return
  120. const { start, end } = data
  121. this.inputMap.start = start
  122. this.inputMap.end = end
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. .custom-money-tag {
  129. display: flex;
  130. align-items: center;
  131. justify-content: space-between;
  132. .line {
  133. width: 12px;
  134. height: 1px;
  135. margin: 0 4px;
  136. background: #acacad;
  137. }
  138. .van-cell.van-field {
  139. flex: 1;
  140. height: 36px;
  141. padding: 10px 12px;
  142. background: #fff;
  143. border: 1px solid rgba(0, 0, 0, 0.1);
  144. border-radius: 5px;
  145. align-items: center;
  146. &.focus {
  147. border-color: $main;
  148. }
  149. }
  150. .extra-text-tip {
  151. color: #9b9ca3;
  152. }
  153. }
  154. </style>