@@ -13,6 +13,8 @@ export default {
1313 menus: undefined ,
1414 config: undefined ,
1515 hideSearchBar: true ,
16+ searchBarIsFocused: false ,
17+ searchBarInputKeyupTimeout: null ,
1618 }
1719 },
1820 computed: {
@@ -45,23 +47,31 @@ export default {
4547 }
4648
4749 // 如果不是显示/隐藏操作,并且还未显示检索框,先按显示操作处理
48- if (message .key !== ' show- hide' && this .hideSearchBar ) {
50+ if (! message .key . includes ( ' hide' ) && this .hideSearchBar ) {
4951 message = { key: ' show-hide' }
5052 }
5153
5254 try {
5355 if (message .key === ' show-hide' ) { // 显示/隐藏
54- this .hideSearchBar = message .hideSearchBar != null ? message .hideSearchBar : ! this .hideSearchBar
56+ const hide = message .hideSearchBar != null ? message .hideSearchBar : ! this .hideSearchBar
57+
58+ // 如果为隐藏操作,但SearchBar未隐藏且未获取焦点,则获取焦点
59+ if (hide && ! this .hideSearchBar && ! this .searchBarIsFocused ) {
60+ this .doSearchBarInputFocus ()
61+ return
62+ }
63+
64+ this .hideSearchBar = hide
5565
5666 // 显示后,获取输入框焦点
5767 if (! this .hideSearchBar ) {
58- setTimeout (() => {
59- try {
60- this .$refs .searchBar .$el .querySelector (' input' ).focus ()
61- } catch {
62- }
63- }, 100 )
68+ this .doSearchBarInputFocus ()
69+ } else {
70+ this .searchBarIsFocused = false
6471 }
72+ } else if (message .key === ' hide' ) { // 隐藏
73+ this .hideSearchBar = true
74+ this .searchBarIsFocused = false
6575 } else if (message .key === ' next' ) { // 下一项
6676 this .$refs .searchBar .next ()
6777 } else if (message .key === ' previous' ) { // 上一项
@@ -70,9 +80,48 @@ export default {
7080 } catch (e) {
7181 console .error (' 操作SearchBar出现异常:' , e)
7282 }
83+
84+ const input = this .getSearchBarInput ()
85+ if (input) {
86+ input .addEventListener (' focus' , this .onSearchBarInputFocus )
87+ input .addEventListener (' blur' , this .onSearchBarInputBlur )
88+ input .addEventListener (' keydown' , this .onSearchBarInputKeydown )
89+ input .addEventListener (' keyup' , this .onSearchBarInputKeyup )
90+ }
7391 })
7492 },
7593 methods: {
94+ getSearchBarInput () {
95+ return this .$refs .searchBar .$el .querySelector (' input[type=text]' )
96+ },
97+ onSearchBarInputFocus () {
98+ this .searchBarIsFocused = true
99+ },
100+ onSearchBarInputBlur () {
101+ this .searchBarIsFocused = false
102+ },
103+ onSearchBarInputKeydown () {
104+ clearTimeout (this .searchBarInputKeyupTimeout )
105+ },
106+ onSearchBarInputKeyup (e ) {
107+ if (! this .$refs .searchBar || e .key === ' Enter' || e .key === ' F3' ) {
108+ return
109+ }
110+ clearTimeout (this .searchBarInputKeyupTimeout )
111+ this .searchBarInputKeyupTimeout = setTimeout (() => {
112+ // 连续调用以下两个方法,为了获取检索结果中的第一项
113+ this .$refs .searchBar .next ()
114+ this .$refs .searchBar .previous ()
115+ }, 150 )
116+ },
117+ doSearchBarInputFocus () {
118+ setTimeout (() => {
119+ const input = this .getSearchBarInput ()
120+ if (input) {
121+ input .focus ()
122+ }
123+ }, 100 )
124+ },
76125 titleClick (item ) {
77126 console .log (' title click:' , item)
78127 },
0 commit comments