set-count.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="set-count flex aic" :class="{'mini': size === 'mini'}">
  3. <view class="box box-left" :class="{'mini': size === 'mini'}"
  4. :style="{color:count <= countMin || disabled?'#BDC1C4':'#000', background:count <= countMin || disabled?'#F2F3F5':''}"
  5. @click.stop="cut(count)">-
  6. </view>
  7. <view class="one" :style="{width: size === 'max' ? '156rpx' : '100rpx'}">
  8. <u--input :customStyle="{width: '100%', height: '100%'}" v-model="count" :disabled="disabled" value="count"
  9. border="none" @focus="focus" @blur="blur" inputAlign="center" @input="inputVal"></u--input>
  10. </view>
  11. <view class="box box-right" :class="{'mini': size === 'mini'}"
  12. :style="{color:count >= countStock || count >= countmax || disabled?'#BDC1C4':'#000', background:count >= countStock || count >= countmax || disabled?'#F2F3F5':''}"
  13. @click.stop="add(count)">+
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. disabled: Boolean,
  21. size: {
  22. type: String,
  23. default: 'max'
  24. },
  25. countStock: { // 库存值
  26. type: Number,
  27. default: 99999,
  28. },
  29. countmax: { //最大值
  30. type: Number,
  31. default: 99999,
  32. },
  33. countMin: { // 最小值
  34. type: Number,
  35. default: 1,
  36. },
  37. value: { // 绑定值
  38. type: [Number, String],
  39. default: 1
  40. },
  41. step: { // 每次加减数
  42. type: [Number, String],
  43. default: 1
  44. }
  45. },
  46. data() {
  47. return {
  48. isCtrl: false,
  49. count: 0,
  50. isInit: true // 是否首次进入
  51. }
  52. },
  53. mounted() {
  54. this.count = this.countMin > this.value ? +this.countMin : +this.value
  55. },
  56. watch: {
  57. value(v) {
  58. this.count = this.countMin > this.value ? +this.countMin : +this.value
  59. }
  60. },
  61. methods: {
  62. setCount(count) {
  63. this.count = count
  64. this.$showToast({
  65. title: `最大设置${count}`,
  66. mask: false
  67. })
  68. },
  69. focus() {
  70. this.isCtrl = false
  71. },
  72. blur() {
  73. this.$nextTick(() => {
  74. if (this.count > this.countStock || this.count > this.countmax) {
  75. this.setCount(this.countStock <= this.countmax ? this.countStock : this.countmax)
  76. this.$emit('input', this.count)
  77. this.$emit('change', this.count)
  78. } else {
  79. if (this.count < this.countMin) {
  80. this.count = this.countMin
  81. }
  82. this.$emit('input', this.count)
  83. this.$emit('change', this.count)
  84. }
  85. })
  86. },
  87. inputVal() {
  88. this.$nextTick(() => {
  89. const inputType = /^0|[^\d]+/g
  90. this.count = String(this.count).replace(inputType, '')
  91. })
  92. },
  93. // 增加数量
  94. add() {
  95. if (this.disabled) {
  96. return
  97. }
  98. this.count = +this.count
  99. if (this.count < this.countmax && this.count < this.countStock) {
  100. this.count += Number(this.step)
  101. this.$emit('input', this.count)
  102. this.$emit('change', this.count)
  103. }
  104. },
  105. // 减少数量
  106. cut() {
  107. if (this.disabled) {
  108. return
  109. }
  110. this.isCtrl = true
  111. this.count = +this.count
  112. if (this.count > this.countMin) {
  113. this.count -= Number(this.step)
  114. this.$emit('input', this.count)
  115. this.$emit('change', this.count)
  116. }
  117. }
  118. }
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. .set-count {
  123. height: 72rpx;
  124. color: $color-text-m;
  125. border-radius: 4rpx;
  126. border: 2rpx solid #DCDFE6;
  127. &.mini {
  128. height: 58rpx;
  129. }
  130. .box {
  131. display: flex;
  132. width: 70rpx;
  133. height: 70rpx;
  134. justify-content: center;
  135. align-items: center;
  136. &.mini {
  137. width: 56rpx;
  138. height: 56rpx;
  139. }
  140. }
  141. .box-left {
  142. border-right: 2rpx solid #DCDFE6;
  143. }
  144. .box-right {
  145. border-left: 2rpx solid #DCDFE6;
  146. }
  147. .one {
  148. width: 156rpx;
  149. height: 100%;
  150. }
  151. }
  152. </style>