68 lines
2.4 KiB
Vue
68 lines
2.4 KiB
Vue
<template>
|
|
<div class="aa-dropdown" ref="root">
|
|
<div class="aa-dropdown-trigger" @click="open = !open">
|
|
<slot>操作</slot>
|
|
</div>
|
|
<div v-show="open" class="aa-dropdown-panel">
|
|
<template v-for="(item, i) in items" :key="item.key || 'd' + i">
|
|
<div v-if="item.divider" class="aa-dropdown-divider"></div>
|
|
<div
|
|
v-else
|
|
class="aa-dropdown-item"
|
|
:class="{ 'is-danger': item.danger, 'is-disabled': item.disabled }"
|
|
@click="onClick(item)"
|
|
>
|
|
<span v-if="item.icon" class="ico">{{ item.icon }}</span>
|
|
{{ item.label }}
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
|
|
|
defineProps({
|
|
items: { type: Array, default: () => [] } // [{ key, label, danger, disabled, divider, icon }]
|
|
});
|
|
const emit = defineEmits(['select']);
|
|
|
|
const open = ref(false);
|
|
const root = ref(null);
|
|
|
|
function onClick(item) {
|
|
if (item.disabled || item.divider) return;
|
|
emit('select', item.key != null ? item.key : item.label);
|
|
open.value = false;
|
|
}
|
|
function onDocClick(e) {
|
|
if (root.value && !root.value.contains(e.target)) open.value = false;
|
|
}
|
|
onMounted(() => document.addEventListener('click', onDocClick));
|
|
onBeforeUnmount(() => document.removeEventListener('click', onDocClick));
|
|
</script>
|
|
|
|
<!-- 样式对齐 组件2.txt Dropdown 规范 -->
|
|
<style scoped>
|
|
.aa-dropdown { position: relative; display: inline-block; }
|
|
.aa-dropdown-trigger {
|
|
border: 1px solid #E8ECF1; background: #fff; border-radius: 4px; height: 32px; padding: 0 12px;
|
|
cursor: pointer; font-size: 14px; color: #262626; display: inline-flex; align-items: center; gap: 4px;
|
|
}
|
|
.aa-dropdown-trigger:hover { border-color: #2F54EB; }
|
|
.aa-dropdown-panel {
|
|
position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; min-width: 120px;
|
|
background: #fff; border-radius: 6px; box-shadow: 0 4px 12px rgba(0,0,0,0.08); padding: 4px 0;
|
|
}
|
|
.aa-dropdown-item {
|
|
display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 12px; cursor: pointer;
|
|
font-size: 14px; color: #262626; white-space: nowrap;
|
|
}
|
|
.aa-dropdown-item:hover { background: #F5F7FA; }
|
|
.aa-dropdown-item.is-danger { color: #F5222D; }
|
|
.aa-dropdown-item.is-disabled { color: #BFBFBF; cursor: not-allowed; }
|
|
.aa-dropdown-item.is-disabled:hover { background: transparent; }
|
|
.aa-dropdown-divider { height: 1px; background: #F0F0F0; margin: 4px 0; }
|
|
</style>
|