graceNvueNumberBox.vue 3.2 KB

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