uni-number-box.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. const scale = this._getDecimalScale();
  91. let value = this.inputValue * scale;
  92. let step = this.step * scale;
  93. if (type === 'minus') {
  94. value -= step;
  95. if (value < this.min * scale) {
  96. return;
  97. }
  98. if (value > this.max * scale) {
  99. value = this.max * scale;
  100. }
  101. } else if (type === 'plus') {
  102. value += step;
  103. if (value > this.max * scale) {
  104. return;
  105. }
  106. if (value < this.min * scale) {
  107. value = this.min * scale;
  108. }
  109. }
  110. this.inputValue = String(value / scale);
  111. },
  112. _getDecimalScale() {
  113. let scale = 1;
  114. // 浮点型
  115. if (~~this.step !== this.step) {
  116. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  117. }
  118. return scale;
  119. },
  120. _onBlur(event) {
  121. let value = event.detail.value;
  122. if (!value) {
  123. // this.inputValue = 0;
  124. return;
  125. }
  126. value = +value;
  127. if (value > this.max) {
  128. value = this.max;
  129. } else if (value < this.min) {
  130. value = this.min;
  131. }
  132. this.inputValue = value;
  133. },
  134. },
  135. };
  136. </script>
  137. <style scoped>
  138. /* #ifdef APP-NVUE */
  139. /* #endif */
  140. .uni-numbox {
  141. /* #ifndef APP-NVUE */
  142. display: flex;
  143. /* #endif */
  144. flex-direction: row;
  145. height: 40px;
  146. /* line-height: 35px; */
  147. width: 160px;
  148. border: 1px solid #e5e5e5;
  149. border-radius: 6px;
  150. background-color: #ffffff;
  151. }
  152. .uni-numbox__value {
  153. flex: 1;
  154. width: 50px;
  155. height: 100%;
  156. text-align: center;
  157. line-height: 1;
  158. font-size: 32px;
  159. background-color: transparent;
  160. border: 0;
  161. }
  162. .uni-numbox__minus {
  163. /* #ifndef APP-NVUE */
  164. display: flex;
  165. /* #endif */
  166. flex-direction: row;
  167. align-items: center;
  168. justify-content: center;
  169. width: 35px;
  170. height: 100%;
  171. padding: 0 5px;
  172. font-size: 20px;
  173. color: #333;
  174. background-color: #f8f8f8;
  175. }
  176. .uni-numbox__plus {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. flex-direction: row;
  181. align-items: center;
  182. justify-content: center;
  183. width: 35px;
  184. height: 100%;
  185. padding: 0 5px;
  186. background-color: #f8f8f8;
  187. }
  188. .uni-numbox--text {
  189. font-size: 40px;
  190. color: #333;
  191. }
  192. .uni-numbox--disabled {
  193. color: #c0c0c0;
  194. }
  195. </style>