index.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <el-select v-bind="$attrs" class="w-full" clearable filterable>
  3. <el-option-group v-for="group in elOptions" :key="group.label" :label="group.label" v-if="group">
  4. <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.value" />
  5. </el-option-group>
  6. <el-option v-for="option in elOptions" :key="option.value" :label="option.label" :value="option.value" v-else>
  7. <slot />
  8. </el-option>
  9. </el-select>
  10. </template>
  11. <script lang="ts" setup>
  12. import http from '/admin/support/http'
  13. import { ref, watch } from 'vue'
  14. const props = defineProps({
  15. options: {
  16. type: Array,
  17. default: [],
  18. },
  19. api: {
  20. type: String,
  21. default: '',
  22. },
  23. group: {
  24. type: Boolean,
  25. default: false,
  26. },
  27. query: {
  28. type: Object,
  29. default: null,
  30. },
  31. })
  32. interface Option {
  33. label: string
  34. value: string | number
  35. }
  36. interface GroupOption {
  37. label: string
  38. options: Array<Option>
  39. }
  40. const getOptions = () => {
  41. http.get('options/' + props.api, props.query).then(r => {
  42. elOptions.value = r.data.data
  43. })
  44. }
  45. const elOptions = props.group ? ref<Array<GroupOption>>() : ref<Array<Option>>()
  46. if (props.api) {
  47. if (!props.query) {
  48. getOptions()
  49. } else {
  50. watch(props, function () {
  51. getOptions()
  52. })
  53. }
  54. } else {
  55. elOptions.value = props.options
  56. }
  57. </script>