u-number-box.vue 13 KB

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