Files
aurora-admin/frameworks/Select.vue3.vue
T

169 lines
7.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="aa-select" :class="{ 'is-open': open, 'is-disabled': disabled }" ref="root">
<div class="aa-select-trigger" @click="toggle" tabindex="0">
<template v-if="multiple">
<span v-if="!selectedArr.length" class="placeholder">{{ placeholder }}</span>
<span v-for="v in selectedArr" :key="v" class="tag">
{{ labelOf(v) }}<span class="x" @click.stop="remove(v)">✕</span>
</span>
</template>
<span v-else-if="selectedVal == null" class="placeholder">{{ placeholder }}</span>
<span v-else>{{ labelOf(selectedVal) }}</span>
<span class="caret">▾</span>
</div>
<div class="aa-select-panel" v-show="open">
<div v-if="searchable" class="aa-select-search">
<input v-model="keyword" placeholder="输入关键字" @click.stop>
</div>
<template v-if="groups && groups.length">
<template v-for="g in filteredGroups" :key="g.label">
<div class="aa-select-group-label">{{ g.label }}</div>
<div
v-for="o in g.options" :key="o.value"
class="aa-select-option"
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
@click="choose(o)"
>
{{ o.label }}<span class="check">✓</span>
</div>
</template>
</template>
<template v-else>
<div
v-for="o in filteredOptions" :key="o.value"
class="aa-select-option"
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
@click="choose(o)"
>
{{ o.label }}<span class="check">✓</span>
</div>
</template>
<div v-if="isEmpty" class="aa-select-empty">无匹配选项</div>
</div>
</div>
</template>
<script setup>
import { computed, ref, watch, onMounted, onBeforeUnmount } from 'vue';
const props = defineProps({
modelValue: { type: [String, Number, Array], default: null },
options: { type: Array, default: () => [] }, // [{ value, label, disabled? }]
groups: { type: Array, default: null }, // [{ label, options: [...] }]
multiple: { type: Boolean, default: false },
searchable: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
placeholder: { type: String, default: '请选择' }
});
const emit = defineEmits(['update:modelValue', 'change']);
const open = ref(false);
const keyword = ref('');
const root = ref(null);
const selectedArr = computed(() => Array.isArray(props.modelValue) ? props.modelValue : []);
const selectedVal = computed(() => Array.isArray(props.modelValue) ? null : props.modelValue);
const filteredOptions = computed(() => {
if (!props.searchable || !props.keyword) return props.options;
const q = props.keyword.trim().toLowerCase();
return props.options.filter(o => String(o.label).toLowerCase().includes(q));
});
const filteredGroups = computed(() => {
if (!props.groups) return [];
if (!props.searchable || !props.keyword) return props.groups;
const q = props.keyword.trim().toLowerCase();
return props.groups
.map(g => ({ label: g.label, options: g.options.filter(o => String(o.label).toLowerCase().includes(q)) }))
.filter(g => g.options.length);
});
const isEmpty = computed(() =>
props.groups ? filteredGroups.value.length === 0 : filteredOptions.value.length === 0
);
watch(open, (v) => { if (v && props.searchable) keyword.value = ''; });
function toggle() {
if (props.disabled) return;
open.value = !open.value;
}
function isSelected(v) {
return props.multiple ? selectedArr.value.includes(v) : selectedVal.value === v;
}
function labelOf(v) {
const flat = props.options.length ? props.options : (props.groups || []).flatMap(g => g.options);
const found = flat.find(o => o.value === v);
return found ? found.label : v;
}
function choose(o) {
if (o.disabled) return;
if (props.multiple) {
const next = selectedArr.value.includes(o.value)
? selectedArr.value.filter(x => x !== o.value)
: [...selectedArr.value, o.value];
emit('update:modelValue', next);
emit('change', next);
} else {
emit('update:modelValue', o.value);
emit('change', o.value);
open.value = false;
}
}
function remove(v) {
const next = selectedArr.value.filter(x => x !== v);
emit('update:modelValue', next);
emit('change', next);
}
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>
<!-- 样式对齐 components.css 与 select.json 契约(触发器样式同 Input) -->
<style scoped>
.aa-select { position: relative; display: inline-block; min-width: 220px; }
.aa-select-trigger {
display: flex; align-items: center; gap: 8px; min-height: 32px; height: auto;
padding: 2px 28px 2px 12px; background: #fff; border: 1px solid #E8ECF1;
border-radius: 4px; box-sizing: border-box; cursor: pointer; transition: border-color .15s, box-shadow .15s;
font-size: 14px; color: #262626; flex-wrap: wrap; position: relative;
}
.aa-select-trigger:hover { border-color: #2F54EB; }
.aa-select.is-open .aa-select-trigger { border-color: #2F54EB; box-shadow: 0 0 0 2px rgba(47,84,235,0.2); }
.aa-select.is-disabled .aa-select-trigger { background: #F5F5F5; border-color: transparent; color: #BFBFBF; cursor: not-allowed; }
.aa-select-trigger .placeholder { color: #BFBFBF; }
.aa-select-trigger .caret { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); transition: transform .15s; color: #8C8C8C; }
.aa-select.is-open .caret { transform: translateY(-50%) rotate(180deg); }
.aa-select-trigger .tag {
display: inline-flex; align-items: center; gap: 4px; background: #F0F5FF; color: #2F54EB;
border-radius: 2px; padding: 1px 6px; font-size: 12px; line-height: 20px;
}
.aa-select-trigger .tag .x { cursor: pointer; }
.aa-select-panel {
position: absolute; z-index: 10; top: calc(100% + 4px); left: 0; right: 0;
background: #fff; border-radius: 4px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
max-height: 256px; overflow-y: auto; padding: 4px 0;
}
.aa-select-search { padding: 6px 8px; border-bottom: 1px solid #F0F0F0; }
.aa-select-search input { width: 100%; box-sizing: border-box; border: 1px solid #E8ECF1; border-radius: 4px; padding: 4px 8px; font-size: 13px; outline: none; }
.aa-select-search input:focus { border-color: #2F54EB; }
.aa-select-group-label { padding: 6px 12px; font-size: 12px; color: #8C8C8C; }
.aa-select-option {
display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 32px; cursor: pointer;
font-size: 14px; color: #262626; box-sizing: border-box;
}
.aa-select-option:hover { background: #F5F7FA; }
.aa-select-option.is-selected { background: #F0F5FF; color: #2F54EB; }
.aa-select-option .check { visibility: hidden; margin-left: auto; }
.aa-select-option.is-selected .check { visibility: visible; }
.aa-select-option.is-disabled { color: #BFBFBF; cursor: not-allowed; }
.aa-select-empty { padding: 16px 12px; text-align: center; color: #BFBFBF; font-size: 13px; }
</style>