cm-number-box.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="cm-number-box">
  3. <view @click="_calcValue('minus')" class="cm-number-box__minus cm-number-box-btns">
  4. <text
  5. class="cm-number-box--text"
  6. :class="{ 'cm-number-box--disabled': inputValue <= min || disabled }"
  7. :style="{ color }"
  8. >
  9. -
  10. </text>
  11. </view>
  12. <input
  13. :disabled="disabled"
  14. @focus="_onFocus"
  15. @blur="_onBlur"
  16. class="cm-number-box__value"
  17. type="number"
  18. v-model="inputValue"
  19. :style="{ background, color }"
  20. />
  21. <view @click="_calcValue('plus')" class="cm-number-box__plus cm-number-box-btns">
  22. <text
  23. class="cm-number-box--text"
  24. :class="{ 'cm-number-box--disabled': inputValue >= max || disabled }"
  25. :style="{ color }"
  26. >
  27. +
  28. </text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. /**
  34. * NumberBox 数字输入框
  35. * @description 带加减按钮的数字输入框
  36. * @tutorial https://ext.dcloud.net.cn/plugin?id=31
  37. * @property {Number} value 输入框当前值
  38. * @property {Number} min 最小值
  39. * @property {Number} max 最大值
  40. * @property {Number} step 每次点击改变的间隔大小
  41. * @property {String} background 背景色
  42. * @property {String} color 字体颜色(前景色)
  43. * @property {Boolean} disabled = [true|false] 是否为禁用状态
  44. * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
  45. * @event {Function} focus 输入框聚焦时触发的事件,参数为 event 对象
  46. * @event {Function} blur 输入框失焦时触发的事件,参数为 event 对象
  47. */
  48. export default {
  49. name: 'UniNumberBox',
  50. emits: ['change', 'input', 'update:modelValue', 'blur', 'focus'],
  51. props: {
  52. value: {
  53. type: [Number, String],
  54. default: 1
  55. },
  56. modelValue: {
  57. type: [Number, String],
  58. default: 1
  59. },
  60. min: {
  61. type: Number,
  62. default: 1
  63. },
  64. max: {
  65. type: Number,
  66. default: 100
  67. },
  68. step: {
  69. type: Number,
  70. default: 1
  71. },
  72. background: {
  73. type: String,
  74. default: '#f5f5f5'
  75. },
  76. color: {
  77. type: String,
  78. default: '#333'
  79. },
  80. disabled: {
  81. type: Boolean,
  82. default: false
  83. }
  84. },
  85. data() {
  86. return {
  87. inputValue: 0
  88. }
  89. },
  90. watch: {
  91. value(val) {
  92. this.inputValue = +val
  93. },
  94. modelValue(val) {
  95. this.inputValue = +val
  96. }
  97. },
  98. created() {
  99. if (this.value === 1) {
  100. this.inputValue = +this.modelValue
  101. }
  102. if (this.modelValue === 1) {
  103. this.inputValue = +this.value
  104. }
  105. },
  106. methods: {
  107. _calcValue(type) {
  108. if (this.disabled) {
  109. return
  110. }
  111. const scale = this._getDecimalScale()
  112. let value = this.inputValue * scale
  113. let step = this.step * scale
  114. if (type === 'minus') {
  115. value -= step
  116. if (value < this.min * scale) {
  117. return
  118. }
  119. if (value > this.max * scale) {
  120. value = this.max * scale
  121. }
  122. }
  123. if (type === 'plus') {
  124. value += step
  125. if (value > this.max * scale) {
  126. return
  127. }
  128. if (value < this.min * scale) {
  129. value = this.min * scale
  130. }
  131. }
  132. this.inputValue = (value / scale).toFixed(String(scale).length - 1)
  133. this.$emit('change', +this.inputValue)
  134. // TODO vue2 兼容
  135. this.$emit('input', +this.inputValue)
  136. // TODO vue3 兼容
  137. this.$emit('update:modelValue', +this.inputValue)
  138. },
  139. _getDecimalScale() {
  140. let scale = 1
  141. // 浮点型
  142. if (~~this.step !== this.step) {
  143. scale = Math.pow(10, String(this.step).split('.')[1].length)
  144. }
  145. return scale
  146. },
  147. _onBlur(event) {
  148. this.$emit('blur', event)
  149. let value = event.detail.value
  150. if (!value) {
  151. // this.inputValue = 0;
  152. return
  153. }
  154. value = +value
  155. if (value > this.max) {
  156. value = this.max
  157. } else if (value < this.min) {
  158. value = this.min
  159. }
  160. const scale = this._getDecimalScale()
  161. this.inputValue = value.toFixed(String(scale).length - 1)
  162. this.$emit('change', +this.inputValue)
  163. this.$emit('input', +this.inputValue)
  164. },
  165. _onFocus(event) {
  166. this.$emit('focus', event)
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. $box-height: 48rpx;
  173. $bg: #fff;
  174. $br: 4rpx;
  175. $color: #333;
  176. .cm-number-box {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. flex-direction: row;
  181. }
  182. .cm-number-box-btns {
  183. /* #ifndef APP-NVUE */
  184. display: flex;
  185. /* #endif */
  186. flex-direction: row;
  187. align-items: center;
  188. justify-content: center;
  189. padding: 0 16rpx;
  190. background-color: $bg;
  191. /* #ifdef H5 */
  192. cursor: pointer;
  193. /* #endif */
  194. border: 1rpx solid #e1e1e1;
  195. }
  196. .cm-number-box__value {
  197. background-color: $bg;
  198. width: 48rpx;
  199. height: $box-height;
  200. text-align: center;
  201. font-size: 26rpx;
  202. border: 2rpx solid #e1e1e1;
  203. border-left-width: 0;
  204. border-right-width: 0;
  205. color: $color;
  206. background-color: #e1e1e1;
  207. }
  208. .cm-number-box__minus {
  209. border-top-left-radius: $box-height / 2;
  210. border-bottom-left-radius: $box-height / 2;
  211. }
  212. .cm-number-box__plus {
  213. border-top-right-radius: $box-height / 2;
  214. border-bottom-right-radius: $box-height / 2;
  215. }
  216. .cm-number-box--text {
  217. // fix nvue
  218. line-height: 44rpx;
  219. font-size: 32rpx;
  220. font-weight: 300;
  221. color: $color;
  222. }
  223. .cm-number-box .cm-number-box--disabled {
  224. color: #c0c0c0 !important;
  225. /* #ifdef H5 */
  226. cursor: not-allowed;
  227. /* #endif */
  228. }
  229. </style>