NumInput.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <q-input
  3. v-model="filteredValue"
  4. outlined dense
  5. input-style="text-align: center"
  6. class="no-mp"
  7. :class="(error ? 'error' : '')"
  8. :disable="disable"
  9. >
  10. <slot></slot>
  11. <template #prepend>
  12. <q-icon
  13. v-ripple="validate(modelValue - step)"
  14. :class="(validate(modelValue - step) ? '' : 'disable')"
  15. :name="minusIcon"
  16. class="button"
  17. @click="minus"
  18. @mousedown.prevent.stop="onMouseDown($event, 'minus')"
  19. @mouseup.prevent.stop="onMouseUp"
  20. @mouseout.prevent.stop="onMouseUp"
  21. @touchstart.stop="onTouchStart($event, 'minus')"
  22. @touchend.stop="onTouchEnd"
  23. @touchcancel.prevent.stop="onTouchEnd"
  24. />
  25. </template>
  26. <template #append>
  27. <q-icon
  28. v-ripple="validate(modelValue + step)"
  29. :class="(validate(modelValue + step) ? '' : 'disable')"
  30. :name="plusIcon"
  31. class="button"
  32. @click="plus"
  33. @mousedown.prevent.stop="onMouseDown($event, 'plus')"
  34. @mouseup.prevent.stop="onMouseUp"
  35. @mouseout.prevent.stop="onMouseUp"
  36. @touchstart.stop="onTouchStart($event, 'plus')"
  37. @touchend.stop="onTouchEnd"
  38. @touchcancel.prevent.stop="onTouchEnd"
  39. />
  40. </template>
  41. </q-input>
  42. </template>
  43. <script>
  44. //-----------------------------------------------------------------------------
  45. import vueComponent from '../vueComponent.js';
  46. import * as utils from '../../share/utils';
  47. const componentOptions = {
  48. watch: {
  49. filteredValue: function(newValue) {
  50. if (this.validate(newValue)) {
  51. this.error = false;
  52. this.$emit('update:modelValue', this.string2number(newValue));
  53. } else {
  54. this.error = true;
  55. }
  56. },
  57. modelValue: function(newValue) {
  58. this.filteredValue = newValue;
  59. },
  60. }
  61. };
  62. class NumInput {
  63. _options = componentOptions;
  64. _props = {
  65. modelValue: Number,
  66. min: { type: Number, default: -Number.MAX_VALUE },
  67. max: { type: Number, default: Number.MAX_VALUE },
  68. step: { type: Number, default: 1 },
  69. digits: { type: Number, default: 0 },
  70. disable: Boolean,
  71. minusIcon: {type: String, default: 'la la-minus-circle'},
  72. plusIcon: {type: String, default: 'la la-plus-circle'},
  73. };
  74. filteredValue = 0;
  75. error = false;
  76. created() {
  77. this.filteredValue = this.modelValue;
  78. }
  79. string2number(value) {
  80. return Number.parseFloat(Number.parseFloat(value).toFixed(this.digits));
  81. }
  82. validate(value) {
  83. let n = this.string2number(value);
  84. if (isNaN(n))
  85. return false;
  86. if (n < this.min)
  87. return false;
  88. if (n > this.max)
  89. return false;
  90. return true;
  91. }
  92. plus() {
  93. const newValue = this.modelValue + this.step;
  94. if (this.validate(newValue))
  95. this.filteredValue = newValue;
  96. }
  97. minus() {
  98. const newValue = this.modelValue - this.step;
  99. if (this.validate(newValue))
  100. this.filteredValue = newValue;
  101. }
  102. onMouseDown(event, way) {
  103. this.startClickRepeat = true;
  104. this.clickRepeat = false;
  105. if (event.button == 0) {
  106. (async() => {
  107. await utils.sleep(300);
  108. if (this.startClickRepeat) {
  109. this.clickRepeat = true;
  110. while (this.clickRepeat) {
  111. if (way == 'plus') {
  112. this.plus();
  113. } else {
  114. this.minus();
  115. }
  116. await utils.sleep(50);
  117. }
  118. }
  119. })();
  120. }
  121. }
  122. onMouseUp() {
  123. if (this.inTouch)
  124. return;
  125. this.startClickRepeat = false;
  126. this.clickRepeat = false;
  127. }
  128. onTouchStart(event, way) {
  129. if (!this.$root.isMobileDevice)
  130. return;
  131. if (event.touches.length == 1) {
  132. this.inTouch = true;
  133. this.onMouseDown({button: 0}, way);
  134. }
  135. }
  136. onTouchEnd() {
  137. if (!this.$root.isMobileDevice)
  138. return;
  139. this.inTouch = false;
  140. this.onMouseUp();
  141. }
  142. }
  143. export default vueComponent(NumInput);
  144. //-----------------------------------------------------------------------------
  145. </script>
  146. <style scoped>
  147. .no-mp {
  148. margin: 0;
  149. padding: 0;
  150. }
  151. .button {
  152. font-size: 130%;
  153. border-radius: 20px;
  154. color: #bbb;
  155. cursor: pointer;
  156. }
  157. .button:hover {
  158. color: #616161;
  159. background-color: #efebe9;
  160. }
  161. .error {
  162. background-color: #ffabab;
  163. border-radius: 3px;
  164. }
  165. .disable, .disable:hover {
  166. cursor: not-allowed;
  167. color: #bbb;
  168. background-color: white;
  169. }
  170. </style>