index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view class="uni-numbox">
  3. <view class="uni-numbox-minus" @tap.stop="_calcValue('subtract')">
  4. <text
  5. class="iconfont yticon icon-jianhao"
  6. :class="minDisabled ? 'uni-numbox-disabled' : ''"
  7. ></text>
  8. </view>
  9. <input
  10. class="uni-numbox-value"
  11. type="number"
  12. :disabled="disabled"
  13. v-model="inputValue"
  14. @input.stop="_onBlur"
  15. />
  16. <view class="uni-numbox-plus" @tap.stop="_calcValue('add')">
  17. <text
  18. class="iconfont yticon iconjia2"
  19. :class="maxDisabled ? 'uni-numbox-disabled' : ''"
  20. ></text>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. name: 'rf-number-box',
  27. props: {
  28. isMax: {
  29. type: Boolean,
  30. default: false
  31. },
  32. isMin: {
  33. type: Boolean,
  34. default: false
  35. },
  36. index: {
  37. type: Number,
  38. default: 0
  39. },
  40. skuId: {
  41. type: Number,
  42. default: 0
  43. },
  44. value: {
  45. type: Number,
  46. default: 0
  47. },
  48. min: {
  49. type: Number,
  50. default: -Infinity
  51. },
  52. max: {
  53. type: Number,
  54. default: Infinity
  55. },
  56. step: {
  57. type: Number,
  58. default: 1
  59. },
  60. disabled: {
  61. type: Boolean,
  62. default: false
  63. }
  64. },
  65. data() {
  66. return {
  67. inputValue: parseInt(this.value, 10),
  68. minDisabled: false,
  69. maxDisabled: false
  70. };
  71. },
  72. created() {
  73. this.maxDisabled = this.isMax;
  74. this.minDisabled = this.isMin;
  75. },
  76. computed: {},
  77. watch: {
  78. inputValue(number) {
  79. const data = {
  80. number: number,
  81. index: this.index,
  82. skuId: this.skuId
  83. };
  84. this.$emit('eventChange', data);
  85. }
  86. },
  87. methods: {
  88. _calcValue(type) {
  89. const scale = this._getDecimalScale();
  90. let value = this.inputValue * scale;
  91. let newValue = 0;
  92. let step = this.step * scale;
  93. if (type === 'subtract') {
  94. newValue = value - step;
  95. if (newValue <= this.min) {
  96. this.minDisabled = true;
  97. }
  98. if (newValue < this.min) {
  99. newValue = this.min;
  100. }
  101. if (newValue < this.max && this.maxDisabled === true) {
  102. this.maxDisabled = false;
  103. }
  104. } else if (type === 'add') {
  105. newValue = value + step;
  106. if (newValue >= this.max) {
  107. this.maxDisabled = true;
  108. }
  109. if (newValue > this.max) {
  110. newValue = this.max;
  111. }
  112. if (newValue > this.min && this.minDisabled === true) {
  113. this.minDisabled = false;
  114. }
  115. }
  116. if (newValue === value) {
  117. return;
  118. }
  119. this.inputValue = parseInt(newValue / scale, 10);
  120. },
  121. _getDecimalScale() {
  122. let scale = 1;
  123. // 浮点型
  124. if (~~this.step !== this.step) {
  125. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  126. }
  127. return scale;
  128. },
  129. _onBlur(event) {
  130. let value = event.detail.value;
  131. if (!value) {
  132. this.inputValue = 1;
  133. return;
  134. }
  135. value = +value;
  136. if (value > this.max) {
  137. value = this.max;
  138. } else if (value < this.min) {
  139. value = this.min;
  140. }
  141. this.inputValue = parseInt(value, 10);
  142. this.$emit('eventChange', this.inputValue);
  143. }
  144. }
  145. };
  146. </script>
  147. <style>
  148. .uni-numbox {
  149. position: absolute;
  150. left: 0;
  151. bottom: 0;
  152. display: flex;
  153. justify-content: flex-start;
  154. align-items: center;
  155. width: 230upx;
  156. height: 70upx;
  157. background: #f5f5f5;
  158. }
  159. .uni-numbox-minus,
  160. .uni-numbox-plus {
  161. margin: 0;
  162. background-color: #f5f5f5;
  163. width: 70upx;
  164. height: 100%;
  165. line-height: 70upx;
  166. text-align: center;
  167. position: relative;
  168. }
  169. .uni-numbox-minus .yticon,
  170. .uni-numbox-plus .yticon {
  171. font-size: 36upx;
  172. color: #555;
  173. }
  174. .uni-numbox-minus {
  175. border-right: none;
  176. border-top-left-radius: 6upx;
  177. border-bottom-left-radius: 6upx;
  178. }
  179. .uni-numbox-plus {
  180. border-left: none;
  181. border-top-right-radius: 6upx;
  182. border-bottom-right-radius: 6upx;
  183. }
  184. .uni-numbox-value {
  185. position: relative;
  186. background-color: #f5f5f5;
  187. width: 90upx;
  188. height: 50upx;
  189. text-align: center;
  190. padding: 0;
  191. font-size: 30upx;
  192. }
  193. .uni-numbox-disabled.yticon {
  194. color: #d6d6d6;
  195. }
  196. </style>