Number.vue 2.4 KB

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