wk-field-input-number.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="wk-field wk-field-input">
  3. <view v-if="_label" class="wk-field__label">
  4. <view v-if="field.isNull === 1" class="line" />
  5. {{ _label }}
  6. </view>
  7. <view class="wk-field__body" @click="handleClick">
  8. <!-- #ifdef MP-WEIXIN -->
  9. <input
  10. ref="core"
  11. v-model="formValue"
  12. :disabled="field.disabled"
  13. :placeholder="_placeholder"
  14. placeholder-class="wk-placeholder placeholder"
  15. type="digit"
  16. class="wk-field__body-core"
  17. @input="changeText">
  18. <!-- #endif -->
  19. <!-- #ifndef MP-WEIXIN -->
  20. <input
  21. ref="core"
  22. v-model="formValue"
  23. :disabled="field.disabled"
  24. :placeholder="_placeholder"
  25. placeholder-class="wk-placeholder placeholder"
  26. type="number"
  27. class="wk-field__body-core"
  28. @input="changeText">
  29. <!-- #endif -->
  30. <view
  31. v-if="field.formType === 'percent'"
  32. class="percent">
  33. %
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import mixins from './mixins'
  40. export default {
  41. name: 'WkFieldInputNumber',
  42. mixins: [mixins],
  43. data() {
  44. return {
  45. timer: null
  46. }
  47. },
  48. methods: {
  49. handleClick() {
  50. if (this.field.disabled) {
  51. if (this.config && this.config.disabledMsg) {
  52. this.$toast(this.config.disabledMsg)
  53. }
  54. return
  55. }
  56. },
  57. formatValue(oldStr) {
  58. let valueStr = this.toNumber(oldStr)
  59. const arr = String(valueStr).split('.')
  60. const len = String(valueStr)
  61. .replace('.', '')
  62. .replace('-', '')
  63. .length
  64. const maxlength = this.field.formType === 'percent' ? 10 : 15
  65. const min = this.$isEmpty(this.field.minNumRestrict) ? -Infinity : Number(this.field.minNumRestrict || -Infinity)
  66. const max = this.$isEmpty(this.field.maxNumRestrict) ? Infinity : Number(this.field.maxNumRestrict || Infinity)
  67. // precisions值 null 不支持小数 0 不限制小数位
  68. let num = ''
  69. if (len > maxlength) {
  70. // 最多支持***位数字(包含小数位)
  71. num = valueStr.slice(0, len)
  72. valueStr = this.formatValue(num)
  73. } else if (this.$isEmpty(this.field.precisions) && String(valueStr).includes('.')) {
  74. // 不支持小数
  75. num = valueStr.slice(0, String(valueStr).indexOf('.'))
  76. valueStr = this.formatValue(num)
  77. } else if (arr.length > 1 && arr[1].length > Number(this.field.precisions)) {
  78. // 小数位不能大于precisions
  79. arr[1] = arr[1].slice(0, Number(this.field.precisions))
  80. num = arr.join('.')
  81. valueStr = this.formatValue(num)
  82. } else if (Number(valueStr) < min) {
  83. // 不能小于min
  84. valueStr = String(Number(min))
  85. } else if (Number(valueStr) > max) {
  86. // 不能大于max
  87. valueStr = String(Number(max))
  88. }
  89. return valueStr
  90. },
  91. // 转化为数字字符串
  92. toNumber(val) {
  93. val = String(val)
  94. if (isNaN(Number(val))) {
  95. val = val.slice(0, -1)
  96. return this.toNumber(val)
  97. } else {
  98. return val
  99. }
  100. },
  101. changeText() {
  102. let oldStr = String(this.formValue)
  103. let valueStr = this.formatValue(this.formValue)
  104. this.formValue = oldStr
  105. this.$nextTick(() => {
  106. this.formValue = valueStr
  107. if (this.value !== this.formValue) {
  108. this.emitChangeEvt(this.formValue)
  109. }
  110. })
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. @import './wkField.scss';
  117. .placeholder {
  118. font-size: $wk-font-sm;
  119. }
  120. .percent {
  121. font-size: $wk-font-sm;
  122. }
  123. </style>