Number.vue 2.7 KB

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