uni-number-box.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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" :value="inputValue" @blur="_onBlur">
  7. <view class="uni-numbox-plus" @click="_calcValue('add')">
  8. <text class="iconfont iconadd" :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. console.log(this.min, this.max)
  78. if (type === 'subtract') {
  79. newValue = value - step;
  80. if (newValue <= this.min) {
  81. this.minDisabled = true;
  82. }
  83. if (newValue < this.min) {
  84. newValue = this.min
  85. }
  86. if (newValue < this.max && this.maxDisabled === true) {
  87. this.maxDisabled = false;
  88. }
  89. } else if (type === 'add') {
  90. newValue = value + step;
  91. if (newValue >= this.max) {
  92. this.maxDisabled = true;
  93. }
  94. if (newValue > this.max) {
  95. newValue = this.max
  96. }
  97. if (newValue > this.min && this.minDisabled === true) {
  98. this.minDisabled = false;
  99. }
  100. }
  101. if (newValue === value) {
  102. return;
  103. }
  104. this.inputValue = newValue / scale;
  105. },
  106. _getDecimalScale() {
  107. let scale = 1;
  108. // 浮点型
  109. if (~~this.step !== this.step) {
  110. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  111. }
  112. return scale;
  113. },
  114. _onBlur(event) {
  115. let value = event.detail.value;
  116. if (!value) {
  117. this.inputValue = 0;
  118. return
  119. }
  120. value = +value;
  121. if (value > this.max) {
  122. value = this.max;
  123. } else if (value < this.min) {
  124. value = this.min
  125. }
  126. this.inputValue = value
  127. }
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .uni-numbox {
  133. /* position:absolute; */
  134. /* left: 30rpx; */
  135. /* bottom: 0; */
  136. display: flex;
  137. justify-content: flex-start;
  138. align-items: center;
  139. width: 180rpx;
  140. height: 52rpx;
  141. background: #f5f5f5;
  142. /* border: 1px solid #C7C7C7; */
  143. /* border-radius: 4rpx 4rpx 4rpx 4rpx; */
  144. /* z-index: 999; */
  145. }
  146. .uni-numbox-minus,
  147. .uni-numbox-plus {
  148. border: 1px solid #C7C7C7;
  149. margin: 0;
  150. background-color: #f5f5f5;
  151. width: 60rpx;
  152. height: 52rpx;
  153. line-height: 52rpx;
  154. text-align: center;
  155. position: relative;
  156. }
  157. .uni-numbox-minus .yticon,
  158. .uni-numbox-plus .yticon {
  159. font-size: 36rpx;
  160. color: #555;
  161. }
  162. .uni-numbox-minus {
  163. border-right: none;
  164. border-top-left-radius: 4rpx;
  165. border-bottom-left-radius: 4rpx;
  166. }
  167. .uni-numbox-plus {
  168. border-left: none;
  169. border-top-right-radius: 4rpx;
  170. border-bottom-right-radius: 4rpx;
  171. }
  172. .uni-numbox-value {
  173. position: relative;
  174. background-color: #f5f5f5;
  175. width: 60rpx;
  176. height: 52rpx;
  177. text-align: center;
  178. padding: 0;
  179. font-size: 30rpx;
  180. border: 1px solid #C7C7C7;
  181. }
  182. .uni-numbox-disabled {
  183. color: #d6d6d6 !important;
  184. }
  185. </style>