graceNumberBox.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="grace-number-box" :style="{width:width}">
  3. <view class="grace-number-box-doBtn" @tap.stop="reduce"
  4. :style="{
  5. width:btnSize, height:btnSize, fontSize:btnFontSize,
  6. lineHeight:btnSize, color:btnColr
  7. }">-</view>
  8. <input class="grace-number-box-input" :value="inputNumber"
  9. type="number" @blur="inputval" :disabled="disabled"
  10. :style="{
  11. background:inputBG, height:inputHeight, lineHeight:inputHeight,
  12. fontSize:inputFontSize, color:inputColor, padding:inputPadding,
  13. borderRadius:inputBorderRadius}"></input>
  14. <view class="grace-number-box-doBtn" @tap.stop="add"
  15. :style="{
  16. width:btnSize, height:btnSize, fontSize:btnFontSize,
  17. lineHeight:btnSize, color:btnColr}">+</view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: "graceNumberBox",
  23. props: {
  24. disabled :{
  25. type : Boolean,
  26. default : false
  27. },
  28. value : {
  29. type : Number,
  30. default : 0
  31. },
  32. index : {
  33. type : Number,
  34. default : 0
  35. },
  36. maxNum:{
  37. type: Number,
  38. default: 999999
  39. },
  40. minNum: {
  41. type: Number,
  42. default: 0
  43. },
  44. datas:{
  45. type: String,
  46. default: ''
  47. },
  48. btnSize : {
  49. type : String,
  50. default : '60rpx'
  51. },
  52. btnFontSize : {
  53. type : String,
  54. default : '36rpx'
  55. },
  56. btnColr : {
  57. type : String,
  58. default : '#666666'
  59. },
  60. inputHeight : {
  61. type : String,
  62. default : '30rpx'
  63. },
  64. inputFontSize : {
  65. type : String,
  66. default : '28rpx'
  67. },
  68. inputColor : {
  69. type : String,
  70. default : '#333333'
  71. },
  72. inputBG : {
  73. type : String,
  74. default : '#F6F7F8'
  75. },
  76. inputPadding : {
  77. type : String,
  78. default : '10rpx'
  79. },
  80. inputBorderRadius : {
  81. type : String,
  82. default : '8rpx'
  83. },
  84. width:{
  85. type : String,
  86. default : '200rpx'
  87. },
  88. step : {
  89. type : Number,
  90. default : 1
  91. }
  92. },
  93. data() {
  94. return {
  95. inputNumber: 0,
  96. callbackNumber : 0
  97. }
  98. },
  99. created:function(){
  100. this.inputNumber = Number(this.value);
  101. this.callbackNumber++;
  102. },
  103. watch:{
  104. value :function(val, vo){this.inputNumber = Number(val);},
  105. inputNumber :function(val, vo){
  106. val = Number(val);
  107. if(isNaN(val)){
  108. setTimeout(()=>{this.inputNumber = Number(vo);}, 200); return;
  109. }
  110. if(val > this.maxNum){
  111. this.$emit('change', [this.maxNum, this.index, this.datas]);
  112. setTimeout(()=>{this.inputNumber = this.maxNum;}, 200);
  113. return ;
  114. }
  115. if(val < this.minNum){
  116. this.$emit('change', [this.minNum, this.index, this.datas]);
  117. setTimeout(()=>{this.inputNumber = this.minNum;}, 200);
  118. return ;
  119. }
  120. if(val != vo && this.callbackNumber > 0){this.$emit('change', [val, this.index, this.datas]);}
  121. }
  122. },
  123. methods: {
  124. add : function(){
  125. var newVal = Number(this.inputNumber) + Number(this.step);
  126. if(newVal > this.maxNum){return ;}
  127. this.inputNumber = Number(newVal);
  128. },
  129. reduce: function () {
  130. var newVal = Number(this.inputNumber) - Number(this.step);
  131. if(newVal < this.minNum){return ;}
  132. this.inputNumber = newVal;
  133. },
  134. inputval:function (e) {
  135. this.inputNumber = e.detail.value;
  136. }
  137. }
  138. }
  139. </script>
  140. <style scoped>
  141. .grace-number-box{display:flex; overflow:hidden; flex-wrap:nowrap; align-items:center;}
  142. .grace-number-box-doBtn{text-align:center; color:#666666; flex-shrink:0;}
  143. .grace-number-box-input{text-align:center; width:700rpx;}
  144. </style>