uni-number-box.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <view class="uni-numbox">
  3. <view @click="_calcValue('minus')" class="uni-numbox__minus uni-cursor-point">
  4. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</text>
  5. </view>
  6. <input :disabled="disabled" @focus="_onFocus" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue"/>
  7. <view @click="_calcValue('plus')" class="uni-numbox__plus uni-cursor-point">
  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. emits:['change','input','update:modelValue','blur','focus'],
  27. props: {
  28. value: {
  29. type: [Number, String],
  30. default: 1
  31. },
  32. modelValue:{
  33. type: [Number, String],
  34. default: 1
  35. },
  36. min: {
  37. type: Number,
  38. default: 0
  39. },
  40. max: {
  41. type: Number,
  42. default: 100
  43. },
  44. step: {
  45. type: Number,
  46. default: 1
  47. },
  48. disabled: {
  49. type: Boolean,
  50. default: false
  51. }
  52. },
  53. data() {
  54. return {
  55. inputValue: 0
  56. };
  57. },
  58. watch: {
  59. value(val) {
  60. this.inputValue = +val;
  61. },
  62. modelValue(val){
  63. this.inputValue = +val;
  64. }
  65. },
  66. created() {
  67. if(this.value === 1){
  68. this.inputValue = +this.modelValue;
  69. }
  70. if(this.modelValue === 1){
  71. this.inputValue = +this.value;
  72. }
  73. },
  74. methods: {
  75. _calcValue(type) {
  76. if (this.disabled) {
  77. return;
  78. }
  79. const scale = this._getDecimalScale();
  80. let value = this.inputValue * scale;
  81. let step = this.step * scale;
  82. if (type === "minus") {
  83. value -= step;
  84. if (value < (this.min * scale)) {
  85. return;
  86. }
  87. if (value > (this.max * scale)) {
  88. value = this.max * scale
  89. }
  90. }
  91. if (type === "plus") {
  92. value += step;
  93. if (value > (this.max * scale)) {
  94. return;
  95. }
  96. if (value < (this.min * scale)) {
  97. value = this.min * scale
  98. }
  99. }
  100. this.inputValue = (value / scale).toFixed(String(scale).length - 1);
  101. this.$emit("change", +this.inputValue);
  102. // TODO vue2 兼容
  103. this.$emit("input", +this.inputValue);
  104. // TODO vue3 兼容
  105. this.$emit("update:modelValue", +this.inputValue);
  106. },
  107. _getDecimalScale() {
  108. let scale = 1;
  109. // 浮点型
  110. if (~~this.step !== this.step) {
  111. scale = Math.pow(10, String(this.step).split(".")[1].length);
  112. }
  113. return scale;
  114. },
  115. _onBlur(event) {
  116. this.$emit('blur', event)
  117. let value = event.detail.value;
  118. if (!value) {
  119. // this.inputValue = 0;
  120. return;
  121. }
  122. value = +value;
  123. if (value > this.max) {
  124. value = this.max;
  125. } else if (value < this.min) {
  126. value = this.min;
  127. }
  128. const scale = this._getDecimalScale();
  129. this.inputValue = value.toFixed(String(scale).length - 1);
  130. this.$emit("change", +this.inputValue);
  131. this.$emit("input", +this.inputValue);
  132. },
  133. _onFocus(event) {
  134. this.$emit('focus', event)
  135. }
  136. }
  137. };
  138. </script>
  139. <style lang="scss" scoped>
  140. $box-height: 35px;
  141. /* #ifdef APP-NVUE */
  142. $box-line-height: 35px;
  143. /* #endif */
  144. $box-line-height: 26px;
  145. $box-width: 35px;
  146. .uni-numbox {
  147. /* #ifndef APP-NVUE */
  148. display: flex;
  149. /* #endif */
  150. flex-direction: row;
  151. height: $box-height;
  152. line-height: $box-height;
  153. width: 120px;
  154. }
  155. .uni-cursor-point {
  156. /* #ifdef H5 */
  157. cursor: pointer;
  158. /* #endif */
  159. }
  160. .uni-numbox__value {
  161. background-color: $uni-bg-color;
  162. width: 50px;
  163. height: $box-height;
  164. text-align: center;
  165. font-size: $uni-font-size-lg;
  166. border-width: 1rpx;
  167. border-style: solid;
  168. border-color: $uni-border-color;
  169. border-left-width: 0;
  170. border-right-width: 0;
  171. }
  172. .uni-numbox__minus {
  173. /* #ifndef APP-NVUE */
  174. display: flex;
  175. /* #endif */
  176. flex-direction: row;
  177. align-items: center;
  178. justify-content: center;
  179. width: $box-width;
  180. height: $box-height;
  181. // line-height: $box-line-height;
  182. // text-align: center;
  183. font-size: 20px;
  184. color: $uni-text-color;
  185. background-color: $uni-bg-color-grey;
  186. border-width: 1rpx;
  187. border-style: solid;
  188. border-color: $uni-border-color;
  189. border-top-left-radius: $uni-border-radius-base;
  190. border-bottom-left-radius: $uni-border-radius-base;
  191. border-right-width: 0;
  192. }
  193. .uni-numbox__plus {
  194. /* #ifndef APP-NVUE */
  195. display: flex;
  196. /* #endif */
  197. flex-direction: row;
  198. align-items: center;
  199. justify-content: center;
  200. width: $box-width;
  201. height: $box-height;
  202. border-width: 1rpx;
  203. border-style: solid;
  204. border-color: $uni-border-color;
  205. border-top-right-radius: $uni-border-radius-base;
  206. border-bottom-right-radius: $uni-border-radius-base;
  207. background-color: $uni-bg-color-grey;
  208. border-left-width: 0;
  209. }
  210. .uni-numbox--text {
  211. font-size: 20px;
  212. color: $uni-text-color;
  213. }
  214. .uni-numbox--disabled {
  215. color: $uni-text-color-disable;
  216. /* #ifdef H5 */
  217. cursor: not-allowed;
  218. /* #endif */
  219. }
  220. </style>