uni-number-box.vue 3.7 KB

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