浏览代码

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

RuoYi 11 月之前
父节点
当前提交
b80932ceb4
共有 2 个文件被更改,包括 36 次插入3 次删除
  1. 1 0
      ruoyi-ui/src/assets/icons/svg/enter.svg
  2. 35 3
      ruoyi-ui/src/components/HeaderSearch/index.vue

文件差异内容过多而无法显示
+ 1 - 0
ruoyi-ui/src/assets/icons/svg/enter.svg


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

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