uni-number-box.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="uni-numbox" :class="{ small: size == 'small' }">
  3. <button type="default" class="decrease" :class="{ disabled: inputValue <= min || disabled, small: size == 'small' }" @click="_calcValue('minus')">-</button>
  4. <!-- <view
  5. class="uni-numbox__minus"
  6. > -->
  7. <!-- :style="'background-image:url(' + $util.img('/upload/uniapp/jian.png') + ')'" -->
  8. <!-- </view> -->
  9. <input :disabled="disabled" v-model="inputValue" class="uni-input uni-numbox__value" type="number" @input="_onInput" @blur="_onInput" :class="{ small: size == 'small' }" />
  10. <button type="default" class="increase" :class="{ disabled: inputValue >= max || disabled, small: size == 'small' }" @click="_calcValue('plus')">+</button>
  11. <!-- <view
  12. class="uni-numbox__plus" @click="_calcValue('plus')"
  13. :style="'background-image:url(' + $util.img('/upload/uniapp/jia.png') + ')'"
  14. >
  15. </view> -->
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'UniNumberBox',
  21. props: {
  22. value: {
  23. type: [Number, String],
  24. default: 1
  25. },
  26. min: {
  27. type: Number,
  28. default: 0
  29. },
  30. max: {
  31. type: Number,
  32. default: 100
  33. },
  34. step: {
  35. type: Number,
  36. default: 1
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. modifyFlag: {
  43. type: Boolean,
  44. default: false
  45. },
  46. size: {
  47. type: String,
  48. default: 'default'
  49. },
  50. index: {
  51. type: Number,
  52. default: -1
  53. }
  54. },
  55. data() {
  56. return {
  57. inputValue: 0
  58. };
  59. },
  60. watch: {
  61. value(val) {
  62. this.inputValue = +val;
  63. },
  64. inputValue(newVal, oldVal, params) {
  65. if (+newVal !== +oldVal && !this.modifyFlag) {
  66. this.$emit('change', newVal, params);
  67. }
  68. }
  69. },
  70. created() {
  71. this.inputValue = +this.value;
  72. },
  73. methods: {
  74. _calcValue(type) {
  75. if (this.disabled) {
  76. return;
  77. }
  78. if (this.modifyFlag) return;
  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. } else if (type === 'plus') {
  85. value += step;
  86. }
  87. if (value < this.min || value > this.max) {
  88. this.$emit('limit', { value: this.inputValue, type }, this.index);
  89. return;
  90. }
  91. this.inputValue = value / scale;
  92. },
  93. _getDecimalScale() {
  94. let scale = 1;
  95. // 浮点型
  96. if (~~this.step !== this.step) {
  97. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  98. }
  99. return scale;
  100. },
  101. _onInput(event) {
  102. setTimeout(() => {
  103. let value = event.detail.value;
  104. if (!/(^[1-9]\d*$)/.test(value)) value = this.min;
  105. if (!value) {
  106. this.inputValue = 0;
  107. return;
  108. }
  109. value = +value;
  110. if (value > this.max) {
  111. value = this.max;
  112. } else if (value < this.min) {
  113. value = this.min;
  114. }
  115. this.inputValue = value;
  116. }, 0);
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="scss">
  122. @charset "UTF-8";
  123. .uni-numbox {
  124. display: inline-flex;
  125. flex-direction: row;
  126. justify-content: flex-start;
  127. align-items: center;
  128. height: 70rpx;
  129. position: relative;
  130. }
  131. .uni-numbox.small {
  132. height: 44rpx;
  133. }
  134. .uni-numbox:after {
  135. content: '';
  136. position: absolute;
  137. transform-origin: center;
  138. box-sizing: border-box;
  139. pointer-events: none;
  140. top: -50%;
  141. left: -50%;
  142. right: -50%;
  143. bottom: -50%;
  144. border-radius: 12rpx;
  145. transform: scale(0.5);
  146. }
  147. .uni-numbox__minus,
  148. .uni-numbox__plus {
  149. width: 40rpx;
  150. height: 40rpx;
  151. border-radius: 50%;
  152. background-size: 100% 100%;
  153. background-position: center;
  154. }
  155. .uni-numbox__value {
  156. position: relative;
  157. background-color: $color-bg;
  158. width: 80rpx;
  159. height: 40rpx;
  160. text-align: center;
  161. border: 1px solid $color-line;
  162. display: inline-block;
  163. line-height: 36rpx;
  164. font-weight: bold;
  165. margin: 0;
  166. padding: 0;
  167. vertical-align: top;
  168. min-height: initial;
  169. border-left: none;
  170. border-right: none;
  171. }
  172. .uni-numbox__value.small {
  173. width: 60rpx;
  174. font-size: $font-size-tag;
  175. }
  176. .uni-numbox__value:after {
  177. content: '';
  178. position: absolute;
  179. transform-origin: center;
  180. box-sizing: border-box;
  181. pointer-events: none;
  182. top: -50%;
  183. left: -50%;
  184. right: -50%;
  185. bottom: -50%;
  186. border-top-width: 0;
  187. border-bottom-width: 0;
  188. transform: scale(0.5);
  189. }
  190. .uni-numbox--disabled {
  191. color: silver;
  192. }
  193. .uni-numbox button {
  194. width: 40rpx;
  195. height: 40rpx;
  196. display: inline-block;
  197. box-sizing: content-box;
  198. border: 1px solid $color-line;
  199. padding: 0;
  200. margin: 0;
  201. border-radius: 0;
  202. background-color: #fff !important;
  203. font-weight: bold;
  204. &.disabled {
  205. color: $color-line;
  206. }
  207. &.decrease {
  208. font-size: 44rpx;
  209. line-height: 32rpx;
  210. }
  211. &.increase {
  212. font-size: $font-size-toolbar;
  213. line-height: 36rpx;
  214. }
  215. }
  216. </style>