cm-number-box.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="number-box">
  3. <view class="sub" @click="onSub" :class="{ disabled: subDisabled }"></view>
  4. <input type="number" class="control-input" v-model="inputValue" @blur="onBlur" />
  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: 0
  26. }
  27. },
  28. data() {
  29. return {
  30. inputValue: ''
  31. }
  32. },
  33. computed: {
  34. addDisabled() {
  35. return this.inputValue === this.max
  36. },
  37. subDisabled() {
  38. return this.inputValue === this.min
  39. }
  40. },
  41. watch: {
  42. inputValue(nVal) {
  43. this.$emit('input', parseInt(this.inputValue))
  44. }
  45. },
  46. created() {
  47. this.inputValue = this.value
  48. },
  49. methods: {
  50. onBlur() {
  51. if (this.inputValue < this.min) this.inputValue = this.min
  52. if (this.inputValue > this.max) this.inputValue = this.max
  53. this.$emit('change', this.inputValue)
  54. },
  55. // 减法
  56. onSub() {
  57. if (this.inputValue <= this.min) {
  58. return
  59. }
  60. this.inputValue--
  61. this.$emit('change', this.inputValue)
  62. },
  63. // 加法
  64. onAdd() {
  65. if (this.inputValue >= this.max) {
  66. return
  67. }
  68. this.inputValue++
  69. this.$emit('change', this.inputValue)
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .number-box {
  76. width: 148rpx;
  77. height: 50rpx;
  78. border: 1rpx solid #e1e1e1;
  79. border-radius: 25rpx;
  80. @extend .cm-flex-between;
  81. .add,
  82. .sub {
  83. @extend .cm-flex-center;
  84. flex: 1;
  85. font-size: 24rpx;
  86. color: #666666;
  87. &.disabled {
  88. color: #cccccc;
  89. }
  90. }
  91. .add {
  92. &::before {
  93. margin-bottom: 2rpx;
  94. content: '+';
  95. }
  96. }
  97. .sub {
  98. &::before {
  99. margin-bottom: 2rpx;
  100. content: '-';
  101. }
  102. }
  103. .control-input {
  104. width: 56rpx;
  105. height: 48rpx;
  106. border-right: 1rpx solid #e1e1e1;
  107. border-left: 1rpx solid #e1e1e1;
  108. background-color: #f7f7f7;
  109. box-sizing: border-box;
  110. text-align: center;
  111. font-size: 26rpx;
  112. color: #333;
  113. }
  114. }
  115. </style>