uni-number-box.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="uni-numbox">
  3. <view @click="_calcValue('minus')" class="uni-numbox__minus uni-numbox-btns" :style="{background}">
  4. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }"
  5. :style="{color}">-
  6. </text>
  7. </view>
  8. <input :disabled="disabled" @focus="_onFocus" @blur="_onBlur" class="uni-numbox__value" type="number"
  9. v-model="inputValue" :style="{background, color}"/>
  10. <view @click="_calcValue('plus')" class="uni-numbox__plus uni-numbox-btns" :style="{background}">
  11. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }"
  12. :style="{color}">+
  13. </text>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. /**
  19. * NumberBox 数字输入框
  20. * @description 带加减按钮的数字输入框
  21. * @tutorial https://ext.dcloud.net.cn/plugin?id=31
  22. * @property {Number} value 输入框当前值
  23. * @property {Number} min 最小值
  24. * @property {Number} max 最大值
  25. * @property {Number} step 每次点击改变的间隔大小
  26. * @property {String} background 背景色
  27. * @property {String} color 字体颜色(前景色)
  28. * @property {Boolean} disabled = [true|false] 是否为禁用状态
  29. * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
  30. * @event {Function} focus 输入框聚焦时触发的事件,参数为 event 对象
  31. * @event {Function} blur 输入框失焦时触发的事件,参数为 event 对象
  32. */
  33. export default {
  34. name: "UniNumberBox",
  35. emits: ['change', 'input', 'update:modelValue', 'blur', 'focus'],
  36. props: {
  37. value: {
  38. type: [Number, String],
  39. default: 1
  40. },
  41. modelValue: {
  42. type: [Number, String],
  43. default: 1
  44. },
  45. min: {
  46. type: Number,
  47. default: 0
  48. },
  49. max: {
  50. type: Number,
  51. default: 100
  52. },
  53. step: {
  54. type: Number,
  55. default: 1
  56. },
  57. background: {
  58. type: String,
  59. default: '#f5f5f5'
  60. },
  61. color: {
  62. type: String,
  63. default: '#333'
  64. },
  65. disabled: {
  66. type: Boolean,
  67. default: false
  68. }
  69. },
  70. data() {
  71. return {
  72. inputValue: 0
  73. };
  74. },
  75. watch: {
  76. value(val) {
  77. this.inputValue = +val;
  78. },
  79. modelValue(val) {
  80. this.inputValue = +val;
  81. }
  82. },
  83. created() {
  84. if (this.value === 1) {
  85. this.inputValue = +this.modelValue;
  86. }
  87. if (this.modelValue === 1) {
  88. this.inputValue = +this.value;
  89. }
  90. },
  91. methods: {
  92. _calcValue(type) {
  93. if (this.disabled) {
  94. return;
  95. }
  96. const scale = this._getDecimalScale();
  97. let value = this.inputValue * scale;
  98. let step = this.step * scale;
  99. if (type === "minus") {
  100. value -= step;
  101. if (value < (this.min * scale)) {
  102. return;
  103. }
  104. if (value > (this.max * scale)) {
  105. value = this.max * scale
  106. }
  107. }
  108. if (type === "plus") {
  109. value += step;
  110. if (value > (this.max * scale)) {
  111. return;
  112. }
  113. if (value < (this.min * scale)) {
  114. value = this.min * scale
  115. }
  116. }
  117. this.inputValue = (value / scale).toFixed(String(scale).length - 1);
  118. this.$emit("change", +this.inputValue);
  119. // TODO vue2 兼容
  120. this.$emit("input", +this.inputValue);
  121. // TODO vue3 兼容
  122. this.$emit("update:modelValue", +this.inputValue);
  123. },
  124. _getDecimalScale() {
  125. let scale = 1;
  126. // 浮点型
  127. if (~~this.step !== this.step) {
  128. scale = Math.pow(10, String(this.step).split(".")[1].length);
  129. }
  130. return scale;
  131. },
  132. _onBlur(event) {
  133. this.$emit('blur', event)
  134. let value = event.detail.value;
  135. if (!value) {
  136. // this.inputValue = 0;
  137. return;
  138. }
  139. value = +value;
  140. if (value > this.max) {
  141. value = this.max;
  142. } else if (value < this.min) {
  143. value = this.min;
  144. }
  145. const scale = this._getDecimalScale();
  146. this.inputValue = value.toFixed(String(scale).length - 1);
  147. this.$emit("change", +this.inputValue);
  148. this.$emit("input", +this.inputValue);
  149. },
  150. _onFocus(event) {
  151. this.$emit('focus', event)
  152. }
  153. }
  154. };
  155. </script>
  156. <style lang="scss">
  157. $box-height: 26px;
  158. $bg: #f5f5f5;
  159. $br: 2px;
  160. $color: #333;
  161. .uni-numbox {
  162. /* #ifndef APP-NVUE */
  163. display: flex;
  164. /* #endif */
  165. flex-direction: row;
  166. }
  167. .uni-numbox-btns {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. flex-direction: row;
  172. align-items: center;
  173. justify-content: center;
  174. padding: 0 8px;
  175. background-color: $bg;
  176. /* #ifdef H5 */
  177. cursor: pointer;
  178. /* #endif */
  179. }
  180. .uni-numbox__value {
  181. margin: 0 2px;
  182. background-color: $bg;
  183. width: 40px;
  184. height: $box-height;
  185. text-align: center;
  186. font-size: 14px;
  187. border-left-width: 0;
  188. border-right-width: 0;
  189. color: $color;
  190. }
  191. .uni-numbox__minus {
  192. border-top-left-radius: $br;
  193. border-bottom-left-radius: $br;
  194. }
  195. .uni-numbox__plus {
  196. border-top-right-radius: $br;
  197. border-bottom-right-radius: $br;
  198. }
  199. .uni-numbox--text {
  200. // fix nvue
  201. line-height: 20px;
  202. font-size: 20px;
  203. font-weight: 300;
  204. color: $color;
  205. }
  206. .uni-numbox .uni-numbox--disabled {
  207. color: #c0c0c0 !important;
  208. /* #ifdef H5 */
  209. cursor: not-allowed;
  210. /* #endif */
  211. }
  212. </style>