uni-number-box.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view class="hx-numbox">
  3. <view @click="_calcValue('minus')" class="hx-numbox__minus" v-if="inputValue>0">
  4. <text class="hx-numbox--text" :class="{ 'hx-numbox--disabled': inputValue <= min || disabled }">-</text>
  5. </view>
  6. <input :disabled="true" @blur="_onBlur" class="hx-numbox__value" type="number" v-model="inputValue" v-if="inputValue>0"/>
  7. <view @click="_calcValue('plus')" class="hx-numbox__plus" >
  8. <text class="hx-numbox--text" :class="{ 'hx-numbox--disabled': inputValue >= max || disabled }">+</text>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: "hxNumberBox",
  15. props: {
  16. value: {
  17. type: [Number, String],
  18. default: 0
  19. },
  20. min: {
  21. type: Number,
  22. default: 0
  23. },
  24. max: {
  25. type: Number,
  26. default: 100
  27. },
  28. step: {
  29. type: Number,
  30. default: 1
  31. },
  32. disabled: {
  33. type: Boolean,
  34. default: false
  35. },
  36. rowData: {
  37. type: Object,
  38. default: ()=>{
  39. return {}
  40. }
  41. },
  42. clickTime: {
  43. type: Number,
  44. default: 0
  45. }
  46. },
  47. data() {
  48. return {
  49. inputValue: 0,
  50. addStaus: true,
  51. };
  52. },
  53. watch: {
  54. value(val) {
  55. this.inputValue = +val;
  56. },
  57. inputValue(newVal, oldVal) {
  58. if (+newVal !== +oldVal) {
  59. //this.$emit("change", newVal,this.rowData);
  60. }
  61. /* if(+newVal > +oldVal){
  62. } */
  63. }
  64. },
  65. created() {
  66. this.inputValue = +this.value;
  67. },
  68. methods: {
  69. _calcValue(type) {
  70. let that = this;
  71. if (this.disabled) {
  72. return;
  73. }
  74. const scale = this._getDecimalScale();
  75. let value = this.inputValue * scale;
  76. let step = this.step * scale;
  77. if (type === "minus") {
  78. this.$emit("lessChange", this.inputValue,this.rowData);
  79. value -= step;
  80. if (value < this.min) {
  81. return;
  82. }
  83. if(value > this.max){
  84. value = this.max
  85. }
  86. } else if (type === "plus") {
  87. this.$emit("addChange", this.inputValue,this.rowData);
  88. if(that.clickTime > 0){
  89. if(!this.addStaus){
  90. return;
  91. }else{
  92. this.addStaus = false;
  93. setTimeout(()=>{
  94. that.addStaus = true;
  95. },that.clickTime)
  96. }
  97. }
  98. value += step;
  99. if (value > this.max) {
  100. return;
  101. }
  102. if(value < this.min){
  103. value = this.min
  104. }
  105. }
  106. this.inputValue = String(value / scale);
  107. this.$emit("change", this.inputValue,this.rowData);
  108. },
  109. _getDecimalScale() {
  110. let scale = 1;
  111. // 浮点型
  112. if (~~this.step !== this.step) {
  113. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  114. }
  115. return scale;
  116. },
  117. _onBlur(event) {
  118. let value = event.detail.value;
  119. if (!value) {
  120. // this.inputValue = 0;
  121. return;
  122. }
  123. value = +value;
  124. if (value > this.max) {
  125. value = this.max;
  126. } else if (value < this.min) {
  127. value = this.min;
  128. }
  129. this.inputValue = value;
  130. }
  131. }
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. $box-height: 28px;
  136. $box-line-height: 22px;
  137. $box-width: 28px;
  138. .hx-numbox {
  139. /* #ifndef APP-NVUE */
  140. display: flex;
  141. /* #endif */
  142. flex-direction: row;
  143. height: $box-height;
  144. line-height: $box-height;
  145. width: 85px;
  146. justify-content: flex-end;
  147. }
  148. .hx-numbox__value {
  149. width: 28px;
  150. height: $box-height;
  151. text-align: center;
  152. font-size: $uni-font-size-lg;
  153. color: #222222;
  154. }
  155. .hx-numbox__minus {
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. flex-direction: row;
  160. align-items: center;
  161. justify-content: center;
  162. width: $box-width - 1;
  163. height: $box-height - 1;
  164. font-size: 30px;
  165. color: $uni-text-color;
  166. background-color: #f8f8f8;
  167. border-radius: 50%;
  168. border: 1px solid #d8d8d8;
  169. color: #868686;
  170. }
  171. .hx-numbox__plus {
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. flex-direction: row;
  176. align-items: center;
  177. justify-content: center;
  178. width: $box-width;
  179. height: $box-height;
  180. background: linear-gradient(100deg, #FFEB3B, #FFC107);
  181. font-size: 22px;
  182. border-radius: 50%;
  183. }
  184. .hx-numbox--text {
  185. font-size: 40rpx;
  186. color: $uni-text-color;
  187. }
  188. .hx-numbox--disabled {
  189. color: $uni-text-color-disable;
  190. }
  191. </style>