<template>
<el-popover
ref="popover"
placement="bottom"
width="400"
trigger="manual"
v-model:visible="isPopoverVisible"
@show="onPopoverShow"
>
<template #reference>
<el-input
v-model="searchText"
:placeholder="placeholder"
@keyup.enter="onEnter"
@input="onInput"
@focus="onFocus"
@blur="onBlur"
>
<template #suffix>
<i v-if="searchText" class="el-input__icon el-icon-circle-close" @click="clearSearch"></i>
<i v-else class="el-input__icon el-icon-search"></i>
</template>
</el-input>
</template>
<el-scrollbar height="240px">
<div class="history-search-list">
<div
class="history-search-item"
v-for="(item, index) in historyList"
:key="index"
@click="selectHistory(item)"
>
{{ item }}
</div>
</div>
</el-scrollbar>
</el-popover>
</template>
<script>
import { ref } from 'vue';
import { ElPopover, ElInput, ElScrollbar } from 'element-plus';
export default {
components: {
ElPopover,
ElInput,
ElScrollbar
},
props: {
placeholder: {
type: String,
default: '请输入搜索内容'
}
},
setup(props, { emit }) {
const searchText = ref('');
const isPopoverVisible = ref(false);
const historyList = ref(JSON.parse(localStorage.getItem('historyList')) || []);
const onEnter = () => {
emit('update:searchText', searchText.value.trim());
addToHistory();
isPopoverVisible.value = false;
};
const onInput = () => {
if (searchText.value.trim()) {
isPopoverVisible.value = true;
}
};
const onFocus = () => {
isPopoverVisible.value = true;
};
const onBlur = () => {
setTimeout(() => {
isPopoverVisible.value = false;
}, 200);
};
const clearSearch = () => {
searchText.value = '';
emit('update:searchText', '');
};
const selectHistory = (item) => {
searchText.value = item;
emit('update:searchText', item);
isPopoverVisible.value = false;
};
const onPopoverShow = () => {
if (historyList.value.includes(searchText.value.trim())) {
historyList.value = historyList.value.filter(i
评论已关闭