uni-number-box.vue 5.3 KB

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