selectCustom.vue 4.24 KB
<template>
  <el-dropdown placement="bottom-start"
               trigger="click"
               :disabled="disabled"
               @command="handleSelect">
    <div class="icon-text-btn"
         :class="{ 'has-selected': value && value.label && value.label !== defaultText, 'is-disabled': disabled }">
      <img v-if="triggerIconType === 'image'"
           alt=""
           class="icon-img"
           :src="triggerIcon">
      <i v-else-if="triggerIconType === 'icon'"
         :class="triggerIcon">
      </i>
      <span>{{ label }}:{{ currentValue }}</span>
    </div>
    <template #dropdown>
      <el-dropdown-menu class="select-custom-dropdown"
                        :style="{ minWidth: `${popoverWidth}px` }">
        <el-dropdown-item v-for="(item, index) in options"
                          :key="index"
                          :class="{ 'is-selected': isSelected(item) }"
                          :command="item">
          <div class="icon-img">
            <img v-if="item.iconUrl"
                 alt=""
                 class="icon-img"
                 :src="item.iconUrl">
            <i v-else-if="item.icon"
               :class="item.icon">
            </i>
          </div>
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script>
export default {
	name: "SelectCustom",
	props: {
		options: {
			type: Array,
			required: true,
			default: () => [],
		},
		value: {
			type: Object,
			default: () => ({ label: "盯防类型", value: "" }),
		},
		label: {
			type: String,
			required: true,
		},
		triggerIcon: {
			type: String,
			required: true,
		},
		triggerIconType: {
			type: String,
			default: "icon",
			validator: value => ["image", "icon"].includes(value),
		},
		defaultText: {
			type: String,
			default: "全部",
		},
		popoverWidth: {
			type: Number,
			default: 120,
		},
		disabled: {
			type: Boolean,
			default: false,
		},
	},
	computed: {
		currentValue() {
			if (this.value && typeof this.value === "object" && this.value.label) {
				return this.value.label;
			}
			return this.defaultText;
		},
	},
	methods: {
		// 根据选项 value(即 warningType)动态获取图标,逻辑与 monitorCard.getAlertIcon 一致
		getOptionIcon(item) {
			if (!item || item.value == null) {
				return null;
			}
			const iconMap = {
				2: require("@/assets/strategy/icon-strategy-type01.png"),
				3: require("@/assets/strategy/icon-strategy-type02.png"),
				4: require("@/assets/strategy/icon-strategy-type07.png"),
				5: require("@/assets/strategy/icon-strategy-type08.png"),
			};
			const key = item.value ?? item.warningType;
			return iconMap[key] || iconMap[2];
		},
		handleSelect(item) {
			this.$emit("change", item);
			const selectedValue = {
				label: item.label,
				value: item.value !== undefined ? item.value : item.label,
			};
			this.$emit("input", selectedValue);
		},
		isSelected(item) {
			if (!this.value)
				return false;
			const itemValue = item.value !== undefined ? item.value : item.label;
			const currentValue = this.value.value !== undefined ? this.value.value : this.value.label;
			return itemValue === currentValue;
		},
	},
};
</script>

<style lang="scss" scoped>
.icon-text-btn {
	cursor: pointer;
	display: flex;
	align-items: center;
	padding: 4px 8px;
	border-radius: 4px;
	transition: all 0.3s;
	font-size: 14px;
	color: #333333;

	&:hover {
		color: #2a58ad;
	}

	i {
		margin-right: 6px;
		font-size: 16px;
	}

	.icon-img {
		width: 16px;
		height: 16px;
		margin-right: 6px;
		object-fit: contain;
		vertical-align: middle;
	}

	&.has-selected {
		background-color: #eff3f6;
	}

	&.is-disabled {
		cursor: not-allowed;
		color: #c0c4cc;

		&:hover {
			color: #c0c4cc;
		}
	}
}
</style>

<style>
.select-custom-dropdown {
	padding: 4px 0 !important;
	border-color: #e5e6eb !important;
	box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1) !important;
}

.select-custom-dropdown .el-dropdown-menu__item {
	display: flex;
	align-items: center;
	padding: 9px 16px;
	font-size: 14px;
	color: #333333;
	line-height: 1.5;
}

.select-custom-dropdown .el-dropdown-menu__item.is-selected {
	background-color: #f7f7f7;
}

.select-custom-dropdown .el-dropdown-menu__item .icon-img {
	width: 18px;
	height: 18px;
	border-radius: 3px;
	margin-right: 8px;
}
</style>