AssociadoSelect.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <DefaultSelect
  3. v-model="selected"
  4. v-bind="$attrs"
  5. use-input
  6. hide-selected
  7. fill-input
  8. clearable
  9. :options="options"
  10. :label
  11. :loading
  12. :placeholder
  13. @filter="filterFn"
  14. >
  15. <template #no-option>
  16. <q-item>
  17. <q-item-section class="text-grey">
  18. {{ $t("http.errors.no_records_found") }}
  19. </q-item-section>
  20. </q-item>
  21. </template>
  22. </DefaultSelect>
  23. </template>
  24. <script setup>
  25. import { ref, onMounted } from "vue";
  26. import { getAssociados } from "src/api/user";
  27. import { normalizeString } from "src/helpers/utils";
  28. import { useI18n } from "vue-i18n";
  29. import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
  30. const { label, placeholder } = defineProps({
  31. label: {
  32. type: String,
  33. default: () => useI18n().t("associado.associado"),
  34. },
  35. placeholder: {
  36. type: String,
  37. default: () => useI18n().t("common.actions.search"),
  38. },
  39. });
  40. const selected = defineModel({ type: Object });
  41. const loading = ref(true);
  42. const baseOptions = ref([]);
  43. const options = ref([]);
  44. const filterFn = (val, update) => {
  45. const needle = normalizeString(val);
  46. options.value = baseOptions.value.filter((v) =>
  47. normalizeString(v.label).includes(needle),
  48. );
  49. update();
  50. };
  51. onMounted(async () => {
  52. try {
  53. const associados = await getAssociados();
  54. baseOptions.value = associados.map((a) => ({
  55. label: a.name,
  56. value: a.id,
  57. data: a,
  58. }));
  59. options.value = baseOptions.value;
  60. } catch (e) {
  61. console.error(e);
  62. } finally {
  63. loading.value = false;
  64. }
  65. });
  66. </script>