Enum.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!--
  2. Liste déroulante dédiée à l'affichage d'objets Enum
  3. @see https://vuetifyjs.com/en/components/selects/
  4. -->
  5. <template>
  6. <main>
  7. <v-skeleton-loader
  8. v-if="$fetchState.pending"
  9. type="list-item"
  10. loading
  11. />
  12. <v-select
  13. v-else
  14. :label="$t(fieldLabel)"
  15. :value="data"
  16. :items="items"
  17. item-text="label"
  18. item-value="value"
  19. :rules="rules"
  20. :disabled="readonly"
  21. :error="error || !!violation"
  22. :error-messages="errorMessage || violation ? $t(violation) : ''"
  23. @change="onChange($event)"
  24. />
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {useNuxtApp} from "#app";
  29. import {useFieldViolation} from "~/composables/form/useFieldViolation";
  30. import {ref, Ref} from "@vue/reactivity";
  31. import {useEnumFetch} from "~/composables/data/useEnumFetch";
  32. import {Enum} from "~/types/data";
  33. const props = defineProps({
  34. enumType: {
  35. type: String,
  36. required: true
  37. },
  38. label: {
  39. type: String,
  40. required: false,
  41. default: null
  42. },
  43. field: {
  44. type: String,
  45. required: false,
  46. default: null
  47. },
  48. data: {
  49. type: String,
  50. required: false,
  51. default: null
  52. },
  53. readonly: {
  54. type: Boolean,
  55. required: false
  56. },
  57. rules: {
  58. type: Array,
  59. required: false,
  60. default: () => []
  61. },
  62. error: {
  63. type: Boolean,
  64. required: false
  65. },
  66. errorMessage: {
  67. type: String,
  68. required: false,
  69. default: null
  70. }
  71. })
  72. const { emit } = useNuxtApp()
  73. const fieldLabel = props.label ?? props.field
  74. const { enumType } = props
  75. const { violation, onChange } = useFieldViolation(props.field, emit)
  76. const items: Ref<Enum> = ref([])
  77. const { fetch } = useEnumFetch()
  78. const { data: fetched, pending } = fetch(enumType)
  79. items.value = fetched.value || []
  80. </script>
  81. <style scoped>
  82. </style>