uni-number-box.vue 5.2 KB

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