u-number-box.vue 11 KB

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