AutocompleteWithEnum.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 as PropType<
  45. | 'filled'
  46. | 'outlined'
  47. | 'plain'
  48. | 'underlined'
  49. | 'solo'
  50. | 'solo-inverted'
  51. | 'solo-filled'
  52. | undefined
  53. >,
  54. required: false,
  55. default: 'filled',
  56. },
  57. })
  58. const { fetch } = useEnumFetch()
  59. const { data: enumItems, pending } = fetch(props.enumName)
  60. const items: ComputedRef<Array<Enum>> = computed(() => {
  61. if (!enumItems.value) {
  62. return []
  63. }
  64. return ArrayUtils.sortObjectsByProp(enumItems.value, 'label') as Array<Enum>
  65. })
  66. </script>
  67. <style scoped lang="scss"></style>