uni-number-box.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view class="uni-numbox">
  3. <view class="uni-numbox-minus"
  4. @click="_calcValue('subtract')"
  5. >
  6. <text class="iconfont icon-jianhao" :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 icon-jiahao" :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. console.log(this.value);
  71. },
  72. computed: {
  73. },
  74. watch: {
  75. inputValue(number) {
  76. const data = {
  77. number: number,
  78. index: this.index
  79. }
  80. this.$emit('eventChange', data);
  81. }
  82. },
  83. methods: {
  84. _calcValue(type) {
  85. const scale = this._getDecimalScale();
  86. let value = this.inputValue * scale;
  87. let newValue = 0;
  88. let step = this.step * scale;
  89. if(type === 'subtract'){
  90. newValue = value - step;
  91. if (newValue <= this.min){
  92. this.minDisabled = true;
  93. }
  94. if(newValue < this.min){
  95. newValue = this.min
  96. }
  97. if(newValue < this.max && this.maxDisabled === true){
  98. this.maxDisabled = false;
  99. }
  100. }else if(type === 'add'){
  101. newValue = value + step;
  102. if (newValue >= this.max){
  103. this.maxDisabled = true;
  104. }
  105. if(newValue > this.max){
  106. newValue = this.max
  107. }
  108. if(newValue > this.min && this.minDisabled === true){
  109. this.minDisabled = false;
  110. }
  111. }
  112. if(newValue === value){
  113. return;
  114. }
  115. this.inputValue = newValue / scale;
  116. },
  117. _getDecimalScale() {
  118. let scale = 1;
  119. // 浮点型
  120. if (~~this.step !== this.step) {
  121. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  122. }
  123. return scale;
  124. },
  125. _onBlur(event) {
  126. let value = event.detail.value;
  127. if (!value) {
  128. this.inputValue = 0;
  129. return
  130. }
  131. value = +value;
  132. if (value > this.max) {
  133. value = this.max;
  134. } else if (value < this.min) {
  135. value = this.min
  136. }
  137. this.inputValue = value
  138. }
  139. }
  140. }
  141. </script>
  142. <style>
  143. .uni-numbox {
  144. position:absolute;
  145. left: 30upx;
  146. bottom: 0;
  147. display: flex;
  148. justify-content: flex-start;
  149. align-items: center;
  150. width:230upx;
  151. height: 44upx;
  152. background:#FFFFFF;
  153. }
  154. .uni-numbox-minus,
  155. .uni-numbox-plus {
  156. margin: 0;
  157. background-color: #FFFFFF;
  158. width: 70upx;
  159. height: 100%;
  160. line-height: 44upx;
  161. text-align: center;
  162. position: relative;
  163. }
  164. .uni-numbox-minus .iconfont,
  165. .uni-numbox-plus .iconfont{
  166. font-size: 36upx;
  167. color: #555;
  168. }
  169. .uni-numbox-minus {
  170. border-right: none;
  171. border-top-left-radius: 6upx;
  172. border-bottom-left-radius: 6upx;
  173. }
  174. .uni-numbox-plus {
  175. border-left: none;
  176. border-top-right-radius: 6upx;
  177. border-bottom-right-radius: 6upx;
  178. }
  179. .uni-numbox-value {
  180. position: relative;
  181. background-color: #f5f5f5;
  182. width: 90upx;
  183. height: 50upx;
  184. text-align: center;
  185. padding: 0;
  186. font-size: 30upx;
  187. }
  188. .uni-numbox-disabled.iconfont {
  189. color: #d6d6d6;
  190. }
  191. </style>