uni-number-box.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="uni-numbox">
  3. <view @click="_calcValue('minus')" class="uni-numbox__minus">
  4. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</text>
  5. </view>
  6. <input :disabled="disabled" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
  7. <view @click="_calcValue('plus')" class="uni-numbox__plus">
  8. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">+</text>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: "UniNumberBox",
  15. props: {
  16. value: {
  17. type: [Number, String],
  18. default: 1
  19. },
  20. min: {
  21. type: Number,
  22. default: 0
  23. },
  24. max: {
  25. type: Number,
  26. default: 100
  27. },
  28. step: {
  29. type: Number,
  30. default: 1
  31. },
  32. disabled: {
  33. type: Boolean,
  34. default: false
  35. }
  36. },
  37. data() {
  38. return {
  39. inputValue: 0
  40. };
  41. },
  42. watch: {
  43. value(val) {
  44. this.inputValue = +val;
  45. },
  46. inputValue(newVal, oldVal) {
  47. if (+newVal !== +oldVal) {
  48. this.$emit("change", newVal);
  49. }
  50. }
  51. },
  52. created() {
  53. this.inputValue = +this.value;
  54. },
  55. methods: {
  56. _calcValue(type) {
  57. if (this.disabled) {
  58. return;
  59. }
  60. const scale = this._getDecimalScale();
  61. let value = this.inputValue * scale;
  62. let step = this.step * scale;
  63. if (type === "minus") {
  64. value -= step;
  65. if (value < this.min) {
  66. return;
  67. }
  68. if(value > this.max){
  69. value = this.max
  70. }
  71. } else if (type === "plus") {
  72. value += step;
  73. if (value > this.max) {
  74. return;
  75. }
  76. if(value < this.min){
  77. value = this.min
  78. }
  79. }
  80. this.inputValue = String(value / scale);
  81. },
  82. _getDecimalScale() {
  83. let scale = 1;
  84. // 浮点型
  85. if (~~this.step !== this.step) {
  86. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  87. }
  88. return scale;
  89. },
  90. _onBlur(event) {
  91. let value = event.detail.value;
  92. if (!value) {
  93. // this.inputValue = 0;
  94. return;
  95. }
  96. value = +value;
  97. if (value > this.max) {
  98. value = this.max;
  99. } else if (value < this.min) {
  100. value = this.min;
  101. }
  102. this.inputValue = value;
  103. }
  104. }
  105. };
  106. </script>
  107. <style lang="scss" scoped>
  108. $box-height: 35px;
  109. /* #ifdef APP-NVUE */
  110. $box-line-height: 35px;
  111. /* #endif */
  112. $box-line-height: 26px;
  113. $box-width: 35px;
  114. .uni-numbox {
  115. /* #ifndef APP-NVUE */
  116. display: flex;
  117. /* #endif */
  118. flex-direction: row;
  119. height: $box-height;
  120. line-height: $box-height;
  121. width: 120px;
  122. }
  123. .uni-numbox__value {
  124. background-color: $uni-bg-color;
  125. width: 40px;
  126. height: $box-height;
  127. text-align: center;
  128. font-size: $uni-font-size-lg;
  129. border-width: 1rpx;
  130. border-style: solid;
  131. border-color: $uni-border-color;
  132. border-left-width: 0;
  133. border-right-width: 0;
  134. }
  135. .uni-numbox__minus {
  136. /* #ifndef APP-NVUE */
  137. display: flex;
  138. /* #endif */
  139. flex-direction: row;
  140. align-items: center;
  141. justify-content: center;
  142. width: $box-width;
  143. height: $box-height;
  144. // line-height: $box-line-height;
  145. // text-align: center;
  146. font-size: 20px;
  147. color: $uni-text-color;
  148. background-color: $uni-bg-color-grey;
  149. border-width: 1rpx;
  150. border-style: solid;
  151. border-color: $uni-border-color;
  152. border-top-left-radius: $uni-border-radius-base;
  153. border-bottom-left-radius: $uni-border-radius-base;
  154. border-right-width: 0;
  155. }
  156. .uni-numbox__plus {
  157. /* #ifndef APP-NVUE */
  158. display: flex;
  159. /* #endif */
  160. flex-direction: row;
  161. align-items: center;
  162. justify-content: center;
  163. width: $box-width;
  164. height: $box-height;
  165. border-width: 1rpx;
  166. border-style: solid;
  167. border-color: $uni-border-color;
  168. border-top-right-radius: $uni-border-radius-base;
  169. border-bottom-right-radius: $uni-border-radius-base;
  170. background-color: $uni-bg-color-grey;
  171. border-left-width: 0;
  172. }
  173. .uni-numbox--text {
  174. font-size: 40rpx;
  175. color: $uni-text-color;
  176. }
  177. .uni-numbox--disabled {
  178. color: $uni-text-color-disable;
  179. }
  180. </style>