num-input.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="num-input">
  3. <view class="i l" @tap.stop="red">-</view>
  4. <view class="i c" @tap.stop="inputNum = value ? value : ''; showInput = true">{{ value }}</view>
  5. <view class="i r basecolor" @tap.stop="add">+</view>
  6. <view v-if="showInput" class="inputModelWrapper">
  7. <view class="inputModel">
  8. <view class="t">请输入数量</view>
  9. <view class="m">
  10. <input ref="input" focus type="number" maxlength="4" v-model="inputNum" />
  11. </view>
  12. <view class="b">
  13. <text class="l" @tap.stop="showInput = false">取消</text>
  14. <text class="r basecolor" @tap.stop="comfirmNum">确定</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. value: { type: Number, default: 0 },
  24. args: { type: Array, default: () => [] },
  25. max: { type: Number, default: 9999 }
  26. },
  27. data() {
  28. return {
  29. showInput: false,
  30. inputNum: 0
  31. }
  32. },
  33. methods: {
  34. red() {
  35. if (this.value > 0) {
  36. this.$emit('change', this.value - 1, ...this.args)
  37. this.$emit('input', this.value - 1, ...this.args)
  38. }
  39. },
  40. add() {
  41. if (this.value + 1 > this.max) {
  42. uni.toast('数量不能再多了')
  43. } else {
  44. this.$emit('change', this.value + 1, ...this.args)
  45. this.$emit('input', this.value + 1, ...this.args)
  46. }
  47. },
  48. comfirmNum() {
  49. if (this.inputNum) {
  50. if (+this.inputNum > this.max) {
  51. this.$emit('change', this.max, ...this.args)
  52. this.$emit('input', this.max, ...this.args)
  53. } else {
  54. this.$emit('change', +this.inputNum, ...this.args)
  55. this.$emit('input', +this.inputNum, ...this.args)
  56. }
  57. } else {
  58. this.$emit('change', 0, ...this.args)
  59. this.$emit('input', 0, ...this.args)
  60. }
  61. this.showInput = false
  62. }
  63. }
  64. }
  65. </script>
  66. <style lang="scss" scoped>
  67. .num-input {
  68. @include flex();
  69. width: 200rpx;
  70. height: 64rpx;
  71. overflow: hidden;
  72. font-size: 32rpx;
  73. background: #F2F2F2;
  74. border-radius: 64rpx;
  75. .i {
  76. flex: 1;
  77. height: 100%;
  78. @include flex();
  79. &.c {
  80. border-left: 4rpx solid #FFFFFF;
  81. border-right: 4rpx solid #FFFFFF;
  82. }
  83. }
  84. .inputModelWrapper {
  85. @include flex();
  86. top: 0;
  87. left: 0;
  88. z-index: 2;
  89. width: 100vw;
  90. height: 100vh;
  91. position: fixed;
  92. background: rgba(0, 0, 0, 0.3);
  93. .inputModel {
  94. width: 600rpx;
  95. height: 400rpx;
  96. background: #FFFFFF;
  97. border-radius: 8rpx;
  98. @include flex(column);
  99. .t {
  100. @include flex();
  101. width: 100%;
  102. height: 100rpx;
  103. border-bottom: 1rpx solid #CCCCCC;
  104. }
  105. .m {
  106. width: 100%;
  107. flex: 1;
  108. @include flex();
  109. input {
  110. width: 240rpx;
  111. height: 120rpx;
  112. text-align: center;
  113. font-size: 88rpx;
  114. border-radius: 8rpx;
  115. border: 1rpx solid #CCCCCC;
  116. }
  117. }
  118. .b {
  119. @include flex();
  120. width: 100%;
  121. height: 100rpx;
  122. border-top: 1rpx solid #CCCCCC;
  123. text {
  124. height: 100%;
  125. flex: 1;
  126. @include flex();
  127. &.basecolor {
  128. border-left: 1rpx solid #CCCCCC;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. </style>