uni-number-box.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div class="uni-numbox">
  3. <div @click="_calcValue('minus')" class="uni-numbox__minus">
  4. <span class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</span>
  5. </div>
  6. <input :disabled="inputDisabled" @blur="_onBlur" :adjust-position='false' class="uni-numbox__value" type="number" v-model="inputValue" />
  7. <div @click="_calcValue('plus')" class="uni-numbox__plus">
  8. <span class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">+</span>
  9. </div>
  10. </div>
  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. inputDisabled: {
  48. type: Boolean,
  49. default: true
  50. }
  51. },
  52. data() {
  53. return {
  54. inputValue: 0
  55. };
  56. },
  57. watch: {
  58. value(val) {
  59. this.inputValue = val;
  60. },
  61. inputValue(newVal, oldVal) {
  62. if (newVal !== oldVal) {
  63. this.$emit("input", newVal);
  64. }
  65. }
  66. },
  67. created() {
  68. this.inputValue = this.value;
  69. },
  70. methods: {
  71. _calcValue(type) {
  72. if (this.disabled) {
  73. return;
  74. }
  75. const scale = this._getDecimalScale();
  76. let value = this.inputValue * scale;
  77. let step = this.step * scale;
  78. if (type === "minus") {
  79. value -= step;
  80. if (value < (this.min * scale)) {
  81. return;
  82. }
  83. if (value > (this.max * scale)) {
  84. value = this.max * scale
  85. }
  86. } else if (type === "plus") {
  87. value += step;
  88. if (value > (this.max * scale)) {
  89. return;
  90. }
  91. if (value < (this.min * scale)) {
  92. value = this.min * scale
  93. }
  94. }
  95. this.inputValue = String(value / scale);
  96. },
  97. _getDecimalScale() {
  98. let scale = 1;
  99. // 浮点型
  100. if (~~this.step !== this.step) {
  101. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  102. }
  103. return scale;
  104. },
  105. _onBlur(event) {
  106. let value = event.detail.value;
  107. if (!value) {
  108. // this.inputValue = 0;
  109. return;
  110. }
  111. value = +value;
  112. if (value > this.max) {
  113. value = this.max;
  114. } else if (value < this.min) {
  115. value = this.min;
  116. }
  117. this.inputValue = value;
  118. }
  119. }
  120. };
  121. </script>
  122. <style scoped>
  123. /* #ifdef APP-NVUE */
  124. /* #endif */
  125. .uni-numbox {
  126. /* #ifndef APP-NVUE */
  127. display: flex;
  128. /* #endif */
  129. flex-direction: row;
  130. height: 35px;
  131. line-height: 35px;
  132. width: 120px;
  133. }
  134. .uni-numbox__value {
  135. background-color: #ffffff;
  136. width: 40px;
  137. height: 33px;
  138. text-align: center;
  139. font-size: 32px;
  140. border-width: 1px;
  141. border-style: solid;
  142. border-color: #e5e5e5;
  143. border-left-width: 0;
  144. border-right-width: 0;
  145. }
  146. .uni-numbox__minus {
  147. /* #ifndef APP-NVUE */
  148. display: flex;
  149. /* #endif */
  150. flex-direction: row;
  151. align-items: center;
  152. justify-content: center;
  153. width: 35px;
  154. height: 35px;
  155. /* line-height: $box-line-height;
  156. */
  157. /* text-align: center;
  158. */
  159. font-size: 20px;
  160. color: #333;
  161. background-color: #f8f8f8;
  162. border-width: 1px;
  163. border-style: solid;
  164. border-color: #e5e5e5;
  165. border-top-left-radius: 6px;
  166. border-bottom-left-radius: 6px;
  167. border-right-width: 0;
  168. }
  169. .uni-numbox__plus {
  170. /* #ifndef APP-NVUE */
  171. display: flex;
  172. /* #endif */
  173. flex-direction: row;
  174. align-items: center;
  175. justify-content: center;
  176. width: 35px;
  177. height: 35px;
  178. border-width: 1px;
  179. border-style: solid;
  180. border-color: #e5e5e5;
  181. border-top-right-radius: 6px;
  182. border-bottom-right-radius: 6px;
  183. background-color: #f8f8f8;
  184. border-left-width: 0;
  185. }
  186. .uni-numbox--text {
  187. font-size: 40px;
  188. color: #333;
  189. }
  190. .uni-numbox--disabled {
  191. color: #c0c0c0;
  192. }
  193. </style>