use-number-box.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="use-numbox pos-a dflex-c border-radius-big" :class="direction">
  3. <view class="use-numbox-minus pos-r tac h-full"
  4. @tap.stop="_calcValue('subtract')"
  5. >
  6. <text class="iconfont iconjian fs-sm" :class="minDisabled?'use-numbox-disabled': ''" ></text>
  7. </view>
  8. <input
  9. class="use-numbox-value pos-r tac"
  10. type="number"
  11. :disabled="disabled"
  12. :value="inputValue"
  13. @blur="_onBlur"
  14. >
  15. <view
  16. class="use-numbox-plus pos-r tac h-full"
  17. @tap.stop="_calcValue('add')"
  18. >
  19. <text class="iconfont iconjia fs-sm" :class="maxDisabled?'use-numbox-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: 0,
  66. minDisabled: false,
  67. maxDisabled: false
  68. }
  69. },
  70. created(){
  71. this.inputValue = this.value;
  72. this.maxDisabled = this.isMax;
  73. this.minDisabled = this.isMin;
  74. },
  75. computed: {
  76. },
  77. watch: {
  78. value(nVal, oVal) {
  79. this.inputValue = nVal;
  80. },
  81. inputValue(nVal, oVal) {
  82. }
  83. },
  84. methods: {
  85. _calcValue(type) {
  86. const scale = this._getDecimalScale();
  87. let value = this.inputValue * scale;
  88. let newValue = 0;
  89. let step = this.step * scale;
  90. if(type === 'subtract'){
  91. newValue = value - step;
  92. if (newValue <= this.min){
  93. this.minDisabled = true;
  94. }
  95. if(newValue < this.min){
  96. newValue = this.min
  97. }
  98. if(newValue < this.max && this.maxDisabled === true){
  99. this.maxDisabled = false;
  100. }
  101. } else if(type === 'add'){
  102. newValue = value + step;
  103. if (newValue >= this.max){
  104. this.maxDisabled = true;
  105. }
  106. if(newValue > this.max){
  107. newValue = this.max
  108. }
  109. if(newValue > this.min && this.minDisabled === true){
  110. this.minDisabled = false;
  111. }
  112. }
  113. if(newValue === value){
  114. return;
  115. }
  116. this.inputValue = newValue / scale;
  117. this.onChange();
  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. console.log('_onBlur', event);
  129. let value = event.detail.value;
  130. if (!value) {
  131. this.inputValue = 0;
  132. this.onChange();
  133. return
  134. }
  135. value = +value;
  136. if (value > this.max) {
  137. value = this.max;
  138. } else if (value < this.min) {
  139. value = this.min
  140. }
  141. this.inputValue = 0;
  142. this.$nextTick(() => {
  143. this.inputValue = value;
  144. this.onChange();
  145. })
  146. },
  147. onChange() {
  148. const data = {
  149. number: this.inputValue,
  150. index: this.index
  151. }
  152. this.$emit('eventChange', data);
  153. }
  154. }
  155. }
  156. </script>
  157. <style>
  158. .use-numbox {
  159. position:absolute;
  160. background:#f5f5f5;
  161. }
  162. .use-numbox.left{
  163. left: 30rpx;
  164. bottom: 0;
  165. }
  166. .use-numbox.right{
  167. width: 150px;
  168. right: 0;
  169. bottom: 10;
  170. }
  171. .use-numbox-minus,
  172. .use-numbox-plus {
  173. margin: 0;
  174. background-color: #f5f5f5;
  175. /* padding: 12rpx 20rpx; */
  176. }
  177. .use-numbox-minus .iconfont,
  178. .use-numbox-plus .iconfont {
  179. color: #555;
  180. font-weight: 700;
  181. }
  182. .use-numbox-minus {
  183. border-right: none;
  184. border-top-left-radius: 6upx;
  185. border-bottom-left-radius: 6upx;
  186. }
  187. .use-numbox-plus {
  188. border-left: none;
  189. border-top-right-radius: 6upx;
  190. border-bottom-right-radius: 6upx;
  191. }
  192. .use-numbox-value {
  193. background-color: #f5f5f5;
  194. /* width: 66rpx; */
  195. height: 50rpx;
  196. padding: 0;
  197. }
  198. .use-numbox-disabled.iconfont {
  199. color: #bbb;
  200. }
  201. </style>