use-stepper.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view class="use-stepper border-radius-big" :class="direction">
  3. <view class="use-stepper-minus"
  4. @click="_calcValue('subtract')"
  5. >
  6. <text class="iconfont iconjian fs-sm" :class="minDisabled?'use-stepper-disabled': ''" ></text>
  7. </view>
  8. <input
  9. class="use-stepper-value"
  10. type="number"
  11. :disabled="disabled"
  12. :value="inputValue"
  13. @blur="_onBlur"
  14. >
  15. <view
  16. class="use-stepper-plus"
  17. @click="_calcValue('add')"
  18. >
  19. <text class="iconfont iconjia fs-sm" :class="maxDisabled?'use-stepper-disabled': ''" ></text>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. isMax: {
  27. type: Boolean,
  28. default: false
  29. },
  30. isMin: {
  31. type: Boolean,
  32. default: false
  33. },
  34. index: {
  35. type: Number,
  36. default: 0
  37. },
  38. value: {
  39. type: Number,
  40. default: 0
  41. },
  42. min: {
  43. type: Number,
  44. default: -Infinity
  45. },
  46. max: {
  47. type: Number,
  48. default: Infinity
  49. },
  50. step: {
  51. type: Number,
  52. default: 1
  53. },
  54. disabled: {
  55. type: Boolean,
  56. default: false
  57. },
  58. direction: {
  59. type: String,
  60. default: 'left'
  61. }
  62. },
  63. data() {
  64. return {
  65. inputValue: this.value,
  66. minDisabled: false,
  67. maxDisabled: false
  68. }
  69. },
  70. created(){
  71. this.maxDisabled = this.isMax;
  72. this.minDisabled = this.isMin;
  73. },
  74. computed: {
  75. },
  76. watch: {
  77. inputValue(number) {
  78. const data = {
  79. number: number,
  80. index: this.index
  81. }
  82. this.$emit('eventChange', data);
  83. }
  84. },
  85. methods: {
  86. _calcValue(type) {
  87. const scale = this._getDecimalScale();
  88. let value = this.inputValue * scale;
  89. let newValue = 0;
  90. let step = this.step * scale;
  91. if(type === 'subtract'){
  92. newValue = value - step;
  93. if (newValue <= this.min){
  94. this.minDisabled = true;
  95. }
  96. if(newValue < this.min){
  97. newValue = this.min
  98. }
  99. if(newValue < this.max && this.maxDisabled === true){
  100. this.maxDisabled = false;
  101. }
  102. }else if(type === 'add'){
  103. newValue = value + step;
  104. if (newValue >= this.max){
  105. this.maxDisabled = true;
  106. }
  107. if(newValue > this.max){
  108. newValue = this.max
  109. }
  110. if(newValue > this.min && this.minDisabled === true){
  111. this.minDisabled = false;
  112. }
  113. }
  114. if(newValue === value){
  115. return;
  116. }
  117. this.inputValue = newValue / scale;
  118. },
  119. _getDecimalScale() {
  120. let scale = 1;
  121. // 浮点型
  122. if (~~this.step !== this.step) {
  123. scale = Math.pow(10, (this.step + '').split('.')[1].length);
  124. }
  125. return scale;
  126. },
  127. _onBlur(event) {
  128. let value = event.detail.value;
  129. if (!value) {
  130. this.inputValue = 0;
  131. return
  132. }
  133. value = +value;
  134. if (value > this.max) {
  135. value = this.max;
  136. } else if (value < this.min) {
  137. value = this.min
  138. }
  139. this.inputValue = value
  140. }
  141. }
  142. }
  143. </script>
  144. <style>
  145. .use-stepper {
  146. position:absolute;
  147. display: flex;
  148. justify-content: flex-start;
  149. align-items: center;
  150. height: 70rpx;
  151. background:#f5f5f5;
  152. }
  153. .use-stepper.left{
  154. left: 30upx;
  155. bottom: 0;
  156. }
  157. .use-stepper.right{
  158. right: 0;
  159. bottom: 0;
  160. }
  161. .use-stepper-minus,
  162. .use-stepper-plus {
  163. margin: 0;
  164. background-color: #f5f5f5;
  165. width: 70rpx;
  166. height: 100%;
  167. line-height: 70rpx;
  168. text-align: center;
  169. position: relative;
  170. }
  171. .use-stepper-minus .iconfont,
  172. .use-stepper-plus .iconfont {
  173. color: #555;
  174. font-weight: 700;
  175. }
  176. .use-stepper-minus {
  177. border-right: none;
  178. border-top-left-radius: 6upx;
  179. border-bottom-left-radius: 6upx;
  180. }
  181. .use-stepper-plus {
  182. border-left: none;
  183. border-top-right-radius: 6upx;
  184. border-bottom-right-radius: 6upx;
  185. }
  186. .use-stepper-value {
  187. position: relative;
  188. background-color: #f5f5f5;
  189. width: 66rpx;
  190. height: 50rpx;
  191. text-align: center;
  192. padding: 0;
  193. }
  194. .use-stepper-disabled.yticon {
  195. color: #d6d6d6;
  196. }
  197. </style>