uni-number-box.vue 5.6 KB

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