综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-number-box.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <view class="uv-number-box">
  3. <view
  4. class="uv-number-box__slot"
  5. @tap.stop="clickHandler('minus')"
  6. @touchstart="onTouchStart('minus')"
  7. @touchend.stop="clearTimeout"
  8. v-if="showMinus && $slots.minus"
  9. >
  10. <slot name="minus" />
  11. </view>
  12. <view
  13. v-else-if="showMinus"
  14. class="uv-number-box__minus"
  15. @tap.stop="clickHandler('minus')"
  16. @touchstart="onTouchStart('minus')"
  17. @touchend.stop="clearTimeout"
  18. hover-class="uv-number-box__minus--hover"
  19. hover-stay-time="150"
  20. :class="{ 'uv-number-box__minus--disabled': isDisabled('minus') }"
  21. :style="[buttonStyle('minus')]"
  22. >
  23. <uv-icon
  24. name="minus"
  25. :color="isDisabled('minus') ? '#c8c9cc' : '#323233'"
  26. size="15"
  27. bold
  28. :customStyle="iconStyle"
  29. ></uv-icon>
  30. </view>
  31. <slot name="input">
  32. <input
  33. :disabled="disabledInput || disabled"
  34. :cursor-spacing="getCursorSpacing"
  35. :class="{ 'uv-number-box__input--disabled': disabled || disabledInput }"
  36. v-model="currentValue"
  37. class="uv-number-box__input"
  38. @blur="onBlur"
  39. @focus="onFocus"
  40. @input="onInput"
  41. type="number"
  42. :style="[inputStyle]"
  43. />
  44. </slot>
  45. <view
  46. class="uv-number-box__slot"
  47. @tap.stop="clickHandler('plus')"
  48. @touchstart="onTouchStart('plus')"
  49. @touchend.stop="clearTimeout"
  50. v-if="showPlus && $slots.plus"
  51. >
  52. <slot name="plus" />
  53. </view>
  54. <view
  55. v-else-if="showPlus"
  56. class="uv-number-box__plus"
  57. @tap.stop="clickHandler('plus')"
  58. @touchstart="onTouchStart('plus')"
  59. @touchend.stop="clearTimeout"
  60. hover-class="uv-number-box__plus--hover"
  61. hover-stay-time="150"
  62. :class="{ 'uv-number-box__minus--disabled': isDisabled('plus') }"
  63. :style="[buttonStyle('plus')]"
  64. >
  65. <uv-icon
  66. name="plus"
  67. :color="isDisabled('plus') ? '#c8c9cc' : '#323233'"
  68. size="15"
  69. bold
  70. :customStyle="iconStyle"
  71. ></uv-icon>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  77. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  78. import props from './props.js';
  79. /**
  80. * numberBox 步进器
  81. * @description 该组件一般用于商城购物选择物品数量的场景。
  82. * @tutorial https://www.uvui.cn/components/numberBox.html
  83. * @property {String | Number} value / v-model 用于双向绑定的值,初始化时设置设为默认min值(最小值) (默认 0 )
  84. * @property {String | Number} name 步进器标识符,在change回调返回
  85. * @property {String | Number} min 最小值 (默认 1 )
  86. * @property {String | Number} max 最大值 (默认 Number.MAX_SAFE_INTEGER )
  87. * @property {String | Number} step 加减的步长,可为小数 (默认 1 )
  88. * @property {Boolean} integer 是否只允许输入整数 (默认 false )
  89. * @property {Boolean} disabled 是否禁用,包括输入框,加减按钮 (默认 false )
  90. * @property {Boolean} disabledInput 是否禁用输入框 (默认 false )
  91. * @property {Boolean} asyncChange 是否开启异步变更,开启后需要手动控制输入值 (默认 false )
  92. * @property {String | Number} inputWidth 输入框宽度,单位为px (默认 35 )
  93. * @property {Boolean} showMinus 是否显示减少按钮 (默认 true )
  94. * @property {Boolean} showPlus 是否显示增加按钮 (默认 true )
  95. * @property {String | Number} decimalLength 显示的小数位数
  96. * @property {Boolean} longPress 是否开启长按加减手势 (默认 true )
  97. * @property {String} color 输入框文字和加减按钮图标的颜色 (默认 '#323233' )
  98. * @property {String | Number} buttonSize 按钮大小,宽高等于此值,单位px,输入框高度和此值保持一致 (默认 30 )
  99. * @property {String} bgColor 输入框和按钮的背景颜色 (默认 '#EBECEE' )
  100. * @property {String | Number} cursorSpacing 指定光标于键盘的距离,避免键盘遮挡输入框,单位px (默认 100 )
  101. * @property {Boolean} disablePlus 是否禁用增加按钮 (默认 false )
  102. * @property {Boolean} disableMinus 是否禁用减少按钮 (默认 false )
  103. * @property {Object | String} iconStyle 加减按钮图标的样式
  104. * @event {Function} onFocus 输入框活动焦点
  105. * @event {Function} onBlur 输入框失去焦点
  106. * @event {Function} onInput 输入框值发生变化
  107. * @event {Function} onChange
  108. * @example <uv-number-box v-model="value" @change="valChange"></uv-number-box>
  109. */
  110. export default {
  111. name: 'uv-number-box',
  112. mixins: [mpMixin, mixin, props],
  113. data() {
  114. return {
  115. // 输入框实际操作的值
  116. currentValue: '',
  117. // 定时器
  118. longPressTimer: null
  119. }
  120. },
  121. watch: {
  122. // 多个值之间,只要一个值发生变化,都要重新检查check()函数
  123. watchChange(n) {
  124. this.check()
  125. },
  126. value(newVal) {
  127. if (newVal !== this.currentValue) {
  128. this.currentValue = this.format(this.value)
  129. }
  130. },
  131. modelValue(newVal) {
  132. if (newVal !== this.currentValue) {
  133. this.currentValue = this.format(this.modelValue)
  134. }
  135. }
  136. },
  137. computed: {
  138. getCursorSpacing() {
  139. // 判断传入的单位,如果为px单位,需要转成px
  140. return this.$uv.getPx(this.cursorSpacing)
  141. },
  142. // 按钮的样式
  143. buttonStyle() {
  144. return (type) => {
  145. const style = {
  146. backgroundColor: this.bgColor,
  147. height: this.$uv.addUnit(this.buttonSize),
  148. color: this.color
  149. }
  150. if (this.isDisabled(type)) {
  151. style.backgroundColor = '#f7f8fa'
  152. }
  153. return style
  154. }
  155. },
  156. // 输入框的样式
  157. inputStyle() {
  158. const disabled = this.disabled || this.disabledInput
  159. const style = {
  160. color: this.color,
  161. backgroundColor: this.bgColor,
  162. height: this.$uv.addUnit(this.buttonSize),
  163. width: this.$uv.addUnit(this.inputWidth)
  164. }
  165. return style
  166. },
  167. // 用于监听多个值发生变化
  168. watchChange() {
  169. return [this.integer, this.decimalLength, this.min, this.max]
  170. },
  171. isDisabled() {
  172. return (type) => {
  173. if (type === 'plus') {
  174. // 在点击增加按钮情况下,判断整体的disabled,是否单独禁用增加按钮,以及当前值是否大于最大的允许值
  175. return (
  176. this.disabled ||
  177. this.disablePlus ||
  178. this.currentValue >= this.max
  179. )
  180. }
  181. // 点击减少按钮同理
  182. return (
  183. this.disabled ||
  184. this.disableMinus ||
  185. this.currentValue <= this.min
  186. )
  187. }
  188. },
  189. },
  190. created() {
  191. this.init()
  192. },
  193. methods: {
  194. init() {
  195. const value = this.value || this.modelValue;
  196. this.currentValue = this.format(value)
  197. },
  198. // 格式化整理数据,限制范围
  199. format(value) {
  200. value = this.filter(value)
  201. // 如果为空字符串,那么设置为0,同时将值转为Number类型
  202. value = value === '' ? 0 : +value
  203. // 对比最大最小值,取在min和max之间的值
  204. value = Math.max(Math.min(this.max, value), this.min)
  205. // 如果设定了最大的小数位数,使用toFixed去进行格式化
  206. if (this.decimalLength !== null) {
  207. value = value.toFixed(this.decimalLength)
  208. }
  209. return value
  210. },
  211. // 过滤非法的字符
  212. filter(value) {
  213. // 只允许0-9之间的数字,"."为小数点,"-"为负数时候使用
  214. value = String(value).replace(/[^0-9.-]/g, '')
  215. // 如果只允许输入整数,则过滤掉小数点后的部分
  216. if (this.integer && value.indexOf('.') !== -1) {
  217. value = value.split('.')[0]
  218. }
  219. return value;
  220. },
  221. check() {
  222. // 格式化了之后,如果前后的值不相等,那么设置为格式化后的值
  223. const val = this.format(this.currentValue);
  224. if (val !== this.currentValue) {
  225. this.currentValue = val
  226. }
  227. },
  228. // 输入框活动焦点
  229. onFocus(event) {
  230. this.$emit('focus', {
  231. ...event.detail,
  232. name: this.name,
  233. })
  234. },
  235. // 输入框失去焦点
  236. onBlur(event) {
  237. // 对输入值进行格式化
  238. const value = this.format(event.detail.value)
  239. // 发出blur事件
  240. this.$emit(
  241. 'blur', {
  242. ...event.detail,
  243. name: this.name,
  244. }
  245. )
  246. },
  247. // 输入框值发生变化
  248. onInput(e) {
  249. const {
  250. value = ''
  251. } = e.detail || {}
  252. // 为空返回
  253. if (value === '') return
  254. let formatted = this.filter(value)
  255. // 最大允许的小数长度
  256. if (this.decimalLength !== null && formatted.indexOf('.') !== -1) {
  257. const pair = formatted.split('.');
  258. formatted = `${pair[0]}.${pair[1].slice(0, this.decimalLength)}`
  259. }
  260. formatted = this.format(formatted)
  261. this.emitChange(formatted);
  262. },
  263. // 发出change事件
  264. emitChange(value) {
  265. // 如果开启了异步变更值,则不修改内部的值,需要用户手动在外部通过v-model变更
  266. if (!this.asyncChange) {
  267. this.$nextTick(() => {
  268. this.$emit('input', value)
  269. this.$emit('update:modelValue', value)
  270. this.currentValue = value
  271. this.$forceUpdate()
  272. })
  273. }
  274. this.$emit('change', {
  275. value,
  276. name: this.name,
  277. });
  278. },
  279. onChange() {
  280. const {
  281. type
  282. } = this
  283. if (this.isDisabled(type)) {
  284. return this.$emit('overlimit', type)
  285. }
  286. const diff = type === 'minus' ? -this.step : +this.step
  287. const value = this.format(this.add(+this.currentValue, diff))
  288. this.emitChange(value)
  289. this.$emit(type)
  290. },
  291. // 对值扩大后进行四舍五入,再除以扩大因子,避免出现浮点数操作的精度问题
  292. add(num1, num2) {
  293. const cardinal = Math.pow(10, 10);
  294. return Math.round((num1 + num2) * cardinal) / cardinal
  295. },
  296. // 点击加减按钮
  297. clickHandler(type) {
  298. this.type = type
  299. this.onChange()
  300. },
  301. longPressStep() {
  302. // 每隔一段时间,重新调用longPressStep方法,实现长按加减
  303. this.clearTimeout()
  304. this.longPressTimer = setTimeout(() => {
  305. this.onChange()
  306. this.longPressStep()
  307. }, 250);
  308. },
  309. onTouchStart(type) {
  310. if (!this.longPress) return
  311. this.clearTimeout()
  312. this.type = type
  313. // 一定时间后,默认达到长按状态
  314. this.longPressTimer = setTimeout(() => {
  315. this.onChange()
  316. this.longPressStep()
  317. }, 600)
  318. },
  319. // 触摸结束,清除定时器,停止长按加减
  320. onTouchEnd() {
  321. if (!this.longPress) return
  322. this.clearTimeout()
  323. },
  324. // 清除定时器
  325. clearTimeout() {
  326. clearTimeout(this.longPressTimer)
  327. this.longPressTimer = null
  328. }
  329. }
  330. }
  331. </script>
  332. <style lang="scss" scoped>
  333. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  334. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  335. $uv-numberBox-hover-bgColor: #E6E6E6 !default;
  336. $uv-numberBox-disabled-color: #c8c9cc !default;
  337. $uv-numberBox-disabled-bgColor: #f7f8fa !default;
  338. $uv-numberBox-plus-radius: 4px !default;
  339. $uv-numberBox-minus-radius: 4px !default;
  340. $uv-numberBox-input-text-align: center !default;
  341. $uv-numberBox-input-font-size: 15px !default;
  342. $uv-numberBox-input-padding: 0 !default;
  343. $uv-numberBox-input-margin: 0 2px !default;
  344. $uv-numberBox-input-disabled-color: #c8c9cc !default;
  345. $uv-numberBox-input-disabled-bgColor: #f2f3f5 !default;
  346. .uv-number-box {
  347. @include flex(row);
  348. align-items: center;
  349. &__slot {
  350. /* #ifndef APP-NVUE */
  351. touch-action: none;
  352. /* #endif */
  353. }
  354. &__plus,
  355. &__minus {
  356. width: 35px;
  357. @include flex;
  358. justify-content: center;
  359. align-items: center;
  360. /* #ifndef APP-NVUE */
  361. touch-action: none;
  362. /* #endif */
  363. &--hover {
  364. background-color: $uv-numberBox-hover-bgColor !important;
  365. }
  366. &--disabled {
  367. color: $uv-numberBox-disabled-color;
  368. background-color: $uv-numberBox-disabled-bgColor;
  369. }
  370. }
  371. &__plus {
  372. border-top-right-radius: $uv-numberBox-plus-radius;
  373. border-bottom-right-radius: $uv-numberBox-plus-radius;
  374. }
  375. &__minus {
  376. border-top-left-radius: $uv-numberBox-minus-radius;
  377. border-bottom-left-radius: $uv-numberBox-minus-radius;
  378. }
  379. &__input {
  380. position: relative;
  381. text-align: $uv-numberBox-input-text-align;
  382. font-size: $uv-numberBox-input-font-size;
  383. padding: $uv-numberBox-input-padding;
  384. margin: $uv-numberBox-input-margin;
  385. @include flex;
  386. align-items: center;
  387. justify-content: center;
  388. &--disabled {
  389. color: $uv-numberBox-input-disabled-color;
  390. background-color: $uv-numberBox-input-disabled-bgColor;
  391. }
  392. }
  393. }
  394. </style>