cm-number-box.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="number-box">
  3. <view class="sub" @click="onSub" :class="{ disabled: subDisabled }"></view>
  4. <input type="number" class="control-input" :value="value" @input="onInput" />
  5. <view class="add" @click="onAdd" :class="{ disabled: addDisabled }"></view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. model: {
  11. prop: 'value',
  12. event: 'input'
  13. },
  14. props: {
  15. min: {
  16. type: Number,
  17. default: 1
  18. },
  19. max: {
  20. type: Number,
  21. default: 99999
  22. },
  23. value: {
  24. type: Number,
  25. default: 1
  26. }
  27. },
  28. computed: {
  29. addDisabled() {
  30. return this.value === this.max
  31. },
  32. subDisabled() {
  33. return this.value === this.min
  34. }
  35. },
  36. watch: {
  37. value(nval) {
  38. console.log(nval)
  39. }
  40. },
  41. methods: {
  42. // 减法
  43. onSub() {
  44. if (this.subDisabled) return
  45. let inputValue = this.value
  46. if (inputValue <= this.min) {
  47. inputValue = this.min
  48. } else {
  49. inputValue--
  50. }
  51. this.$emit('input', inputValue)
  52. this.$emit('change', inputValue)
  53. },
  54. // 加法
  55. onAdd() {
  56. if (this.addDisabled) return
  57. let inputValue = this.value
  58. if (inputValue >= this.max) {
  59. inputValue = this.max
  60. } else {
  61. inputValue++
  62. }
  63. this.$emit('input', inputValue)
  64. this.$emit('change', inputValue)
  65. },
  66. // 失去焦点
  67. onInput(e) {
  68. let inputValue = parseInt(e.detail.value)
  69. if (isNaN(inputValue)) {
  70. inputValue = this.min
  71. }
  72. if (inputValue < this.min) {
  73. inputValue = this.min
  74. } else if (inputValue > this.max) {
  75. inputValue = this.max
  76. }
  77. this.$emit('input', inputValue)
  78. this.$emit('change', inputValue)
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .number-box {
  85. width: 148rpx;
  86. height: 50rpx;
  87. border: 1rpx solid #e1e1e1;
  88. border-radius: 25rpx;
  89. @extend .cm-flex-between;
  90. .add,
  91. .sub {
  92. @extend .cm-flex-center;
  93. flex: 1;
  94. font-size: 24rpx;
  95. color: #666666;
  96. &.disabled {
  97. color: #cccccc;
  98. }
  99. }
  100. .add {
  101. &::before {
  102. margin-bottom: 2rpx;
  103. content: '+';
  104. }
  105. }
  106. .sub {
  107. &::before {
  108. margin-bottom: 2rpx;
  109. content: '-';
  110. }
  111. }
  112. .control-input {
  113. width: 56rpx;
  114. height: 48rpx;
  115. border-right: 1rpx solid #e1e1e1;
  116. border-left: 1rpx solid #e1e1e1;
  117. background-color: #f7f7f7;
  118. box-sizing: border-box;
  119. text-align: center;
  120. font-size: 26rpx;
  121. color: #333;
  122. }
  123. }
  124. </style>