su-number-box.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="uni-numbox">
  3. <v-btn
  4. icon="mdi-minus"
  5. size="x-small"
  6. :disabled="inputValue <= min || disabled"
  7. @click="_calcValue('minus')"
  8. ></v-btn>
  9. <input
  10. :disabled="disabled"
  11. @focus="_onFocus"
  12. @blur="_onBlur"
  13. class="uni-numbox__value inputItem mx-2"
  14. type="number"
  15. v-model="inputValue"
  16. :style="{ color }"
  17. />
  18. <v-btn
  19. icon="mdi-plus"
  20. size="x-small"
  21. :disabled="inputValue >= max || disabled"
  22. @click="_calcValue('plus')"
  23. ></v-btn>
  24. <div class="ml-3" style="color: #b7b7b7; font-size: 14px;">库存:{{ totalStock }}</div>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * NumberBox 数字输入框
  30. * @description 带加减按钮的数字输入框
  31. * @tutorial https://ext.dcloud.net.cn/plugin?id=31
  32. * @property {Number} value 输入框当前值
  33. * @property {Number} min 最小值
  34. * @property {Number} max 最大值
  35. * @property {Number} step 每次点击改变的间隔大小
  36. * @property {String} background 背景色
  37. * @property {String} color 字体颜色(前景色)
  38. * @property {Boolean} disabled = [true|false] 是否为禁用状态
  39. * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
  40. * @event {Function} focus 输入框聚焦时触发的事件,参数为 event 对象
  41. * @event {Function} blur 输入框失焦时触发的事件,参数为 event 对象
  42. */
  43. export default {
  44. name: 'UniNumberBox',
  45. emits: ['change', 'input', 'update:modelValue', 'blur', 'focus'],
  46. props: {
  47. value: {
  48. type: [Number, String],
  49. default: 1,
  50. },
  51. modelValue: {
  52. type: [Number, String],
  53. default: 1,
  54. },
  55. min: {
  56. type: Number,
  57. default: 0,
  58. },
  59. max: {
  60. type: Number,
  61. default: 100,
  62. },
  63. totalStock: {
  64. type: Number,
  65. default: 0,
  66. },
  67. step: {
  68. type: Number,
  69. default: 1,
  70. },
  71. background: {
  72. type: String,
  73. default: '#f5f5f5',
  74. },
  75. color: {
  76. type: String,
  77. default: '#333',
  78. },
  79. disabled: {
  80. type: Boolean,
  81. default: false,
  82. },
  83. activity: {
  84. type: String,
  85. default: 'none',
  86. },
  87. },
  88. data() {
  89. return {
  90. inputValue: 0,
  91. stock: 0,
  92. };
  93. },
  94. watch: {
  95. value(val) {
  96. this.inputValue = +val;
  97. },
  98. modelValue(val) {
  99. this.inputValue = +val;
  100. },
  101. max(val) {
  102. this.inputValue = this.inputValue-0 > + val ? + val : this.inputValue;
  103. },
  104. },
  105. created() {
  106. if (this.value === 1) {
  107. this.inputValue = +this.modelValue;
  108. }
  109. if (this.modelValue === 1) {
  110. this.inputValue = +this.value;
  111. }
  112. },
  113. methods: {
  114. _calcValue(type) {
  115. if (this.disabled) {
  116. return;
  117. }
  118. const scale = this._getDecimalScale();
  119. let value = this.inputValue * scale;
  120. let step = this.step * scale;
  121. if (type === 'minus') {
  122. value -= step;
  123. if (value < this.min * scale) {
  124. return;
  125. }
  126. if (value > this.max * scale) {
  127. value = this.max * scale;
  128. }
  129. }
  130. if (type === 'plus') {
  131. value += step;
  132. if (value > this.max * scale) {
  133. return;
  134. }
  135. if (value < this.min * scale) {
  136. value = this.min * scale;
  137. }
  138. }
  139. this.inputValue = (value / scale).toFixed(String(scale).length - 1);
  140. this.$emit('change', +this.inputValue);
  141. // TODO vue2 兼容
  142. this.$emit('input', +this.inputValue);
  143. // TODO vue3 兼容
  144. this.$emit('update:modelValue', +this.inputValue);
  145. },
  146. _getDecimalScale() {
  147. let scale = 1;
  148. // 浮点型
  149. if (~~this.step !== this.step) {
  150. scale = Math.pow(10, String(this.step).split('.')[1].length);
  151. }
  152. return scale;
  153. },
  154. _onBlur(event) {
  155. let value = this.inputValue;
  156. this.$emit('blur', value);
  157. value = +value;
  158. if (value > this.max) {
  159. value = this.max;
  160. } else if (value < this.min) {
  161. value = this.min;
  162. }
  163. const scale = this._getDecimalScale();
  164. this.inputValue = value.toFixed(String(scale).length - 1);
  165. this.$emit('change', +this.inputValue);
  166. this.$emit('input', +this.inputValue);
  167. },
  168. _onFocus(event) {
  169. this.$emit('focus', event);
  170. },
  171. },
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. input::-webkit-outer-spin-button, input::-webkit-inner-spin-button{
  176. -webkit-appearance: none !important;
  177. margin: 0;
  178. }
  179. .inputItem {
  180. width: 70px;
  181. border: 1px solid #eee;
  182. padding: 2px 5px;
  183. text-align: center;
  184. }
  185. </style>