You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.2 KiB

6 months ago
  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. if(that.rowData.num>=that.rowData.kcnum){
  88. uni.showToast({
  89. title:'库存不足!',
  90. icon:'none'
  91. });
  92. return;
  93. }
  94. this.$emit("addChange", this.inputValue,this.rowData);
  95. if(that.clickTime > 0){
  96. if(!this.addStaus){
  97. return;
  98. }else{
  99. this.addStaus = false;
  100. setTimeout(()=>{
  101. that.addStaus = true;
  102. },that.clickTime)
  103. }
  104. }
  105. value += step;
  106. if (value > this.max) {
  107. return;
  108. }
  109. if(value < this.min){
  110. value = this.min
  111. }
  112. }
  113. this.inputValue = String(value / scale);
  114. this.$emit("change", this.inputValue,this.rowData);
  115. },
  116. _getDecimalScale() {
  117. let scale = 1;
  118. // 浮点型
  119. if (~~this.step !== this.step) {
  120. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  121. }
  122. return scale;
  123. },
  124. _onBlur(event) {
  125. let value = event.detail.value;
  126. if (!value) {
  127. // this.inputValue = 0;
  128. return;
  129. }
  130. value = +value;
  131. if (value > this.max) {
  132. value = this.max;
  133. } else if (value < this.min) {
  134. value = this.min;
  135. }
  136. this.inputValue = value;
  137. }
  138. }
  139. };
  140. </script>
  141. <style lang="scss" scoped>
  142. $box-height: 28px;
  143. $box-line-height: 22px;
  144. $box-width: 28px;
  145. .hx-numbox {
  146. /* #ifndef APP-NVUE */
  147. display: flex;
  148. /* #endif */
  149. flex-direction: row;
  150. height: $box-height;
  151. line-height: $box-height;
  152. width: 85px;
  153. justify-content: flex-end;
  154. }
  155. .hx-numbox__value {
  156. width: 28px;
  157. height: $box-height;
  158. text-align: center;
  159. font-size: $uni-font-size-lg;
  160. color: #222222;
  161. }
  162. .hx-numbox__minus {
  163. /* #ifndef APP-NVUE */
  164. display: flex;
  165. /* #endif */
  166. flex-direction: row;
  167. align-items: center;
  168. justify-content: center;
  169. width: $box-width - 4;
  170. height: $box-height - 4;
  171. font-size: 22px;
  172. color: $uni-text-color;
  173. background-color: #f8f8f8;
  174. border-radius: 50%;
  175. border: 2px solid #d8d8d8;
  176. color: #868686;
  177. }
  178. .hx-numbox__plus {
  179. /* #ifndef APP-NVUE */
  180. display: flex;
  181. /* #endif */
  182. flex-direction: row;
  183. align-items: center;
  184. justify-content: center;
  185. width: $box-width;
  186. height: $box-height;
  187. background: linear-gradient(100deg, #67C979, #39A74D);
  188. font-size: 22px;
  189. border-radius: 50%;
  190. }
  191. .hx-numbox--text {
  192. font-size: 40rpx;
  193. color: #BCBCBC;
  194. }
  195. .hx-numbox--disabled {
  196. color: $uni-text-color-disable;
  197. }
  198. </style>