u-number-box.vue 11 KB

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