u-number-box.vue 12 KB

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