Parcourir la source

菜单搜索支持键盘选择&悬浮主题背景

RuoYi il y a 11 mois
Parent
commit
b80932ceb4

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 0
ruoyi-ui/src/assets/icons/svg/enter.svg


+ 35 - 3
ruoyi-ui/src/components/HeaderSearch/index.vue

@@ -16,11 +16,14 @@
16
         prefix-icon="el-icon-search"
16
         prefix-icon="el-icon-search"
17
         placeholder="菜单搜索,支持标题、URL模糊查询"
17
         placeholder="菜单搜索,支持标题、URL模糊查询"
18
         clearable
18
         clearable
19
+        @keyup.enter.native="selectActiveResult"
20
+        @keydown.up.native="navigateResult('up')"
21
+        @keydown.down.native="navigateResult('down')"
19
       >
22
       >
20
       </el-input>
23
       </el-input>
21
       <el-scrollbar wrap-class="right-scrollbar-wrapper">
24
       <el-scrollbar wrap-class="right-scrollbar-wrapper">
22
         <div class="result-wrap">
25
         <div class="result-wrap">
23
-          <div class="search-item" v-for="item in options" :key="item.path">
26
+          <div class="search-item" v-for="(item, index) in options" :key="item.path" :style="activeStyle(index)" @mouseenter="activeIndex = index" @mouseleave="activeIndex = -1">
24
             <div class="left">
27
             <div class="left">
25
               <svg-icon class="menu-icon" :icon-class="item.icon" />
28
               <svg-icon class="menu-icon" :icon-class="item.icon" />
26
             </div>
29
             </div>
@@ -32,6 +35,7 @@
32
                 {{ item.path }}
35
                 {{ item.path }}
33
               </div>
36
               </div>
34
             </div>
37
             </div>
38
+            <svg-icon icon-class="enter" v-show="index === activeIndex"/>
35
           </div>
39
           </div>
36
        </div>
40
        </div>
37
       </el-scrollbar>
41
       </el-scrollbar>
@@ -51,11 +55,15 @@ export default {
51
       search: '',
55
       search: '',
52
       options: [],
56
       options: [],
53
       searchPool: [],
57
       searchPool: [],
58
+      activeIndex: -1,
54
       show: false,
59
       show: false,
55
       fuse: undefined
60
       fuse: undefined
56
     }
61
     }
57
   },
62
   },
58
   computed: {
63
   computed: {
64
+    theme() {
65
+      return this.$store.state.settings.theme
66
+    },
59
     routes() {
67
     routes() {
60
       return this.$store.getters.defaultRoutes
68
       return this.$store.getters.defaultRoutes
61
     }
69
     }
@@ -84,6 +92,7 @@ export default {
84
       this.search = ''
92
       this.search = ''
85
       this.options = []
93
       this.options = []
86
       this.show = false
94
       this.show = false
95
+      this.activeIndex = -1
87
     },
96
     },
88
     change(val) {
97
     change(val) {
89
       const path = val.path
98
       const path = val.path
@@ -162,11 +171,31 @@ export default {
162
       return res
171
       return res
163
     },
172
     },
164
     querySearch(query) {
173
     querySearch(query) {
174
+      this.activeIndex = -1
165
       if (query !== '') {
175
       if (query !== '') {
166
         this.options = this.fuse.search(query).map((item) => item.item) ?? this.searchPool
176
         this.options = this.fuse.search(query).map((item) => item.item) ?? this.searchPool
167
       } else {
177
       } else {
168
         this.options = this.searchPool
178
         this.options = this.searchPool
169
       }
179
       }
180
+    },
181
+    activeStyle(index) {
182
+      if (index !== this.activeIndex) return {}
183
+      return {
184
+        "background-color": this.theme,
185
+        "color": "#fff"
186
+      }
187
+    },
188
+    navigateResult(direction) {
189
+      if (direction === "up") {
190
+        this.activeIndex = this.activeIndex <= 0 ? this.options.length - 1 : this.activeIndex - 1
191
+      } else if (direction === "down") {
192
+        this.activeIndex = this.activeIndex >= this.options.length - 1 ? 0 : this.activeIndex + 1
193
+      }
194
+    },
195
+    selectActiveResult() {
196
+      if (this.options.length > 0 && this.activeIndex >= 0) {
197
+        this.change(this.options[this.activeIndex])
198
+      }
170
     }
199
     }
171
   }
200
   }
172
 }
201
 }
@@ -189,11 +218,13 @@ export default {
189
 
218
 
190
 .result-wrap {
219
 .result-wrap {
191
   height: 280px;
220
   height: 280px;
192
-  margin: 12px 0;
221
+  margin: 6px 0;
193
 
222
 
194
   .search-item {
223
   .search-item {
195
     display: flex;
224
     display: flex;
196
     height: 48px;
225
     height: 48px;
226
+    align-items: center;
227
+    padding-right: 10px;
197
 
228
 
198
     .left {
229
     .left {
199
       width: 60px;
230
       width: 60px;
@@ -202,16 +233,17 @@ export default {
202
       .menu-icon {
233
       .menu-icon {
203
         width: 18px;
234
         width: 18px;
204
         height: 18px;
235
         height: 18px;
205
-        margin-top: 5px;
206
       }
236
       }
207
     }
237
     }
208
 
238
 
209
     .search-info {
239
     .search-info {
210
       padding-left: 5px;
240
       padding-left: 5px;
241
+      margin-top: 10px;
211
       width: 100%;
242
       width: 100%;
212
       display: flex;
243
       display: flex;
213
       flex-direction: column;
244
       flex-direction: column;
214
       justify-content: flex-start;
245
       justify-content: flex-start;
246
+      flex: 1;
215
 
247
 
216
       .menu-title,
248
       .menu-title,
217
       .menu-path {
249
       .menu-path {