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,6 +7,7 @@
7 <Input 7 <Input
8 :class="`${prefixCls}-input`" 8 :class="`${prefixCls}-input`"
9 :placeholder="t('common.searchText')" 9 :placeholder="t('common.searchText')"
  10 + ref="inputRef"
10 allow-clear 11 allow-clear
11 @change="handleSearch" 12 @change="handleSearch"
12 > 13 >
@@ -57,7 +58,7 @@ @@ -57,7 +58,7 @@
57 </Teleport> 58 </Teleport>
58 </template> 59 </template>
59 <script lang="ts"> 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 import { SearchOutlined } from '@ant-design/icons-vue'; 63 import { SearchOutlined } from '@ant-design/icons-vue';
63 import { Input } from 'ant-design-vue'; 64 import { Input } from 'ant-design-vue';
@@ -90,15 +91,10 @@ @@ -90,15 +91,10 @@
90 const { t } = useI18n(); 91 const { t } = useI18n();
91 const [refs, setRefs] = useRefs(); 92 const [refs, setRefs] = useRefs();
92 const { getIsMobile } = useAppInject(); 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 const getIsNotData = computed(() => { 99 const getIsNotData = computed(() => {
104 return !keyword || unref(searchResult).length === 0; 100 return !keyword || unref(searchResult).length === 0;
@@ -118,6 +114,16 @@ @@ -118,6 +114,16 @@
118 emit('close'); 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 return { 127 return {
122 t, 128 t,
123 prefixCls, 129 prefixCls,
@@ -131,6 +137,7 @@ @@ -131,6 +137,7 @@
131 scrollWrap, 137 scrollWrap,
132 handleMouseenter, 138 handleMouseenter,
133 handleClose, 139 handleClose,
  140 + inputRef,
134 }; 141 };
135 }, 142 },
136 }); 143 });