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