uni-number-box.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 scoped>
  119. /* #ifdef APP-NVUE */
  120. /* #endif */
  121. .uni-numbox {
  122. /* #ifndef APP-NVUE */
  123. display: flex;
  124. /* #endif */
  125. flex-direction: row;
  126. height: 35px;
  127. line-height: 35px;
  128. width: 120px;
  129. }
  130. .uni-numbox__value {
  131. background-color: #ffffff;
  132. width: 40px;
  133. height: 35px;
  134. text-align: center;
  135. font-size: 16;
  136. border-width: 1rpx;
  137. border-style: solid;
  138. border-color: #e5e5e5;
  139. border-left-width: 0;
  140. border-right-width: 0;
  141. }
  142. .uni-numbox__minus {
  143. /* #ifndef APP-NVUE */
  144. display: flex;
  145. /* #endif */
  146. flex-direction: row;
  147. align-items: center;
  148. justify-content: center;
  149. width: 35px;
  150. height: 35px;
  151. /* line-height: $box-line-height;
  152. */
  153. /* text-align: center;
  154. */
  155. font-size: 20px;
  156. color: #333;
  157. background-color: #f8f8f8;
  158. border-width: 1rpx;
  159. border-style: solid;
  160. border-color: #e5e5e5;
  161. border-top-left-radius: 3px;
  162. border-bottom-left-radius: 3px;
  163. border-right-width: 0;
  164. }
  165. .uni-numbox__plus {
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. flex-direction: row;
  170. align-items: center;
  171. justify-content: center;
  172. width: 35px;
  173. height: 35px;
  174. border-width: 1rpx;
  175. border-style: solid;
  176. border-color: #e5e5e5;
  177. border-top-right-radius: 3px;
  178. border-bottom-right-radius: 3px;
  179. background-color: #f8f8f8;
  180. border-left-width: 0;
  181. }
  182. .uni-numbox--text {
  183. font-size: 40rpx;
  184. color: #333;
  185. }
  186. .uni-numbox--disabled {
  187. color: #c0c0c0;
  188. }
  189. </style>