uni-number-box.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class="uni-numbox">
  3. <div @click="_calcValue('minus')" class="uni-numbox__minus">
  4. <span
  5. class="uni-numbox--text"
  6. :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }"
  7. >-</span
  8. >
  9. </div>
  10. <input
  11. :disabled="inputDisabled"
  12. @blur="_onBlur"
  13. :adjust-position="false"
  14. class="uni-numbox__value"
  15. type="number"
  16. v-model="inputValue"
  17. />
  18. <div @click="_calcValue('plus')" class="uni-numbox__plus">
  19. <span
  20. class="uni-numbox--text"
  21. :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }"
  22. >+</span
  23. >
  24. </div>
  25. </div>
  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 {Boolean} disabled = [true|false] 是否为禁用状态
  37. * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
  38. */
  39. export default {
  40. name: 'UniNumberBox',
  41. props: {
  42. value: {
  43. type: [Number, String],
  44. default: 1,
  45. },
  46. min: {
  47. type: Number,
  48. default: 0,
  49. },
  50. max: {
  51. type: Number,
  52. default: 100,
  53. },
  54. step: {
  55. type: Number,
  56. default: 1,
  57. },
  58. disabled: {
  59. type: Boolean,
  60. default: false,
  61. },
  62. inputDisabled: {
  63. type: Boolean,
  64. default: true,
  65. },
  66. },
  67. data() {
  68. return {
  69. inputValue: 0,
  70. };
  71. },
  72. watch: {
  73. value(val) {
  74. this.inputValue = val;
  75. },
  76. inputValue(newVal, oldVal) {
  77. if (newVal !== oldVal) {
  78. this.$emit('input', newVal);
  79. }
  80. },
  81. },
  82. created() {
  83. this.inputValue = this.value;
  84. },
  85. methods: {
  86. _calcValue(type) {
  87. if (this.disabled) {
  88. return;
  89. }
  90. console.log(type, 91)
  91. const scale = this._getDecimalScale();
  92. let value = this.inputValue * scale;
  93. let step = this.step * scale;
  94. if (type === 'minus') {
  95. value -= step;
  96. console.log(97, value , this.min * scale)
  97. if (value < this.min * scale) {
  98. return;
  99. }
  100. console.log(101, value, this.max, scale)
  101. if (value > this.max * scale) {
  102. value = this.max * scale;
  103. }
  104. } else if (type === 'plus') {
  105. value += step;
  106. if (value > this.max * scale) {
  107. return;
  108. }
  109. if (value < this.min * scale) {
  110. value = this.min * scale;
  111. }
  112. }
  113. console.log(113, value, scale)
  114. this.inputValue = String(value / scale);
  115. },
  116. _getDecimalScale() {
  117. let scale = 1;
  118. // 浮点型
  119. if (~~this.step !== this.step) {
  120. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  121. }
  122. return scale;
  123. },
  124. _onBlur(event) {
  125. let value = event.detail.value;
  126. if (!value) {
  127. // this.inputValue = 0;
  128. return;
  129. }
  130. value = +value;
  131. if (value > this.max) {
  132. value = this.max;
  133. } else if (value < this.min) {
  134. value = this.min;
  135. }
  136. this.inputValue = value;
  137. },
  138. },
  139. };
  140. </script>
  141. <style scoped>
  142. /* #ifdef APP-NVUE */
  143. /* #endif */
  144. .uni-numbox {
  145. /* #ifndef APP-NVUE */
  146. display: flex;
  147. /* #endif */
  148. flex-direction: row;
  149. height: 40px;
  150. /* line-height: 35px; */
  151. width: 160px;
  152. border: 1px solid #e5e5e5;
  153. border-radius: 6px;
  154. background-color: #ffffff;
  155. }
  156. .uni-numbox__value {
  157. flex: 1;
  158. width: 50px;
  159. height: 100%;
  160. text-align: center;
  161. padding: 0;
  162. margin: 0;
  163. line-height: 40px;
  164. font-size: 32px;
  165. background-color: transparent;
  166. border: 0;
  167. }
  168. .uni-numbox__minus {
  169. /* #ifndef APP-NVUE */
  170. display: flex;
  171. /* #endif */
  172. flex-direction: row;
  173. align-items: center;
  174. justify-content: center;
  175. width: 35px;
  176. height: 100%;
  177. padding: 0 5px;
  178. font-size: 20px;
  179. color: #333;
  180. background-color: #f8f8f8;
  181. }
  182. .uni-numbox__plus {
  183. /* #ifndef APP-NVUE */
  184. display: flex;
  185. /* #endif */
  186. flex-direction: row;
  187. align-items: center;
  188. justify-content: center;
  189. width: 35px;
  190. height: 100%;
  191. padding: 0 5px;
  192. background-color: #f8f8f8;
  193. }
  194. .uni-numbox--text {
  195. font-size: 40px;
  196. color: #333;
  197. }
  198. .uni-numbox--disabled {
  199. color: #c0c0c0;
  200. }
  201. </style>