Commit 1ae636296df2cf99e8a777f053c539c50e6ad49a

Authored by 无木
1 parent b218f10e

feat(app-search): auto focus on show

菜单搜索组件在显示后input自动获得焦点
src/components/Application/src/search/AppSearchModal.vue
... ... @@ -7,6 +7,7 @@
7 7 <Input
8 8 :class="`${prefixCls}-input`"
9 9 :placeholder="t('common.searchText')"
  10 + ref="inputRef"
10 11 allow-clear
11 12 @change="handleSearch"
12 13 >
... ... @@ -57,7 +58,7 @@
57 58 </Teleport>
58 59 </template>
59 60 <script lang="ts">
60   - import { defineComponent, computed, unref, ref } from 'vue';
  61 + import { defineComponent, computed, unref, ref, watch, nextTick } from 'vue';
61 62  
62 63 import { SearchOutlined } from '@ant-design/icons-vue';
63 64 import { Input } from 'ant-design-vue';
... ... @@ -90,15 +91,10 @@
90 91 const { t } = useI18n();
91 92 const [refs, setRefs] = useRefs();
92 93 const { getIsMobile } = useAppInject();
  94 + const inputRef = ref<Nullable<HTMLElement>>(null);
93 95  
94   - const {
95   - handleSearch,
96   - searchResult,
97   - keyword,
98   - activeIndex,
99   - handleEnter,
100   - handleMouseenter,
101   - } = useMenuSearch(refs, scrollWrap, emit);
  96 + const { handleSearch, searchResult, keyword, activeIndex, handleEnter, handleMouseenter } =
  97 + useMenuSearch(refs, scrollWrap, emit);
102 98  
103 99 const getIsNotData = computed(() => {
104 100 return !keyword || unref(searchResult).length === 0;
... ... @@ -118,6 +114,16 @@
118 114 emit('close');
119 115 }
120 116  
  117 + watch(
  118 + () => _.visible,
  119 + (v: boolean) => {
  120 + v &&
  121 + nextTick(() => {
  122 + unref(inputRef)?.focus();
  123 + });
  124 + }
  125 + );
  126 +
121 127 return {
122 128 t,
123 129 prefixCls,
... ... @@ -131,6 +137,7 @@
131 137 scrollWrap,
132 138 handleMouseenter,
133 139 handleClose,
  140 + inputRef,
134 141 };
135 142 },
136 143 });
... ...