kun-galgame-vue/src/components/KUNGalgameSearchBox.vue

297 lines
7.2 KiB
Vue
Raw Normal View History

2023-05-31 06:31:51 +00:00
<script setup lang="ts">
import { Icon } from '@iconify/vue'
2023-08-25 13:01:54 +00:00
// 导入防抖函数
import { debounce } from '@/utils/debounce'
2023-08-25 13:50:17 +00:00
import { ref, computed, onBeforeMount } from 'vue'
2023-08-24 15:32:56 +00:00
// 导入用户 store
import { useKUNGalgameHomeStore } from '@/store/modules/home'
import { storeToRefs } from 'pinia'
2023-08-25 13:01:54 +00:00
// 导入设置面板 store
import { useKUNGalgameSettingsStore } from '@/store/modules/settings'
// 使用设置面板的 store
const { showKUNGalgameLanguage } = storeToRefs(useKUNGalgameSettingsStore())
2023-08-25 13:50:17 +00:00
const { keywords, searchHistory, category } = storeToRefs(
useKUNGalgameHomeStore()
)
2023-08-24 15:32:56 +00:00
2023-08-25 13:50:17 +00:00
// 输入框的值
2023-08-24 15:32:56 +00:00
const inputValue = ref('')
2023-08-25 13:50:17 +00:00
// 是否显示搜索历史
2023-08-25 13:01:54 +00:00
const isShowSearchHistory = ref(false)
2023-08-25 13:50:17 +00:00
// 输入框被激活之后的样式
2023-08-25 13:01:54 +00:00
const inputActiveClass = ref({})
2023-08-24 15:32:56 +00:00
2023-08-25 13:50:17 +00:00
// 定义 props这里的作用是告诉输入框在哪个分类中搜索话题
const props = defineProps(['category'])
// 初始化搜索框内容,不然刷新页面 input 内依然有内容
// 将搜索的话题类别赋值,因为搜索框会在三个页面渲染,对应三个分类
onBeforeMount(() => {
keywords.value = ''
category.value = props.category
})
2023-08-24 15:32:56 +00:00
// 定义防抖处理函数
const debouncedSearch = debounce((inputValue: string) => {
2023-08-25 13:01:54 +00:00
keywords.value = inputValue
2023-08-24 15:32:56 +00:00
}, 300) // 300 毫秒的防抖延迟
2023-08-25 13:01:54 +00:00
// 当搜索框 focus 时
const handleInputFocus = () => {
if (searchHistory.value.length !== 0) {
isShowSearchHistory.value = true
}
inputActiveClass.value = {
backgroundColor: 'var(--kungalgame-white)',
}
}
// 当搜索框 blur 时
const handleInputBlur = () => {
// 延迟一段时间隐藏搜索历史,以便点击搜索历史时可以触发填充事件
setTimeout(() => {
isShowSearchHistory.value = false
inputActiveClass.value = {}
}, 100)
}
2023-08-25 13:50:17 +00:00
// 搜索函数逻辑
const search = () => {
debouncedSearch(inputValue.value)
if (!searchHistory.value.includes(inputValue.value)) {
// 仅当数组中没有相同元素时才将元素推入数组
searchHistory.value.push(inputValue.value)
}
}
2023-08-25 13:01:54 +00:00
// 用户点击 enter 时
const handleClickEnter = (event: KeyboardEvent) => {
event.preventDefault()
2023-08-25 13:50:17 +00:00
search()
2023-08-25 13:01:54 +00:00
}
// 点击搜索按钮搜索逻辑
const handleClickSearch = () => {
if (inputValue.value.trim()) {
2023-08-25 13:50:17 +00:00
search()
2023-08-25 13:01:54 +00:00
}
}
// 点击历史记录
const handleClickHistory = (index: number) => {
inputValue.value = searchHistory.value[index]
}
// 清空历史记录
const clearSearchHistory = () => {
searchHistory.value = []
}
// 删除历史记录
const handleDeleteHistory = (historyIndex: number) => {
searchHistory.value.splice(historyIndex, 1)
}
2023-05-31 06:31:51 +00:00
</script>
<template>
<!-- 交互区搜索框 -->
<div class="container">
<!-- 搜索框表单 -->
<form class="search-form">
<!-- 搜索框内容 -->
<div class="content">
<!-- 框体 -->
2023-08-24 15:32:56 +00:00
<input
v-model="inputValue"
type="search"
class="input"
2023-08-25 13:01:54 +00:00
:style="inputActiveClass"
2023-09-07 15:15:48 +00:00
:placeholder="`${$tm('mainPage.header.search')}`"
2023-08-25 13:01:54 +00:00
@focus="handleInputFocus"
@blur="handleInputBlur"
2023-08-24 15:32:56 +00:00
@input="debouncedSearch(inputValue)"
2023-08-25 13:01:54 +00:00
@keydown.enter="handleClickEnter"
2023-08-24 15:32:56 +00:00
/>
2023-05-31 06:31:51 +00:00
</div>
<!-- 搜索框图标 -->
2023-08-25 13:01:54 +00:00
<div class="search-btn" @click="handleClickSearch">
2023-05-31 06:31:51 +00:00
<Icon icon="line-md:search" />
</div>
</form>
<!-- 搜索历史容器 -->
2023-08-25 13:01:54 +00:00
<div v-if="isShowSearchHistory" class="history">
2023-05-31 06:31:51 +00:00
<!-- 搜索历史标题 -->
<div class="title">
2023-09-07 15:15:48 +00:00
<span>{{ $tm('mainPage.header.history') }}</span>
<span @click="clearSearchHistory">
{{ $tm('mainPage.header.clear') }}
</span>
2023-05-31 06:31:51 +00:00
</div>
<!-- 搜索历史 -->
<div class="history-container">
2023-08-25 13:01:54 +00:00
<div
class="single-history"
v-for="(history, index) in searchHistory"
:key="index"
@click="handleClickHistory(index)"
>
<span>{{ history }} </span>
<span @click="handleDeleteHistory(index)"
><Icon class="delete" icon="line-md:close-circle"
/></span>
2023-08-24 11:51:35 +00:00
</div>
2023-05-31 06:31:51 +00:00
</div>
</div>
</div>
</template>
<style lang="scss" scoped>
2023-08-19 10:10:37 +00:00
/* 搜索话题 */
2023-05-31 06:31:51 +00:00
.container {
2023-06-06 08:20:02 +00:00
height: 39px;
2023-05-31 06:31:51 +00:00
width: 1px;
justify-content: center;
align-items: center;
/* 页面缩小不换行 */
white-space: nowrap;
2023-06-05 06:32:08 +00:00
background-color: var(--kungalgame-trans-blue-2);
2023-06-12 15:54:06 +00:00
border: 1px solid var(--kungalgame-blue-4);
2023-05-31 06:31:51 +00:00
flex-grow: 2;
/* 相对于二级菜单定位 */
position: relative;
display: flex;
box-sizing: border-box;
2023-06-06 13:56:18 +00:00
color: var(--kungalgame-font-color-3);
2023-05-31 06:31:51 +00:00
}
/* 搜索框表单 */
.search-form {
display: flex;
2023-06-06 08:20:02 +00:00
height: 39px;
2023-05-31 06:31:51 +00:00
/* 随着页面自增长 */
width: 1px;
flex-grow: 1;
/* 居中 */
justify-content: center;
align-items: center;
}
/* 搜索内容区 */
.content {
width: 100%;
}
/* 框体 */
.input {
padding: 0 15px;
2023-06-06 08:20:02 +00:00
height: 39px;
2023-05-31 06:31:51 +00:00
width: 100%;
/* 搜索时输入的字体大小 */
font-size: 16px;
border: none;
2023-06-05 17:08:04 +00:00
background-color: var(--kungalgame-trans-white-5);
2023-06-06 13:56:18 +00:00
color: var(--kungalgame-font-color-3);
2023-08-25 13:01:54 +00:00
transition: all 0.2s;
2023-05-31 06:31:51 +00:00
&::placeholder {
2023-06-12 15:54:06 +00:00
color: var(--kungalgame-font-color-1);
2023-05-31 06:31:51 +00:00
}
}
/* 搜索按钮 */
.search-btn {
/* 正方形,不缩小 */
2023-06-06 08:20:02 +00:00
height: 39px;
width: 39px;
2023-05-31 06:31:51 +00:00
flex-shrink: 0;
2023-06-05 06:32:08 +00:00
border-left: 1px solid var(--kungalgame-trans-blue-4);
2023-05-31 06:31:51 +00:00
/* 搜索图标居中 */
display: flex;
justify-content: center;
align-items: center;
/* 内边距盒子 */
box-sizing: border-box;
font-size: 18px;
2023-08-25 13:01:54 +00:00
cursor: pointer;
transition: all 0.2s;
2023-05-31 06:31:51 +00:00
&:hover {
2023-08-24 11:51:35 +00:00
background-color: var(--kungalgame-red-1);
2023-05-31 06:31:51 +00:00
}
&:active {
2023-08-24 11:51:35 +00:00
background-color: var(--kungalgame-red-2);
2023-05-31 06:31:51 +00:00
}
}
/* 搜索历史的容器 */
.history {
width: 100%;
/* 相对于 nav 的搜索区域绝对定位 */
position: absolute;
2023-08-24 08:04:34 +00:00
/* 紧话题搜索区定位 */
2023-06-06 08:20:02 +00:00
top: 39px;
2023-05-31 06:31:51 +00:00
left: 0;
flex-direction: column;
2023-06-06 08:20:02 +00:00
background-color: var(--kungalgame-white);
2023-06-05 06:32:08 +00:00
color: var(--kungalgame-font-color-3);
border: 1px solid var(--kungalgame-red-1);
2023-05-31 06:31:51 +00:00
border-radius: 7px;
2023-06-12 17:41:14 +00:00
box-shadow: var(--shadow);
2023-05-31 06:31:51 +00:00
}
/* 搜索历史 title 的文字 */
.title {
display: flex;
margin: 10px;
/* 两个提示文字左右分布 */
justify-content: space-between;
span {
font-size: 14px;
&:nth-child(2) {
cursor: pointer;
2023-06-05 17:08:04 +00:00
border-bottom: 1.5px solid var(--kungalgame-trans-white-5);
2023-05-31 06:31:51 +00:00
&:hover {
2023-06-05 06:32:08 +00:00
border-bottom: 1.5px solid var(--kungalgame-blue-4);
2023-05-31 06:31:51 +00:00
}
}
}
}
/* 搜索历史 TAG 的存放容器 */
.history-container {
display: flex;
flex-direction: column;
/* 单个搜索记录的字体 */
font-size: 13px;
/* 搜索记录左右两侧的空白距离 */
margin: 10px;
2023-08-25 13:01:54 +00:00
}
.single-history {
width: 100%;
display: flex;
justify-content: space-between;
padding: 7px 3px;
margin: 2px 0;
&:hover {
color: var(--kungalgame-blue-4);
.delete {
display: flex;
2023-05-31 06:31:51 +00:00
}
}
2023-08-25 13:01:54 +00:00
span:nth-child(1) {
cursor: default;
2023-05-31 06:31:51 +00:00
position: relative;
display: flex;
overflow: hidden;
}
2023-08-25 13:01:54 +00:00
span:nth-child(2) {
width: 17px;
}
2023-05-31 06:31:51 +00:00
}
/* 删除按钮 */
.delete {
2023-08-24 11:51:35 +00:00
width: 30px;
2023-08-25 13:01:54 +00:00
right: 5px;
2023-08-24 11:51:35 +00:00
font-size: 17px;
2023-05-31 06:31:51 +00:00
position: absolute;
2023-08-24 11:51:35 +00:00
justify-content: center;
align-items: center;
2023-08-25 13:01:54 +00:00
cursor: pointer;
2023-06-05 06:32:08 +00:00
color: var(--kungalgame-font-color-0);
2023-08-24 11:51:35 +00:00
background-color: var(--kungalgame-white);
2023-05-31 06:31:51 +00:00
display: none;
}
</style>