cm-number-box.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <view class="number-box">
  3. <view class="sub iconfont icon-jianhao" @click="sub"></view>
  4. <input class="number" type="number" v-model="count" @blur="blur" />
  5. <view class="add iconfont icon-jiahao" @click="add"></view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'number-box',
  11. data() {
  12. return {
  13. count: 1
  14. }
  15. },
  16. model: {
  17. prop: 'value',
  18. event: 'input'
  19. },
  20. props: {
  21. value: {
  22. type: Number,
  23. default: 0
  24. },
  25. defaultVal: {
  26. type:Number,
  27. default: 0
  28. },
  29. max: {
  30. type: Number,
  31. default: 99999999
  32. },
  33. min: {
  34. type: Number,
  35. default: 1
  36. }
  37. },
  38. watch: {
  39. count(nVal){
  40. this.count = parseInt(nVal)
  41. },
  42. defaultVal(nVal){
  43. this.count = nVal
  44. }
  45. },
  46. created() {
  47. this.count = this.defaultVal
  48. },
  49. methods: {
  50. add() {
  51. if (this.count === this.max) {
  52. return this.$util.msg(`购买数量不能大于${this.max}`, 2000)
  53. }
  54. this.count++
  55. this.$emit('input', this.count)
  56. this.$emit('change', this.count)
  57. },
  58. sub() {
  59. if (this.count === this.min) {
  60. return this.$util.msg(`购买数量不能少于${this.min}`, 2000)
  61. }
  62. this.count--
  63. this.$emit('input', this.count)
  64. this.$emit('change', this.count)
  65. },
  66. blur() {
  67. if (this.count > this.max) {
  68. this.count = this.max
  69. return this.$util.msg(`购买数量不能大于${this.max}`, 2000)
  70. }
  71. if (this.count < this.min) {
  72. this.count = this.min
  73. return this.$util.msg(`购买数量不能少于${this.min}`, 2000)
  74. }
  75. this.$emit('input', this.count)
  76. this.$emit('change', this.count)
  77. }
  78. }
  79. }
  80. </script>
  81. <style lang="scss" scoped>
  82. .number-box {
  83. width: 148rpx;
  84. height: 48rpx;
  85. box-sizing: border-box;
  86. display: flex;
  87. justify-content: space-between;
  88. align-items: center;
  89. border-radius: 24rpx;
  90. border: 1rpx solid #e1e1e1;
  91. .sub,
  92. .add {
  93. flex: 1;
  94. text-align: center;
  95. font-size: 28rpx;
  96. color: #ccc;
  97. }
  98. .number {
  99. text-align: center;
  100. width: 56rpx;
  101. font-size: 26rpx;
  102. font-weight: 500;
  103. color: #333333;
  104. box-sizing: border-box;
  105. border-right: 1rpx solid #e1e1e1;
  106. border-left: 1rpx solid #e1e1e1;
  107. }
  108. }
  109. </style>