uni-number-box.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="uni-numbox" :style="{marginRight:margin_right*2+'rpx'}">
  3. <view @click="_calcValue('minus')" class="uni-numbox__minus">
  4. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</text>
  5. </view>
  6. <input :disabled="disabled" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
  7. <view @click="_calcValue('plus')" class="uni-numbox__plus">
  8. <text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">+</text>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: "UniNumberBox",
  15. props: {
  16. value: {
  17. type: [Number, String],
  18. default: 1
  19. },
  20. min: {
  21. type: Number,
  22. // default: 1
  23. },
  24. max: {
  25. type: Number,
  26. default: 100
  27. },
  28. step: {
  29. type: Number,
  30. default: 1
  31. },
  32. disabled: {
  33. type: Boolean,
  34. default: false
  35. },
  36. margin_right:{
  37. type:Number,
  38. default:0
  39. }
  40. },
  41. data() {
  42. return {
  43. inputValue: 1
  44. };
  45. },
  46. watch: {
  47. value(val) {
  48. this.inputValue = +val;
  49. },
  50. inputValue(newVal, oldVal) {
  51. if (+newVal !== +oldVal) {
  52. this.$emit("change", newVal);
  53. }
  54. }
  55. },
  56. created() {
  57. this.inputValue = +this.value;
  58. },
  59. methods: {
  60. _calcValue(type) {
  61. if (this.disabled) {
  62. return;
  63. }
  64. const scale = this._getDecimalScale();
  65. let value = this.inputValue * scale;
  66. let step = this.step * scale;
  67. if (type === "minus") {
  68. value -= step;
  69. if(value < 1 ){
  70. value = 1
  71. uni.showToast({
  72. title:'商品不能再减少了',
  73. icon:'none'
  74. })
  75. return
  76. }
  77. // if (value < this.min) {
  78. // return;
  79. // }
  80. // if(value > this.max){
  81. // value = this.max
  82. // }
  83. } else if (type === "plus") {
  84. value += step;
  85. if(this.hasLogin){ //已登录
  86. if (value > this.max) {
  87. value = this.max
  88. uni.showToast({
  89. title:'库存不足',
  90. icon:'none'
  91. })
  92. return;
  93. }
  94. if(value < this.min){
  95. value = this.min
  96. }
  97. }else{ //未登录
  98. }
  99. }
  100. this.inputValue = String(value / scale);
  101. },
  102. _getDecimalScale() {
  103. let scale = 1;
  104. // 浮点型
  105. if (~~this.step !== this.step) {
  106. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  107. }
  108. return scale;
  109. },
  110. _onBlur(event) {
  111. let value = event.detail.value;
  112. if (!value || value == '') {
  113. value = 1;
  114. }
  115. value = +value;
  116. if (value > this.max) {
  117. value = this.max;
  118. } else if (value < this.min) {
  119. value = this.min;
  120. }
  121. if(value < 1){
  122. value = 1
  123. }
  124. this.inputValue = value;
  125. }
  126. }
  127. };
  128. </script>
  129. <style lang="scss" scoped>
  130. $box-height: 35px;
  131. /* #ifdef APP-NVUE */
  132. $box-line-height: 35px;
  133. /* #endif */
  134. $box-line-height: 26px;
  135. $box-width: 35px;
  136. .uni-numbox {
  137. /* #ifndef APP-NVUE */
  138. display: flex;
  139. /* #endif */
  140. flex-direction: row;
  141. height: 50rpx;
  142. line-height: 50rpx;
  143. width: 182rpx;
  144. }
  145. .uni-numbox__value {
  146. background-color: $uni-bg-color;
  147. width: 78rpx;
  148. height: 50rpx;
  149. text-align: center;
  150. font-size: 24rpx;
  151. border-width: 1rpx;
  152. border-style: solid;
  153. border-color: rgba(0,0,0,.1);
  154. }
  155. .uni-numbox__minus {
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. flex-direction: row;
  160. align-items: center;
  161. justify-content: center;
  162. width: 50rpx;
  163. height: 50rpx;
  164. // line-height: $box-line-height;
  165. // text-align: center;
  166. font-size: 20px;
  167. color: $uni-text-color;
  168. background-color: #fff;
  169. border-width: 1rpx;
  170. border-style: solid;
  171. border-color: rgba(0,0,0,.1);
  172. border-top-left-radius: $uni-border-radius-base;
  173. border-bottom-left-radius: $uni-border-radius-base;
  174. border-right-width: 0;
  175. }
  176. .uni-numbox__plus {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. flex-direction: row;
  181. align-items: center;
  182. justify-content: center;
  183. width: 50rpx;
  184. height: 50rpx;
  185. border-width: 1rpx;
  186. border-style: solid;
  187. border-color: rgba(0,0,0,.1);
  188. border-top-right-radius: 6rpx;
  189. border-bottom-right-radius: 6rpx;
  190. background-color: #fff;
  191. border-left-width: 0;
  192. }
  193. .uni-numbox--text {
  194. font-size: 30rpx;
  195. color: #2D2D2D;
  196. }
  197. .uni-numbox--disabled {
  198. color: #949494;
  199. }
  200. .uni-input-input{
  201. font-size: 24rpx!important;
  202. color: #2D2D2D;
  203. }
  204. </style>