gui-step-box.vue 3.3 KB

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