提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<ElDialog v-if="isModal" v-model="showSearch" :show-close="false" title="菜单搜索"> |
|
3 |
<el-select |
|
4 |
filterable |
|
5 |
:reserve-keyword="false" |
|
6 |
remote |
|
7 |
placeholder="请输入菜单内容" |
|
8 |
:remote-method="remoteMethod" |
|
9 |
style="width: 100%" |
|
10 |
@change="handleChange" |
|
11 |
> |
|
12 |
<el-option |
|
13 |
v-for="item in options" |
|
14 |
:key="item.value" |
|
15 |
:label="item.label" |
|
16 |
:value="item.value" |
|
17 |
/> |
|
18 |
</el-select> |
|
19 |
</ElDialog> |
|
20 |
<div v-else class="custom-hover" @click.stop="showTopSearch = !showTopSearch"> |
|
21 |
<Icon icon="ep:search" /> |
|
22 |
<el-select |
9259c2
|
23 |
@click.stop |
820397
|
24 |
filterable |
H |
25 |
:reserve-keyword="false" |
|
26 |
remote |
|
27 |
placeholder="请输入菜单内容" |
|
28 |
:remote-method="remoteMethod" |
|
29 |
class="overflow-hidden transition-all-600" |
|
30 |
:class="showTopSearch ? '!w-220px ml2' : '!w-0'" |
|
31 |
@change="handleChange" |
|
32 |
> |
|
33 |
<el-option |
|
34 |
v-for="item in options" |
|
35 |
:key="item.value" |
|
36 |
:label="item.label" |
|
37 |
:value="item.value" |
|
38 |
/> |
|
39 |
</el-select> |
|
40 |
</div> |
|
41 |
</template> |
|
42 |
|
|
43 |
<script lang="ts" setup> |
|
44 |
defineProps({ |
|
45 |
isModal: { |
|
46 |
type: Boolean, |
|
47 |
default: true |
|
48 |
} |
|
49 |
}) |
|
50 |
|
|
51 |
const router = useRouter() // 路由对象 |
|
52 |
const showSearch = ref(false) // 是否显示弹框 |
|
53 |
const showTopSearch = ref(false) // 是否显示顶部搜索框 |
|
54 |
const value: Ref = ref('') // 用户输入的值 |
|
55 |
|
|
56 |
const routers = router.getRoutes() // 路由对象 |
|
57 |
const options = computed(() => { |
|
58 |
// 提示选项 |
|
59 |
if (!value.value) { |
|
60 |
return [] |
|
61 |
} |
|
62 |
const list = routers.filter((item: any) => { |
|
63 |
if (item.meta.title?.indexOf(value.value) > -1 || item.path.indexOf(value.value) > -1) { |
|
64 |
return true |
|
65 |
} |
|
66 |
}) |
|
67 |
return list.map((item) => { |
|
68 |
return { |
|
69 |
label: `${item.meta.title}${item.path}`, |
|
70 |
value: item.path |
|
71 |
} |
|
72 |
}) |
|
73 |
}) |
|
74 |
|
|
75 |
function remoteMethod(data) { |
|
76 |
// 这里可以执行相应的操作(例如打开搜索框等) |
|
77 |
value.value = data |
|
78 |
} |
|
79 |
|
|
80 |
function handleChange(path) { |
|
81 |
router.push({ path }) |
|
82 |
hiddenTopSearch() |
|
83 |
} |
|
84 |
|
|
85 |
function hiddenTopSearch() { |
|
86 |
showTopSearch.value = false |
|
87 |
} |
|
88 |
|
|
89 |
onMounted(() => { |
|
90 |
window.addEventListener('keydown', listenKey) |
|
91 |
window.addEventListener('click', hiddenTopSearch) |
|
92 |
}) |
|
93 |
|
|
94 |
onUnmounted(() => { |
|
95 |
window.removeEventListener('keydown', listenKey) |
|
96 |
window.removeEventListener('click', hiddenTopSearch) |
|
97 |
}) |
|
98 |
|
|
99 |
// 监听 ctrl + k |
|
100 |
function listenKey(event) { |
|
101 |
if ((event.ctrlKey || event.metaKey) && event.key === 'k') { |
|
102 |
showSearch.value = !showSearch.value |
|
103 |
// 这里可以执行相应的操作(例如打开搜索框等) |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
defineExpose({ |
|
108 |
openSearch: () => { |
|
109 |
showSearch.value = true |
|
110 |
} |
|
111 |
}) |
|
112 |
</script> |