<template>
<div class="search_select_body">
<el-select
v-model="currentOption"
placeholder="请选择"
popper-class="select_box"
:multiple="multipleOpt"
@change="changeSelect"
@visible-change="showOrHidden"
@remove-tag="removeTag"
>
<div class="search_input_box">
<el-input v-model="searchKey" placeholder="请输入搜索内容" @input="confirmSearch" />
</div>
<el-option v-for="(item, index) in memberOption" :key="index" :label="item.name" :value="item.id"
><span v-if="item.id" class="diy_check"></span>{{ item.displayname }}</el-option
>
</el-select>
</div>
</template>
<script lang="ts">
import { defineComponent, toRefs, reactive, computed, toRef, ref } from 'vue';
interface Idata {
optionData: any[];
multipleOpt: boolean;
}
interface Ioption {
name: string;
id: string;
}
export default defineComponent({
props: {
optionData: {
type: Array,
default() {
return [];
},
require: true,
},
multipleOpt: {
type: Boolean,
default: true,
require: true,
},
},
emits: ['deliverVal', 'deliverKeyword'],
setup(props: Idata, context) {
const state = reactive({
searchKey: '',
});
let currentOption: any = ref([]);
const confirmSearch = (val: string | number) => {
if (val !== '') {
context.emit('deliverKeyword', val.toString());
}
};
const { optionData, multipleOpt } = toRefs(props);
if (!multipleOpt.value) {
currentOption.value = '';
}
const memberOption = computed(() => {
let searchOption = [] as Ioption[];
if (optionData.value.length) {
optionData.value.forEach((item: any) => {
searchOption.push({
name: item.displayname,
id: item.id,
});
});
}
return searchOption.length ? searchOption : [''];
});
const changeSelect = (val: string[] | string) => {
if (memberOption.value.length == 1 && memberOption.value[0] == '') {
currentOption.value = multipleOpt.value ? [] : '';
}
};
const showOrHidden = (val: boolean) => {
if (!val) {
if (multipleOpt.value) {
multiChangeVal();
} else {
context.emit('deliverVal', currentOption.value);
}
}
};
const removeTag = () => {
multiChangeVal();
};
const multiChangeVal = () => {
let result = [] as any[];
memberOption.value.forEach((item: any) => {
currentOption.value.forEach((items: string) => {
if (items === item.id) {
result.push({
name: item.displayname,
id: item.id,
});
}
});
});
context.emit('deliverVal', result);
};
return {
confirmSearch,
...toRefs(state),
memberOption,
changeSelect,
showOrHidden,
currentOption,
removeTag,
};
},
});
</script>
<style scoped lang="less">
.search_select_body {
position: relative;
.search_input_box {
position: absolute;
top: 44px;
left: 0;
z-index: 3000;
}
}
</style>
|