index.vue 16 KB

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