u-number-box.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view class="u-numberbox">
  3. <view class="u-icon-minus" @tap.stop="minus" :class="{ 'u-icon-disabled': disabled || inputVal <= min }" :style="{
  4. background: bgColor,
  5. height: inputHeight + 'rpx',
  6. color: color
  7. }">
  8. <u-icon name="minus" :size="size"></u-icon>
  9. </view>
  10. <input :disabled="disabledInput || disabled" :cursor-spacing="getCursorSpacing" :class="{ 'u-input-disabled': disabled }" v-model="inputVal" class="u-number-input" @blur="onBlur"
  11. type="number" :style="{
  12. color: color,
  13. fontSize: size + 'rpx',
  14. background: bgColor,
  15. height: inputHeight + 'rpx',
  16. width: inputWidth + 'rpx'
  17. }" />
  18. <view class="u-icon-plus" @tap.stop="plus" :class="{ 'u-icon-disabled': disabled || inputVal >= max }" :style="{
  19. background: bgColor,
  20. height: inputHeight + 'rpx',
  21. color: color
  22. }">
  23. <u-icon name="plus" :size="size"></u-icon>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * numberBox 步进器
  30. * @description 该组件一般用于商城购物选择物品数量的场景。注意:该输入框只能输入大于或等于0的整数,不支持小数输入
  31. * @tutorial https://www.uviewui.com/components/numberBox.html
  32. * @property {Number} value 输入框初始值(默认1)
  33. * @property {String} bg-color 输入框和按钮的背景颜色(默认#F2F3F5)
  34. * @property {Number} min 用户可输入的最小值(默认0)
  35. * @property {Number} max 用户可输入的最大值(默认99999)
  36. * @property {Number} step 步长,每次加或减的值(默认1)
  37. * @property {Boolean} disabled 是否禁用操作,禁用后无法加减或手动修改输入框的值(默认false)
  38. * @property {Boolean} disabled-input 是否禁止输入框手动输入值(默认false)
  39. * @property {String | Number} size 输入框文字和按钮字体大小,单位rpx(默认26)
  40. * @property {String} color 输入框文字和加减按钮图标的颜色(默认#323233)
  41. * @property {String | Number} input-width 输入框宽度,单位rpx(默认80)
  42. * @property {String | Number} input-height 输入框和按钮的高度,单位rpx(默认50)
  43. * @property {String | Number} index 事件回调时用以区分当前发生变化的是哪个输入框
  44. * @property {String | Number} cursor-spacing 指定光标于键盘的距离,避免键盘遮挡输入框,单位rpx(默认200)
  45. * @event {Function} change 输入框内容发生变化时触发,对象形式
  46. * @event {Function} blur 输入框失去焦点时触发,对象形式
  47. * @event {Function} minus 点击减少按钮时触发(按钮可点击情况下),对象形式
  48. * @event {Function} plus 点击增加按钮时触发(按钮可点击情况下),对象形式
  49. * @example <u-number-box :min="1" :max="100"></u-number-box>
  50. */
  51. export default {
  52. name: "u-number-box",
  53. props: {
  54. // 预显示的数字
  55. value: {
  56. type: Number,
  57. default: 1
  58. },
  59. // 背景颜色
  60. bgColor: {
  61. type: String,
  62. default: '#F2F3F5'
  63. },
  64. // 最小值
  65. min: {
  66. type: Number,
  67. default: 0
  68. },
  69. // 最大值
  70. max: {
  71. type: Number,
  72. default: 99999
  73. },
  74. // 步进值,每次加或减的值
  75. step: {
  76. type: Number,
  77. default: 1
  78. },
  79. // 是否禁用加减操作
  80. disabled: {
  81. type: Boolean,
  82. default: false
  83. },
  84. // input的字体大小,单位rpx
  85. size: {
  86. type: [Number, String],
  87. default: 26
  88. },
  89. // 加减图标的颜色
  90. color: {
  91. type: String,
  92. default: '#323233'
  93. },
  94. // input宽度,单位rpx
  95. inputWidth: {
  96. type: [Number, String],
  97. default: 80
  98. },
  99. // input高度,单位rpx
  100. inputHeight: {
  101. type: [Number, String],
  102. default: 50
  103. },
  104. // index索引,用于列表中使用,让用户知道是哪个numberbox发生了变化,一般使用for循环出来的index值即可
  105. index: {
  106. type: [Number, String],
  107. default: ''
  108. },
  109. // 是否禁用输入框,与disabled作用于输入框时,为OR的关系,即想要禁用输入框,又可以加减的话
  110. // 设置disabled为false,disabledInput为true即可
  111. disabledInput: {
  112. type: Boolean,
  113. default: false
  114. },
  115. // 输入框于键盘之间的距离
  116. cursorSpacing: {
  117. type: [Number, String],
  118. default: 100
  119. }
  120. },
  121. watch: {
  122. value(val, val1) {
  123. // 防止用户在change事件回调中将回调值赋值给valut变量,导致change事件触发两次
  124. if(Number(val) != this.inputVal) this.inputVal = Number(val);
  125. },
  126. inputVal(v1, v2) {
  127. // 为了让用户能够删除所有输入值,重新输入内容,删除所有值后,内容为空字符串
  128. if (v1 == '') return;
  129. let value = 0;
  130. // 首先判断是否正整数,并且在min和max之间,如果不是,使用原来值
  131. let tmp = /(^\d+$)/.test(v1);
  132. if (tmp && v1 >= this.min && v1 <= this.max) value = v1;
  133. else value = v2;
  134. this.handleChange(value, 'change');
  135. this.$nextTick(() => {
  136. this.inputVal = value;
  137. })
  138. }
  139. },
  140. data() {
  141. return {
  142. inputVal: 0 // 输入框中的值,不能直接使用props中的value,因为应该改变props的状态
  143. };
  144. },
  145. created() {
  146. this.inputVal = Number(this.value);
  147. },
  148. computed: {
  149. getCursorSpacing() {
  150. // 先将值转为px单位,再转为数值
  151. return Number(uni.upx2px(this.cursorSpacing));
  152. }
  153. },
  154. methods: {
  155. minus() {
  156. this.computeVal('minus');
  157. },
  158. plus() {
  159. this.computeVal('plus');
  160. },
  161. // 为了保证小数相加减出现精度溢出的问题
  162. calcPlus(num1, num2) {
  163. let baseNum, baseNum1, baseNum2;
  164. try {
  165. baseNum1 = num1.toString().split('.')[1].length;
  166. } catch (e) {
  167. baseNum1 = 0;
  168. }
  169. try {
  170. baseNum2 = num2.toString().split('.')[1].length;
  171. } catch (e) {
  172. baseNum2 = 0;
  173. }
  174. baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  175. let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2; //精度
  176. return ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision);
  177. },
  178. // 为了保证小数相加减出现精度溢出的问题
  179. calcMinus(num1, num2) {
  180. let baseNum, baseNum1, baseNum2;
  181. try {
  182. baseNum1 = num1.toString().split('.')[1].length;
  183. } catch (e) {
  184. baseNum1 = 0;
  185. }
  186. try {
  187. baseNum2 = num2.toString().split('.')[1].length;
  188. } catch (e) {
  189. baseNum2 = 0;
  190. }
  191. baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  192. let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2;
  193. return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
  194. },
  195. computeVal(type) {
  196. uni.hideKeyboard();
  197. if (this.disabled) return;
  198. let value = 0;
  199. // 减
  200. if (type === 'minus') {
  201. value = this.calcMinus(this.inputVal, this.step);
  202. } else if (type === 'plus') {
  203. value = this.calcPlus(this.inputVal, this.step);
  204. }
  205. // 判断是否小于最小值和大于最大值
  206. if (value < this.min || value > this.max) {
  207. return;
  208. }
  209. this.inputVal = value;
  210. this.handleChange(value, type);
  211. },
  212. // 处理用户手动输入的情况
  213. onBlur(event) {
  214. let val = 0;
  215. let value = event.detail.value;
  216. // 如果为非0-9数字组成,或者其第一位数值为0,直接让其等于min值
  217. // 这里不直接判断是否正整数,是因为用户传递的props min值可能为0
  218. if (!/(^\d+$)/.test(value) || value[0] == 0) val = this.min;
  219. val = +value;
  220. if (val > this.max) {
  221. val = this.max;
  222. } else if (val < this.min) {
  223. val = this.min;
  224. }
  225. this.$nextTick(() => {
  226. this.inputVal = val;
  227. })
  228. this.handleChange(val, "blur");
  229. },
  230. handleChange(value, type) {
  231. if (this.disabled) return;
  232. // 发出input事件,修改通过v-model绑定的值,达到双向绑定的效果
  233. this.$emit('input', Number(value));
  234. this.$emit(type, {
  235. // 转为Number类型
  236. value: Number(value),
  237. index: this.index
  238. })
  239. }
  240. }
  241. };
  242. </script>
  243. <style lang="scss" scoped>
  244. .u-numberbox {
  245. display: inline-flex;
  246. align-items: center;
  247. }
  248. .u-number-input {
  249. position: relative;
  250. text-align: center;
  251. padding: 0;
  252. margin: 0 6rpx;
  253. display: flex;
  254. align-items: center;
  255. justify-content: center;
  256. }
  257. .u-icon-plus,
  258. .u-icon-minus {
  259. width: 60rpx;
  260. display: flex;
  261. justify-content: center;
  262. align-items: center;
  263. }
  264. .u-icon-plus {
  265. border-radius: 0 8rpx 8rpx 0;
  266. }
  267. .u-icon-minus {
  268. border-radius: 8rpx 0 0 8rpx;
  269. }
  270. .u-icon-disabled {
  271. color: #c8c9cc !important;
  272. background: #f7f8fa !important;
  273. }
  274. .u-input-disabled {
  275. color: #c8c9cc !important;
  276. background-color: #f2f3f5 !important;
  277. }
  278. </style>