Number.vue 3.0 KB

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