select.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div v-loading="loading || isLoading" :element-loading-background="elementLoadingBackground" style="width: 100%;">
  3. <el-select
  4. style="width: 100%;"
  5. v-bind="$attrs"
  6. v-model="paramVal"
  7. :placeholder="$attrs.placeholder ? $attrs.placeholder : `请选择`"
  8. clearable
  9. filterable
  10. @change="handleChange"
  11. :popper-append-to-body="false"
  12. :title="optionsMapCpt.get(paramVal)?.[labelKey]"
  13. >
  14. <slot v-if="$slots.default">
  15. <el-option
  16. v-for="item in options"
  17. :key="item[valueKey]"
  18. :label="item[labelKey]"
  19. :value="item[valueKey]"
  20. :title="item[labelKey]"
  21. />
  22. </slot>
  23. </el-select>
  24. </div>
  25. </template>
  26. <script lang="ts">
  27. import {
  28. defineComponent,
  29. computed,
  30. onMounted,
  31. ref,
  32. reactive,
  33. watch,
  34. getCurrentInstance,
  35. ComponentInternalInstance,
  36. toRefs,
  37. nextTick, onBeforeMount, inject
  38. } from 'vue'
  39. import {useStore} from 'vuex'
  40. import {useRouter, useRoute} from 'vue-router'
  41. export default defineComponent({
  42. name: '',
  43. components: {},
  44. props: {
  45. param: {},
  46. label: {},
  47. options: { type: Array, default: () => [] },
  48. labelKey: { type: String, default: 'dictLabel' },
  49. valueKey: { type: String, default: 'dictValue' },
  50. static: { default: false },
  51. isLoading: { default: false }
  52. },
  53. setup(props, { emit }) {
  54. const store = useStore();
  55. const router = useRouter();
  56. const route = useRoute();
  57. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  58. const state = reactive({
  59. paramVal: props.param,
  60. loading: true,
  61. elementLoadingBackground: inject('element-loading-background', null)
  62. })
  63. watch(() => state.paramVal, (n) => {
  64. emit('emitParam', n)
  65. })
  66. watch(() => props.param, (n) => {
  67. state.paramVal = n
  68. })
  69. watch(() => [props.options, props.static], () => {
  70. state.loading = false
  71. })
  72. const optionsMapCpt = computed(() => {
  73. const map = new Map()
  74. props.options?.forEach((v: any) => {
  75. map.set(v[props.valueKey], v)
  76. })
  77. return map
  78. })
  79. const handleChange = (val: any) => {
  80. emit('getObject', optionsMapCpt.value.get(val))
  81. }
  82. onMounted(() => {
  83. if (props.static !== false || props.options?.length > 0) {
  84. state.loading = false
  85. }
  86. })
  87. return {
  88. ...toRefs(state),
  89. handleChange,
  90. optionsMapCpt
  91. }
  92. },
  93. })
  94. </script>
  95. <style scoped lang="scss">
  96. </style>