vue3中使用antd 4.0 select组件可选择也可输入可搜索
<template>
<Select
v-model:value="changeReason"
:search-value="searchReason"
:options="reasonList"
:show-search="true"
:filter-option="filterOptions"
@input-key-down="keyDownFn"
@search="searchReasonFn"
@change="changeReasonFn"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const changeReason = ref();
const searchReason = ref();
const reasonList = [
{label: '喜喜', value: 'xixi' },
{label: '哈哈', value: 'haha'}
]
const filterOptions = (v, o) => {
return o.value.indexOf(v) >= 0;
};
let canClear = false;
function keyDownFn(e) {
canClear = false;
if (e.target.value.length == 1 && e.key == 'Backspace') {
canClear = true;
}
if (e.target.value.length == 1 && e.key == 'Delete') {
canClear = true;
}
}
function searchReasonFn(v) {
if (!v) {
searchReason.value = canClear ? v : searchReason.value;
changeReason.value = canClear ? v : changeReason.value;
return;
}
let data = reasonList.value?.find((i) => i.label == v);
changeReason.value = data ? data.label : undefined;
searchReason.value = v;
}
function changeReasonFn() {
searchReason.value = undefined;
}
</script>