Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-21 11:29:20 +08:00

fix: 修复搜索菜单列表滚动 BUG (#105)

This commit is contained in:
ClariS 2023-08-14 16:00:12 +08:00 committed by GitHub
parent e32e3226d6
commit f12f33014e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -26,9 +26,11 @@ const inputRef = ref<HTMLInputElement | null>(null)
const scrollbarRef = ref<InstanceType<typeof ElScrollbar> | null>(null)
const searchResultRef = ref<InstanceType<typeof SearchResult> | null>(null)
const keyword = ref("")
const keyword = ref<string>("")
const resultList = shallowRef<RouteRecordRaw[]>([])
const activeRouteName = ref<RouteRecordName>("")
/** 是否按下了上键或下键(用于解决和 mouseenter 事件的冲突) */
const isPressUpOrDown = ref<boolean>(false)
/** 控制搜索对话框宽度 */
const modalWidth = computed(() => (appStore.device === DeviceEnum.Mobile ? "80vw" : "40vw"))
@ -76,13 +78,15 @@ const handleClose = () => {
/** 根据下标位置进行滚动 */
const scrollTo = (index: number) => {
const scrollTop = searchResultRef.value?.getScrollTop(index)
if (!searchResultRef.value) return
const scrollTop = searchResultRef.value.getScrollTop(index)
// el-scrollbar
scrollTop && scrollbarRef.value?.setScrollTop(scrollTop)
scrollbarRef.value?.setScrollTop(scrollTop)
}
/** 键盘上键 */
const handleUp = () => {
isPressUpOrDown.value = true
const { length } = resultList.value
if (length === 0) return
const index = resultList.value.findIndex((item) => item.name === activeRouteName.value)
@ -98,6 +102,7 @@ const handleUp = () => {
/** 键盘下键 */
const handleDown = () => {
isPressUpOrDown.value = true
const { length } = resultList.value
if (length === 0) return
const index = resultList.value.findIndex((item) => item.name === activeRouteName.value)
@ -118,6 +123,11 @@ const handleEnter = () => {
router.push({ name: activeRouteName.value })
handleClose()
}
/** 释放上键或下键 */
const handleReleaseUpOrDown = () => {
isPressUpOrDown.value = false
}
</script>
<template>
@ -128,6 +138,7 @@ const handleEnter = () => {
@keydown.up="handleUp"
@keydown.down="handleDown"
@keydown.enter="handleEnter"
@keyup.up.down="handleReleaseUpOrDown"
:before-close="handleClose"
:width="modalWidth"
top="5vh"
@ -142,8 +153,14 @@ const handleEnter = () => {
<el-empty v-if="resultList.length === 0" description="暂无搜索结果" :image-size="100" />
<template v-else>
<p>搜索结果</p>
<el-scrollbar ref="scrollbarRef" max-height="40vh">
<SearchResult ref="searchResultRef" v-model="activeRouteName" :list="resultList" @click="handleEnter" />
<el-scrollbar ref="scrollbarRef" max-height="40vh" always>
<SearchResult
ref="searchResultRef"
v-model="activeRouteName"
:list="resultList"
:isPressUpOrDown="isPressUpOrDown"
@click="handleEnter"
/>
</el-scrollbar>
</template>
<template #footer>

View File

@ -5,6 +5,7 @@ import { type RouteRecordName, type RouteRecordRaw } from "vue-router"
interface Props {
modelValue: RouteRecordName
list: RouteRecordRaw[]
isPressUpOrDown: boolean
}
const props = defineProps<Props>()
@ -36,6 +37,8 @@ const itemStyle = (item: RouteRecordRaw) => {
/** 鼠标移入 */
const handleMouseenter = (item: RouteRecordRaw) => {
// mouseenter
if (props.isPressUpOrDown) return
activeRouteName.value = item.name!
}