CSelect.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <script setup lang="ts">
  2. import type { CSelectItem } from '@/types/HomeTypes'
  3. import { CSelectType } from '@/types/HomeTypes'
  4. interface Props {
  5. selectInfo: {
  6. [key: string]: CSelectItem
  7. }
  8. selectStyle?: string
  9. }
  10. withDefaults(defineProps<Props>(), {
  11. selectStyle: 'width: 150px; margin-right: 10px',
  12. })
  13. const emits = defineEmits(['changeSelect'])
  14. /**
  15. * @description: 选择改变,向外抛出事件
  16. * @param {*} newVal 新值
  17. * @param {*} name 新值对应的字段名
  18. * @param {*} item 当前选择项
  19. * @return {*}
  20. */
  21. const changeSelect = (newVal: any, name: string, item: CSelectItem) => {
  22. // 这里如果有图例的话需要去同步一下cnName
  23. if (item.label) {
  24. item.label = item.options.find(item => item.value === newVal)?.label
  25. }
  26. emits('changeSelect', newVal, name)
  27. }
  28. </script>
  29. <template>
  30. <div style="display: flex; align-items: center">
  31. <template v-for="selectItem in selectInfo">
  32. <div style="display: inline-flex; align-items: center">
  33. <div
  34. class="legendColor"
  35. v-if="selectItem.type === CSelectType.RadioLegend"
  36. :style="{ backgroundColor: selectItem.color }"
  37. ></div>
  38. <!-- 对于值为对象的选项需要给value-key,默认为value -->
  39. <el-select
  40. v-model="selectItem.value"
  41. placeholder=""
  42. @change="changeSelect(selectItem.value, selectItem.name, selectItem)"
  43. v-bind="$attrs"
  44. :multiple="
  45. selectItem.type === CSelectType.Multiple ||
  46. selectItem.type === CSelectType.MultipleTag
  47. "
  48. collapse-tags
  49. :style="selectStyle"
  50. :value-key="typeof selectItem.value === 'object' ? 'name' : 'value'"
  51. >
  52. <el-option
  53. v-for="listItem in selectItem.options"
  54. :key="listItem.value"
  55. :label="listItem.label"
  56. :value="listItem.value"
  57. />
  58. <template #tag v-if="selectItem.type === CSelectType.MultipleTag">
  59. <span style="font-size: 14px; color: #606266; padding-left: 5px">{{
  60. selectItem.tagText
  61. }}</span>
  62. </template>
  63. </el-select>
  64. </div>
  65. </template>
  66. </div>
  67. </template>
  68. <style scoped>
  69. .legendColor {
  70. width: 20px;
  71. height: 15px;
  72. margin-right: 8px;
  73. border-radius: 3px;
  74. }
  75. </style>