AutocompleteWithEnum.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <UiInputAutocomplete
  3. :model-value="modelValue"
  4. :field="field"
  5. :items="items"
  6. :is-loading="pending"
  7. :return-object="false"
  8. item-title="label"
  9. item-value="value"
  10. :variant="variant"
  11. @update:model-value="$emit('update:model-value', $event)"
  12. />
  13. </template>
  14. <script setup lang="ts">
  15. import {useEnumFetch} from "~/composables/data/useEnumFetch";
  16. import ArrayUtils from "~/services/utils/arrayUtils";
  17. import type {ComputedRef} from "@vue/reactivity";
  18. import type {Enum} from "~/types/data";
  19. import type {PropType} from "@vue/runtime-core";
  20. const props = defineProps({
  21. modelValue: {
  22. type: String as PropType<string | null>,
  23. required: false,
  24. default: null
  25. },
  26. enumName: {
  27. type: String,
  28. required: true
  29. },
  30. field: {
  31. type: String,
  32. required: false,
  33. default: null
  34. },
  35. label: {
  36. type: String,
  37. required: false,
  38. default: null
  39. },
  40. /**
  41. * @see https://vuetifyjs.com/en/api/v-autocomplete/#props-variant
  42. */
  43. variant: {
  44. type: String,
  45. required: false,
  46. default: 'filled'
  47. }
  48. })
  49. const { fetch } = useEnumFetch()
  50. const { data: enumItems, pending } = fetch(props.enumName)
  51. const items: ComputedRef<Array<Enum>> = computed(() => {
  52. if (!enumItems.value) {
  53. return []
  54. }
  55. return ArrayUtils.sortObjectsByProp(enumItems.value, 'label') as Array<Enum>
  56. })
  57. </script>
  58. <style scoped lang="scss">
  59. </style>