Number.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!--
  2. An input for numeric values
  3. -->
  4. <!-- eslint-disable vue/valid-v-bind -->
  5. <template>
  6. <v-text-field
  7. ref="input"
  8. :model-value.number="modelValue"
  9. :label="label || field ? $t(label ?? field) : undefined"
  10. hide-details
  11. type="number"
  12. :variant="variant"
  13. density="compact"
  14. @update:model-value="onModelUpdate($event)"
  15. />
  16. </template>
  17. <script setup lang="ts">
  18. import type { PropType, Ref } from 'vue'
  19. type Density = null | 'default' | 'comfortable' | 'compact'
  20. const props = defineProps({
  21. modelValue: {
  22. type: Number,
  23. required: true,
  24. },
  25. /**
  26. * Nom de la propriété d'une entité lorsque l'input concerne cette propriété
  27. * - Utilisé par la validation
  28. * - Laisser null si le champ ne s'applique pas à une entité
  29. */
  30. field: {
  31. type: String,
  32. required: false,
  33. default: null,
  34. },
  35. /**
  36. * Label du champ
  37. * Si non défini, c'est le nom de propriété qui est utilisé
  38. */
  39. label: {
  40. type: String,
  41. required: false,
  42. default: null,
  43. },
  44. default: {
  45. type: Number,
  46. required: false,
  47. default: 0,
  48. },
  49. min: {
  50. type: Number,
  51. required: false,
  52. default: null,
  53. },
  54. max: {
  55. type: Number,
  56. required: false,
  57. default: null,
  58. },
  59. /**
  60. * @see https://vuetifyjs.com/en/api/v-autocomplete/#props-variant
  61. */
  62. variant: {
  63. type: String as PropType<
  64. | 'filled'
  65. | 'outlined'
  66. | 'plain'
  67. | 'underlined'
  68. | 'solo'
  69. | 'solo-inverted'
  70. | 'solo-filled'
  71. | undefined
  72. >,
  73. required: false,
  74. default: 'outlined',
  75. },
  76. })
  77. /**
  78. * Reference to the v-text-field
  79. */
  80. // eslint-disable-next-line
  81. const input: Ref<any> = ref(null)
  82. /**
  83. * Cast the value to a number, or fallback on default value
  84. * @param val
  85. */
  86. const cast = (val: number | string): number => {
  87. val = Number(val)
  88. if (isNaN(val)) {
  89. return props.default
  90. }
  91. return val
  92. }
  93. /**
  94. * Ensure the value is between min and max values
  95. * @param val
  96. */
  97. const keepInRange = (val: number) => {
  98. if (props.min !== null && props.max !== null && props.min >= props.max) {
  99. console.warn('Number input: minimum value is greater than maximum value')
  100. }
  101. if (props.min !== null && val < props.min) {
  102. val = props.min
  103. }
  104. if (props.max !== null && val > props.max) {
  105. val = props.max
  106. }
  107. return val
  108. }
  109. const emit = defineEmits(['update:modelValue'])
  110. const onModelUpdate = (event: string) => {
  111. // props.modelValue = keepInRange(cast(event))
  112. // emitUpdate()
  113. emit('update:modelValue', keepInRange(cast(event)))
  114. }
  115. /**
  116. * Setup min and max values at the input level
  117. */
  118. onMounted(() => {
  119. const inputElement = input.value!.$el.querySelector('input')
  120. if (props.min !== null) {
  121. inputElement.min = props.min
  122. }
  123. if (props.max !== null) {
  124. inputElement.max = props.max
  125. }
  126. })
  127. </script>