Number.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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="onModelUpdate($event)"
  14. />
  15. </template>
  16. <script setup lang="ts">
  17. import type { PropType } from 'vue'
  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<
  67. | 'filled'
  68. | 'outlined'
  69. | 'plain'
  70. | 'underlined'
  71. | 'solo'
  72. | 'solo-inverted'
  73. | 'solo-filled'
  74. | undefined
  75. >,
  76. required: false,
  77. default: 'filled',
  78. },
  79. })
  80. /**
  81. * Reference to the v-text-field
  82. */
  83. const input: Ref<any> = ref(null)
  84. /**
  85. * Cast the value to a number, or fallback on default value
  86. * @param val
  87. */
  88. const cast = (val: number | string): number => {
  89. val = Number(val)
  90. if (isNaN(val)) {
  91. return props.default
  92. }
  93. return val
  94. }
  95. /**
  96. * Ensure the value is between min and max values
  97. * @param val
  98. */
  99. const keepInRange = (val: number) => {
  100. if (props.min !== null && props.max !== null && props.min >= props.max) {
  101. console.warn('Number input: minimum value is greater than maximum value')
  102. }
  103. if (props.min !== null && val < props.min) {
  104. val = props.min
  105. }
  106. if (props.max !== null && val > props.max) {
  107. val = props.max
  108. }
  109. return val
  110. }
  111. const onModelUpdate = (event: string) => {
  112. // eslint-disable-next-line vue/no-mutating-props
  113. props.modelValue = keepInRange(cast(event))
  114. emitUpdate()
  115. }
  116. const emit = defineEmits(['update:modelValue'])
  117. /**
  118. * Emit the update event
  119. */
  120. const emitUpdate = () => {
  121. emit('update:modelValue', props.modelValue)
  122. }
  123. /**
  124. * Setup min and max values at the input level
  125. */
  126. onMounted(() => {
  127. const inputElement = input.value.$el.querySelector('input')
  128. if (props.min !== null) {
  129. inputElement.min = props.min
  130. }
  131. if (props.max !== null) {
  132. inputElement.max = props.max
  133. }
  134. })
  135. </script>