index.vue 13 KB

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