uni-number-box.vue 3.9 KB

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