select.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export default {
  2. props: ["options"],
  3. template: `
  4. <q-select
  5. ref="qRef"
  6. v-bind="$attrs"
  7. :options="filteredOptions"
  8. @filter="filterFn"
  9. >
  10. <template v-for="(_, slot) in $slots" v-slot:[slot]="slotProps">
  11. <slot :name="slot" v-bind="slotProps || {}" />
  12. </template>
  13. </q-select>
  14. `,
  15. data() {
  16. return {
  17. initialOptions: this.options,
  18. filteredOptions: this.options,
  19. };
  20. },
  21. methods: {
  22. filterFn(val, update, abort) {
  23. update(() => (this.filteredOptions = val ? this.findFilteredOptions() : this.initialOptions));
  24. },
  25. findFilteredOptions() {
  26. const needle = this.$el.querySelector("input[type=search]")?.value.toLocaleLowerCase();
  27. return needle
  28. ? this.initialOptions.filter((v) => String(v.label).toLocaleLowerCase().indexOf(needle) > -1)
  29. : this.initialOptions;
  30. },
  31. },
  32. updated() {
  33. if (!this.$attrs.multiple) return;
  34. const newFilteredOptions = this.findFilteredOptions();
  35. if (newFilteredOptions.length !== this.filteredOptions.length) {
  36. this.filteredOptions = newFilteredOptions;
  37. }
  38. },
  39. watch: {
  40. options: {
  41. handler(newOptions) {
  42. this.initialOptions = newOptions;
  43. this.filteredOptions = newOptions;
  44. },
  45. immediate: true,
  46. },
  47. },
  48. };