gui-step-box.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view class="gui-flex gui-rows gui-nowrap gui-align-items-center"
  3. :style="{width:width}">
  4. <view hover-class="gui-tap">
  5. <text class="gui-block-text gui-text-center gui-number-box-button" @tap.stop="reduce"
  6. :style="buttonStyle">-</text>
  7. </view>
  8. <input class="gui-number-box-input" :disabled="disabled"
  9. v-model="inputNumber" type="digit" @blur="inputval"
  10. :style="inputStyle" />
  11. <view hover-class="gui-tap">
  12. <text class="gui-block-text gui-text-center gui-number-box-button" @tap.stop="add"
  13. :style="buttonStyle">+</text>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name : "gui-step-box",
  20. props : {
  21. width : { type : String, default : '200rpx' },
  22. value : { type : Number, default : 0 },
  23. step : { type : Number, default : 1 },
  24. maxNum : { type : Number, default : 9999 },
  25. minNum : { type : Number, default : 0 },
  26. buttonStyle : { type : String, default : 'width:66rpx; font-size:38rpx; color:rgba(69, 90, 100, 0.6);' },
  27. inputStyle : { type : String, default : 'line-height:58rpx; height:58rpx; font-size:26rpx; color:#2B2E3D; background-color:#F6F7F8; border-radius:8rpx;'},
  28. disabled : { type : Boolean, default : false },
  29. index : { type : Number, default : 0 },
  30. datas : { type : Array, default : function () {
  31. return [];
  32. }},
  33. decimal : { type : Number, default:2}
  34. },
  35. data() {
  36. return {
  37. inputNumber : 0,
  38. callbackNumber : 0
  39. }
  40. },
  41. created:function(){
  42. this.inputNumber = Number(this.value);
  43. },
  44. watch:{
  45. value : function(val, vo){
  46. this.inputNumber = Number(val);
  47. },
  48. inputNumber :function(val, vo){
  49. val = Number(val);
  50. if(isNaN(val)){
  51. setTimeout(()=>{this.inputNumber = Number(vo);}, 200); return;
  52. }
  53. var newVal = this.decimalVal(val);
  54. if(newVal != val){
  55. setTimeout(()=>{this.inputNumber = Number(newVal);}, 200);
  56. return;
  57. }
  58. if(val > this.maxNum){
  59. setTimeout(()=>{this.inputNumber = this.maxNum;}, 200);
  60. return ;
  61. }
  62. if(val < this.minNum){
  63. setTimeout(()=>{this.inputNumber = this.minNum;}, 200);
  64. return ;
  65. }
  66. }
  67. },
  68. methods : {
  69. add : function(){
  70. var newVal = Number(this.inputNumber) + Number(this.step);
  71. newVal = this.decimalVal(newVal);
  72. if(newVal > this.maxNum){return ;}
  73. this.inputNumber = Number(newVal);
  74. setTimeout(()=>{
  75. this.$emit('change', [this.inputNumber, this.index, this.datas]);
  76. }, 300);
  77. },
  78. reduce : function () {
  79. var newVal = Number(this.inputNumber) - Number(this.step);
  80. newVal = this.decimalVal(newVal);
  81. if(newVal < this.minNum){return ;}
  82. this.inputNumber = newVal;
  83. setTimeout(()=>{
  84. this.$emit('change', [this.inputNumber, this.index, this.datas]);
  85. }, 300);
  86. },
  87. inputval : function (e) {
  88. this.inputNumber = e.detail.value;
  89. setTimeout(()=>{
  90. this.$emit('change', [this.inputNumber, this.index, this.datas]);
  91. }, 300);
  92. },
  93. decimalVal : function (val) {
  94. var isDecimal = String(val).indexOf(".");
  95. if(isDecimal != -1){
  96. val = val.toFixed(this.decimal);
  97. var valArr = String(val).split('.');
  98. if(valArr[1].length > this.decimal){
  99. valArr[1] = valArr[1].substr(0, this.decimal);
  100. val = Number(valArr.join('.'));
  101. }
  102. }
  103. return val ;
  104. }
  105. }
  106. }
  107. </script>
  108. <style scoped>
  109. /* #ifndef APP-NVUE */
  110. .gui-number-box-input{text-align:center; width:500rpx;}
  111. .gui-number-box-button{overflow:hidden; flex-shrink:0;}
  112. /* #endif */
  113. /* #ifdef APP-NVUE */
  114. .gui-number-box-input{text-align:center; width:50rpx; flex:1;}
  115. .gui-number-box-button{overflow:hidden;}
  116. /* #endif */
  117. </style>