Skip to content

Commit dca3efd

Browse files
committed
perf: 全局搜索框打开后自动聚焦输入框
1 parent ee368d2 commit dca3efd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/effects/layouts/src/widgets/global-search/global-search.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import type { MenuRecordRaw } from '@vben/types';
33
4-
import { onMounted, onUnmounted, ref, watch } from 'vue';
4+
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
55
66
import {
77
ArrowDown,
@@ -40,6 +40,7 @@ const props = withDefaults(
4040
4141
const [open, toggleOpen] = useToggle();
4242
const keyword = ref('');
43+
const searchInputRef = ref<HTMLInputElement>();
4344
4445
function handleClose() {
4546
open.value = false;
@@ -54,6 +55,12 @@ whenever(cmd, () => {
5455
}
5556
});
5657
58+
whenever(open, () => {
59+
nextTick(() => {
60+
searchInputRef.value?.focus();
61+
});
62+
});
63+
5764
const preventDefaultBrowserSearchHotKey = (event: KeyboardEvent) => {
5865
if (event.key.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey)) {
5966
event.preventDefault();
@@ -115,6 +122,7 @@ onMounted(() => {
115122
>
116123
<Search class="text-muted-foreground size-4" />
117124
<input
125+
ref="searchInputRef"
118126
v-model="keyword"
119127
:placeholder="$t('widgets.search.searchNavigate')"
120128
class="ring-none placeholder:text-muted-foreground w-[80%] rounded-md border border-none bg-transparent p-2 pl-0 text-sm outline-none ring-0 ring-offset-transparent focus-visible:ring-transparent"

0 commit comments

Comments
 (0)