208 lines
4.3 KiB
Vue
208 lines
4.3 KiB
Vue
<template>
|
|
<div class="custom-select-wrapper" v-click-outside="closeDropdown">
|
|
<div class="custom-select-display" @click="toggleDropdown">
|
|
<span class="selected-text">{{ selectedLabel }}</span>
|
|
<div class="select-arrow" :class="{ open: isOpen }">
|
|
<svg width="10" height="5" viewBox="0 0 10 5" fill="none">
|
|
<path
|
|
d="M1 1L5 4L9 1"
|
|
stroke="#455363"
|
|
stroke-width="1.5"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
</div>
|
|
<transition name="dropdown">
|
|
<div v-if="isOpen" class="custom-select-dropdown">
|
|
<div
|
|
v-for="option in props.options"
|
|
:key="option.value"
|
|
class="custom-select-option"
|
|
:class="{ selected: option.value === props.modelValue }"
|
|
@click="selectOption(option.value)"
|
|
>
|
|
{{ option.label }}
|
|
</div>
|
|
</div>
|
|
</transition>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, computed } from "vue";
|
|
|
|
const props = defineProps({
|
|
options: {
|
|
type: Array,
|
|
required: true,
|
|
},
|
|
modelValue: {
|
|
type: [String, Number],
|
|
required: true,
|
|
},
|
|
});
|
|
|
|
const emit = defineEmits(["update:modelValue"]);
|
|
|
|
const isOpen = ref(false);
|
|
|
|
const selectedLabel = computed(() => {
|
|
const option = props.options.find((opt) => opt.value === props.modelValue);
|
|
return option ? option.label : "";
|
|
});
|
|
|
|
const toggleDropdown = () => {
|
|
isOpen.value = !isOpen.value;
|
|
};
|
|
|
|
const closeDropdown = () => {
|
|
isOpen.value = false;
|
|
};
|
|
|
|
const selectOption = (value) => {
|
|
emit("update:modelValue", value);
|
|
closeDropdown();
|
|
};
|
|
|
|
// 点击外部关闭下拉菜单
|
|
const vClickOutside = {
|
|
mounted(el, binding) {
|
|
el.clickOutsideEvent = (event) => {
|
|
if (!(el === event.target || el.contains(event.target))) {
|
|
binding.value();
|
|
}
|
|
};
|
|
document.addEventListener("click", el.clickOutsideEvent);
|
|
},
|
|
unmounted(el) {
|
|
document.removeEventListener("click", el.clickOutsideEvent);
|
|
},
|
|
};
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.custom-select-wrapper {
|
|
position: relative;
|
|
width: 100%;
|
|
height: 34 * 2.5px;
|
|
user-select: none;
|
|
}
|
|
|
|
.custom-select-display {
|
|
width: 100%;
|
|
height: 100%;
|
|
padding: 7 * 2.5px 36 * 2.5px 7 * 2.5px 12 * 2.5px;
|
|
border: 1 * 2.5px solid #e0e0e6;
|
|
border-radius: 3 * 2.5px;
|
|
background: #fff;
|
|
font-family: "PingFang SC", sans-serif;
|
|
font-weight: 400;
|
|
font-size: 14 * 2.5px;
|
|
line-height: 1.375em;
|
|
letter-spacing: 0.48 * 2.5px;
|
|
color: #455363;
|
|
cursor: pointer;
|
|
box-sizing: border-box;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
position: relative;
|
|
|
|
&:hover {
|
|
border-color: #ff7bac;
|
|
}
|
|
}
|
|
|
|
.selected-text {
|
|
flex: 1;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.select-arrow {
|
|
position: absolute;
|
|
right: 12 * 2.5px;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
transition: transform 0.3s ease;
|
|
|
|
&.open {
|
|
transform: translateY(-50%) rotate(180deg);
|
|
}
|
|
}
|
|
|
|
.custom-select-dropdown {
|
|
position: absolute;
|
|
top: calc(100% + 4 * 2.5px);
|
|
left: 0;
|
|
right: 0;
|
|
max-height: 300 * 2.5px;
|
|
overflow-y: auto;
|
|
background: #fff;
|
|
border: 1 * 2.5px solid #e0e0e6;
|
|
border-radius: 3 * 2.5px;
|
|
box-shadow: 0 2 * 2.5px 8 * 2.5px rgba(0, 0, 0, 0.1);
|
|
z-index: 1000;
|
|
|
|
// 自定义滚动条样式
|
|
&::-webkit-scrollbar {
|
|
width: 5 * 2.5px;
|
|
}
|
|
|
|
&::-webkit-scrollbar-track {
|
|
background: #f1f1f1;
|
|
border-radius: 4 * 2.5px;
|
|
}
|
|
|
|
&::-webkit-scrollbar-thumb {
|
|
background: #ccc;
|
|
border-radius: 4 * 2.5px;
|
|
}
|
|
|
|
&::-webkit-scrollbar-thumb:hover {
|
|
background: #fff0f5;
|
|
cursor: pointer;
|
|
}
|
|
}
|
|
|
|
.custom-select-option {
|
|
padding: 8 * 2.5px 12 * 2.5px;
|
|
font-family: "PingFang SC", sans-serif;
|
|
font-weight: 400;
|
|
font-size: 14 * 2.5px;
|
|
cursor: pointer;
|
|
transition: background 0.2s ease;
|
|
background: #fff;
|
|
&:hover {
|
|
background: rgba(204, 204, 204, 0.2);
|
|
}
|
|
|
|
&.selected {
|
|
color: #ff7bac;
|
|
font-weight: 500;
|
|
}
|
|
}
|
|
|
|
// 下拉动画
|
|
.dropdown-enter-active,
|
|
.dropdown-leave-active {
|
|
transition: opacity 0.2s ease, transform 0.2s ease;
|
|
}
|
|
|
|
.dropdown-enter-from {
|
|
opacity: 0;
|
|
transform: translateY(-10 * 2.5px);
|
|
}
|
|
|
|
.dropdown-leave-to {
|
|
opacity: 0;
|
|
transform: translateY(-10 * 2.5px);
|
|
}
|
|
</style>
|