Number.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. required: true
  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. /**
  64. * Reference to the v-text-field
  65. */
  66. const input: Ref<any> = ref(null)
  67. /**
  68. * Cast the value to a number, or fallback on default value
  69. * @param val
  70. */
  71. const cast = (val: number | string): number => {
  72. val = Number(val)
  73. if (isNaN(val)) {
  74. return props.default
  75. }
  76. return val
  77. }
  78. /**
  79. * Ensure the value is between min and max values
  80. * @param val
  81. */
  82. const keepInRange = (val: number) => {
  83. if (props.min !== null && props.max !== null && props.min >= props.max) {
  84. console.warn('Number input: minimum value is greater than maximum value')
  85. }
  86. if (props.min !== null && val < props.min) {
  87. val = props.min
  88. }
  89. if (props.max !== null && val > props.max) {
  90. val = props.max
  91. }
  92. return val
  93. }
  94. const emit = defineEmits(['update:modelValue'])
  95. /**
  96. * Emit the update event
  97. */
  98. const emitUpdate = () => {
  99. emit('update:modelValue', props.modelValue)
  100. }
  101. /**
  102. * Setup min and max values at the input level
  103. */
  104. onMounted(() => {
  105. const inputElement = input.value.$el.querySelector('input')
  106. if (props.min !== null) {
  107. inputElement.min = props.min
  108. }
  109. if (props.max !== null) {
  110. inputElement.max = props.max
  111. }
  112. })
  113. </script>