NumInput.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. />
  16. </template>
  17. <template v-slot:append>
  18. <q-icon :class="(validate(value + step) ? '' : 'disable')"
  19. name="la la-plus-circle"
  20. class="button"
  21. :v-ripple="validate(value + step)"
  22. @click="plus"/>
  23. </template>
  24. </q-input>
  25. </template>
  26. <script>
  27. //-----------------------------------------------------------------------------
  28. import Vue from 'vue';
  29. import Component from 'vue-class-component';
  30. const NumInputProps = Vue.extend({
  31. props: {
  32. value: Number,
  33. min: { type: Number, default: -Number.MAX_VALUE },
  34. max: { type: Number, default: Number.MAX_VALUE },
  35. step: { type: Number, default: 1 },
  36. }
  37. })
  38. export default @Component({
  39. watch: {
  40. filteredValue: function(newValue) {
  41. if (this.validate(newValue)) {
  42. this.error = false;
  43. this.$emit('input', Number.parseFloat(newValue));
  44. } else {
  45. this.error = true;
  46. }
  47. },
  48. value: function(newValue) {
  49. this.filteredValue = newValue;
  50. },
  51. }
  52. })
  53. class NumInput extends NumInputProps {
  54. filteredValue = 0;
  55. error = false;
  56. created() {
  57. this.mask = '#'.repeat(this.max.toString().length);
  58. this.filteredValue = this.value;
  59. }
  60. validate(value) {
  61. let n = Number.parseFloat(value);
  62. if (isNaN(n))
  63. return false;
  64. if (n < this.min)
  65. return false;
  66. if (n > this.max)
  67. return false;
  68. return true;
  69. }
  70. plus() {
  71. const newValue = this.value + this.step;
  72. if (this.validate(newValue))
  73. this.filteredValue = newValue;
  74. }
  75. minus() {
  76. const newValue = this.value - this.step;
  77. if (this.validate(newValue))
  78. this.filteredValue = newValue;
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. </script>
  83. <style scoped>
  84. .no-mp {
  85. margin: 0;
  86. padding: 0;
  87. }
  88. .button {
  89. font-size: 130%;
  90. border-radius: 20px;
  91. color: #bbb;
  92. }
  93. .button:hover {
  94. color: #616161;
  95. background-color: #efebe9;
  96. }
  97. .error {
  98. background-color: #ffabab;
  99. border-radius: 3px;
  100. }
  101. .disable, .disable:hover {
  102. cursor: not-allowed;
  103. color: #bbb;
  104. background-color: white;
  105. }
  106. </style>