You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
3.3 KiB

3 years ago
  1. <template>
  2. <view class="uni-numbox">
  3. <view class="uni-numbox-minus"
  4. @click="_calcValue('subtract')"
  5. >
  6. <text class="mix-icon icon--jianhao" :class="minDisabled?'uni-numbox-disabled': ''" ></text>
  7. </view>
  8. <input
  9. class="uni-numbox-value"
  10. type="number"
  11. :disabled="inputDisabled"
  12. :value="inputValue"
  13. @blur="_onBlur"
  14. >
  15. <view
  16. class="uni-numbox-plus"
  17. @click="_calcValue('add')"
  18. >
  19. <text class="mix-icon icon-jia2" :class="maxDisabled?'uni-numbox-disabled': ''" ></text>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. /**
  25. * index 当前行下标
  26. * value 默认值
  27. * min 最小值
  28. * max 最大值
  29. * step 步进值
  30. * disabled 禁用
  31. */
  32. export default {
  33. name: 'uni-number-box',
  34. props: {
  35. index: {
  36. type: Number,
  37. default: 0
  38. },
  39. value: {
  40. type: Number,
  41. default: 1
  42. },
  43. min: {
  44. type: Number,
  45. default: -Infinity
  46. },
  47. max: {
  48. type: Number,
  49. default: 99
  50. },
  51. step: {
  52. type: Number,
  53. default: 1
  54. },
  55. inputDisabled: {
  56. type: Boolean,
  57. default: false
  58. }
  59. },
  60. data() {
  61. return {
  62. inputValue: this.value,
  63. }
  64. },
  65. created(){
  66. },
  67. computed: {
  68. maxDisabled(){
  69. return this.inputValue >= this.max;
  70. },
  71. minDisabled(){
  72. return this.inputValue <= this.min;
  73. },
  74. },
  75. watch: {
  76. inputValue(number) {
  77. const data = {
  78. number: number,
  79. index: this.index
  80. }
  81. this.$emit('eventChange', data);
  82. },
  83. },
  84. methods: {
  85. _calcValue(type) {
  86. let value = this.inputValue;
  87. let newValue = 0;
  88. let step = this.step;
  89. if(type === 'subtract'){
  90. newValue = value - step;
  91. if(newValue < this.min){
  92. newValue = this.min
  93. if(this.min > 1){
  94. this.$api.msg(this.limit_message);
  95. }
  96. }
  97. }else if(type === 'add'){
  98. newValue = value + step;
  99. if(newValue > this.max){
  100. newValue = this.max
  101. }
  102. }
  103. if(newValue === value){
  104. return;
  105. }
  106. this.inputValue = newValue;
  107. },
  108. _onBlur(event) {
  109. let value = event.detail.value;
  110. let constValue = value;
  111. if (!value) {
  112. this.inputValue = 0;
  113. return
  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(constValue != value){
  122. this.inputValue = constValue;
  123. this.$nextTick(()=>{
  124. this.inputValue = value
  125. })
  126. }else{
  127. this.inputValue = value
  128. }
  129. }
  130. }
  131. }
  132. </script>
  133. <style>
  134. .uni-numbox {
  135. display: flex;
  136. justify-content: flex-start;
  137. flex-direction: row;
  138. align-items: center;
  139. height: 50rpx;
  140. }
  141. .uni-numbox-minus,
  142. .uni-numbox-plus {
  143. display: flex;
  144. align-items: center;
  145. justify-content: center;
  146. width: 50rpx;
  147. height: 100%;
  148. line-height: 1;
  149. background-color: #f7f7f7;
  150. }
  151. .uni-numbox-minus .mix-icon,
  152. .uni-numbox-plus .mix-icon{
  153. font-size: 32rpx;
  154. color: #333;
  155. }
  156. .uni-numbox-value {
  157. display: flex;
  158. align-items: center;
  159. justify-content: center;
  160. background-color: #fff;
  161. width: 60rpx;
  162. height: 50rpx;
  163. min-height: 50rpx;
  164. text-align: center;
  165. font-size: 28rpx;
  166. color: #333;
  167. }
  168. .uni-numbox-disabled.mix-icon {
  169. color: #C0C4CC;
  170. }
  171. </style>