add-number.vue 3.8 KB

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