uni-number-box.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /**
  14. * NumberBox 数字输入框
  15. * @description 带加减按钮的数字输入框
  16. * @tutorial https://ext.dcloud.net.cn/plugin?id=31
  17. * @property {Number} value 输入框当前值
  18. * @property {Number} min 最小值
  19. * @property {Number} max 最大值
  20. * @property {Number} step 每次点击改变的间隔大小
  21. * @property {Boolean} disabled = [true|false] 是否为禁用状态
  22. * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
  23. */
  24. export default {
  25. name: "UniNumberBox",
  26. props: {
  27. value: {
  28. type: [Number, String],
  29. default: 1
  30. },
  31. min: {
  32. type: Number,
  33. default: 0
  34. },
  35. max: {
  36. type: Number,
  37. default: 100
  38. },
  39. step: {
  40. type: Number,
  41. default: 1
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false
  46. }
  47. },
  48. data() {
  49. return {
  50. inputValue: 0
  51. };
  52. },
  53. watch: {
  54. value(val) {
  55. this.inputValue = +val;
  56. },
  57. inputValue(newVal, oldVal) {
  58. if (+newVal !== +oldVal) {
  59. this.$emit("change", newVal);
  60. }
  61. }
  62. },
  63. created() {
  64. this.inputValue = +this.value;
  65. },
  66. methods: {
  67. _calcValue(type) {
  68. if (this.disabled) {
  69. return;
  70. }
  71. const scale = this._getDecimalScale();
  72. let value = this.inputValue * scale;
  73. let step = this.step * scale;
  74. if (type === "minus") {
  75. value -= step;
  76. if (value < (this.min * scale)) {
  77. return;
  78. }
  79. if (value > (this.max * scale)) {
  80. value = this.max * scale
  81. }
  82. } else if (type === "plus") {
  83. value += step;
  84. if (value > (this.max * scale)) {
  85. return;
  86. }
  87. if (value < (this.min * scale)) {
  88. value = this.min * scale
  89. }
  90. }
  91. this.inputValue = String(value / scale);
  92. },
  93. _getDecimalScale() {
  94. let scale = 1;
  95. // 浮点型
  96. if (~~this.step !== this.step) {
  97. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  98. }
  99. return scale;
  100. },
  101. _onBlur(event) {
  102. let value = event.detail.value;
  103. if (!value) {
  104. // this.inputValue = 0;
  105. return;
  106. }
  107. value = +value;
  108. if (value > this.max) {
  109. value = this.max;
  110. } else if (value < this.min) {
  111. value = this.min;
  112. }
  113. this.inputValue = value;
  114. }
  115. }
  116. };
  117. </script>
  118. <style lang="scss" scoped>
  119. $box-height: 35px;
  120. /* #ifdef APP-NVUE */
  121. $box-line-height: 35px;
  122. /* #endif */
  123. $box-line-height: 26px;
  124. $box-width: 35px;
  125. .uni-numbox {
  126. /* #ifndef APP-NVUE */
  127. display: flex;
  128. /* #endif */
  129. flex-direction: row;
  130. height: $box-height;
  131. line-height: $box-height;
  132. width: 120px;
  133. }
  134. .uni-numbox__value {
  135. background-color: $uni-bg-color;
  136. width: 40px;
  137. height: $box-height;
  138. text-align: center;
  139. font-size: $uni-font-size-lg;
  140. border-width: 1rpx;
  141. border-style: solid;
  142. border-color: $uni-border-color;
  143. border-left-width: 0;
  144. border-right-width: 0;
  145. }
  146. .uni-numbox__minus {
  147. /* #ifndef APP-NVUE */
  148. display: flex;
  149. /* #endif */
  150. flex-direction: row;
  151. align-items: center;
  152. justify-content: center;
  153. width: $box-width;
  154. height: $box-height;
  155. // line-height: $box-line-height;
  156. // text-align: center;
  157. font-size: 20px;
  158. color: $uni-text-color;
  159. background-color: $uni-bg-color-grey;
  160. border-width: 1rpx;
  161. border-style: solid;
  162. border-color: $uni-border-color;
  163. border-top-left-radius: $uni-border-radius-base;
  164. border-bottom-left-radius: $uni-border-radius-base;
  165. border-right-width: 0;
  166. }
  167. .uni-numbox__plus {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. flex-direction: row;
  172. align-items: center;
  173. justify-content: center;
  174. width: $box-width;
  175. height: $box-height;
  176. border-width: 1rpx;
  177. border-style: solid;
  178. border-color: $uni-border-color;
  179. border-top-right-radius: $uni-border-radius-base;
  180. border-bottom-right-radius: $uni-border-radius-base;
  181. background-color: $uni-bg-color-grey;
  182. border-left-width: 0;
  183. }
  184. .uni-numbox--text {
  185. font-size: 40rpx;
  186. color: $uni-text-color;
  187. }
  188. .uni-numbox--disabled {
  189. color: $uni-text-color-disable;
  190. }
  191. </style>