num-input.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  38. },
  39. add() {
  40. if (this.value + 1 > this.max) {
  41. uni.toast('数量不能再多了')
  42. } else {
  43. this.$emit('change', this.value + 1, ...this.args)
  44. }
  45. },
  46. comfirmNum() {
  47. if (this.inputNum) {
  48. if (+this.inputNum > this.max) {
  49. this.$emit('change', this.max, ...this.args)
  50. } else {
  51. this.$emit('change', +this.inputNum, ...this.args)
  52. }
  53. } else {
  54. this.$emit('change', 0, ...this.args)
  55. }
  56. this.showInput = false
  57. }
  58. }
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .num-input {
  63. @include flex();
  64. width: 200rpx;
  65. height: 64rpx;
  66. overflow: hidden;
  67. font-size: 32rpx;
  68. background: #F2F2F2;
  69. border-radius: 64rpx;
  70. .i {
  71. flex: 1;
  72. height: 100%;
  73. @include flex();
  74. &.c {
  75. border-left: 4rpx solid #FFFFFF;
  76. border-right: 4rpx solid #FFFFFF;
  77. }
  78. }
  79. .inputModelWrapper {
  80. @include flex();
  81. top: 0;
  82. left: 0;
  83. z-index: 2;
  84. width: 100vw;
  85. height: 100vh;
  86. position: fixed;
  87. background: rgba(0, 0, 0, 0.3);
  88. .inputModel {
  89. width: 600rpx;
  90. height: 400rpx;
  91. background: #FFFFFF;
  92. border-radius: 8rpx;
  93. @include flex(column);
  94. .t {
  95. @include flex();
  96. width: 100%;
  97. height: 100rpx;
  98. border-bottom: 1rpx solid #CCCCCC;
  99. }
  100. .m {
  101. width: 100%;
  102. flex: 1;
  103. @include flex();
  104. input {
  105. width: 240rpx;
  106. height: 120rpx;
  107. text-align: center;
  108. font-size: 88rpx;
  109. border-radius: 8rpx;
  110. border: 1rpx solid #CCCCCC;
  111. }
  112. }
  113. .b {
  114. @include flex();
  115. width: 100%;
  116. height: 100rpx;
  117. border-top: 1rpx solid #CCCCCC;
  118. text {
  119. height: 100%;
  120. flex: 1;
  121. @include flex();
  122. &.basecolor {
  123. border-left: 1rpx solid #CCCCCC;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. </style>