NumInput.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <q-input outlined dense
  3. v-model="filteredValue"
  4. input-style="text-align: center"
  5. class="no-mp"
  6. :class="(error ? 'error' : '')"
  7. >
  8. <slot></slot>
  9. <template v-slot:prepend>
  10. <q-icon :class="(validate(value - step) ? '' : 'disable')"
  11. name="la la-minus-circle"
  12. class="button"
  13. :v-ripple="validate(value - step)"
  14. @click="minus"
  15. @mousedown.prevent.stop="onMouseDown($event, 'minus')"
  16. @mouseup.prevent.stop="onMouseUp($event, 'minus')"
  17. @mouseout.prevent.stop="onMouseUp($event, 'minus')"
  18. />
  19. </template>
  20. <template v-slot:append>
  21. <q-icon :class="(validate(value + step) ? '' : 'disable')"
  22. name="la la-plus-circle"
  23. class="button"
  24. :v-ripple="validate(value + step)"
  25. @click="plus"
  26. @mousedown.prevent.stop="onMouseDown($event, 'plus')"
  27. @mouseup.prevent.stop="onMouseUp($event, 'plus')"
  28. @mouseout.prevent.stop="onMouseUp($event, 'plus')"
  29. />
  30. </template>
  31. </q-input>
  32. </template>
  33. <script>
  34. //-----------------------------------------------------------------------------
  35. import Vue from 'vue';
  36. import Component from 'vue-class-component';
  37. import * as utils from '../../share/utils';
  38. const NumInputProps = Vue.extend({
  39. props: {
  40. value: Number,
  41. min: { type: Number, default: -Number.MAX_VALUE },
  42. max: { type: Number, default: Number.MAX_VALUE },
  43. step: { type: Number, default: 1 },
  44. }
  45. })
  46. export default @Component({
  47. watch: {
  48. filteredValue: function(newValue) {
  49. if (this.validate(newValue)) {
  50. this.error = false;
  51. this.$emit('input', Number.parseFloat(newValue));
  52. } else {
  53. this.error = true;
  54. }
  55. },
  56. value: function(newValue) {
  57. this.filteredValue = newValue;
  58. },
  59. }
  60. })
  61. class NumInput extends NumInputProps {
  62. filteredValue = 0;
  63. error = false;
  64. created() {
  65. this.mask = '#'.repeat(this.max.toString().length);
  66. this.filteredValue = this.value;
  67. }
  68. validate(value) {
  69. let n = Number.parseFloat(value);
  70. if (isNaN(n))
  71. return false;
  72. if (n < this.min)
  73. return false;
  74. if (n > this.max)
  75. return false;
  76. return true;
  77. }
  78. plus() {
  79. const newValue = this.value + this.step;
  80. if (this.validate(newValue))
  81. this.filteredValue = newValue;
  82. }
  83. minus() {
  84. const newValue = this.value - this.step;
  85. if (this.validate(newValue))
  86. this.filteredValue = newValue;
  87. }
  88. onMouseDown(event, way) {
  89. this.startClickRepeat = true;
  90. this.clickRepeat = false;
  91. if (event.button == 0) {
  92. (async() => {
  93. await utils.sleep(300);
  94. if (this.startClickRepeat) {
  95. this.clickRepeat = true;
  96. while (this.clickRepeat) {
  97. if (way == 'plus') {
  98. this.plus();
  99. } else {
  100. this.minus();
  101. }
  102. await utils.sleep(50);
  103. }
  104. }
  105. })();
  106. }
  107. }
  108. onMouseUp() {
  109. this.startClickRepeat = false;
  110. this.clickRepeat = false;
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. </script>
  115. <style scoped>
  116. .no-mp {
  117. margin: 0;
  118. padding: 0;
  119. }
  120. .button {
  121. font-size: 130%;
  122. border-radius: 20px;
  123. color: #bbb;
  124. }
  125. .button:hover {
  126. color: #616161;
  127. background-color: #efebe9;
  128. }
  129. .error {
  130. background-color: #ffabab;
  131. border-radius: 3px;
  132. }
  133. .disable, .disable:hover {
  134. cursor: not-allowed;
  135. color: #bbb;
  136. background-color: white;
  137. }
  138. </style>