Skip to content

Commit 4f93959

Browse files
committed
feat(sidebar): pull out search component
re #384
1 parent a10a869 commit 4f93959

File tree

4 files changed

+133
-121
lines changed

4 files changed

+133
-121
lines changed

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"axios": "~0.18.0",
2020
"core-js": "^3.6.5",
2121
"dayjs": "^1.10.4",
22-
"element-plus": "^1.0.1-beta.19",
22+
"element-plus": "^1.0.2-beta.36",
2323
"event-source-polyfill": "^1.0.7",
2424
"fastscan": "^1.0.4",
2525
"good-storage": "^1.1.0",
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<template>
2+
<div v-if="showSidebarSearch" style="margin-top: 15px">
3+
<div class="search-display" v-if="!showSearchList" @click="toSearch"><i class="el-icon-search"></i></div>
4+
<el-select
5+
clearable
6+
filterable
7+
size="medium"
8+
class="search"
9+
v-model="sidebar"
10+
ref="searchInput"
11+
v-if="showSearchList"
12+
:filter-method="search"
13+
@change="handleChange"
14+
placeholder="请输入关键字"
15+
>
16+
<el-option v-for="item in groups" :key="item.key" :label="item.title" :value="item.path"> </el-option>
17+
</el-select>
18+
</div>
19+
</template>
20+
21+
<script>
22+
import { mapGetters } from 'vuex'
23+
import emitter from 'lin/util/emitter'
24+
25+
import Config from '@/config/index'
26+
27+
export default {
28+
data() {
29+
return {
30+
groups: [],
31+
sidebar: '',
32+
showSearchList: false,
33+
showSidebarSearch: Config.showSidebarSearch,
34+
}
35+
},
36+
computed: {
37+
...mapGetters(['sidebarList']),
38+
},
39+
mounted() {
40+
emitter.on('removeSidebarSearch', () => {
41+
this.showSidebarSearch = false
42+
})
43+
emitter.on('showSidebarSearch', () => {
44+
if (Config.showSidebarSearch) {
45+
this.showSidebarSearch = true
46+
}
47+
})
48+
},
49+
methods: {
50+
handleChange(val) {
51+
this.groups = []
52+
this.sidebar = ''
53+
this.showSearchList = false
54+
this.$router.push(val)
55+
},
56+
toSearch() {
57+
this.showSearchList = true
58+
setTimeout(() => {
59+
this.$refs.searchInput.focus()
60+
}, 200)
61+
},
62+
search(val) {
63+
this.groups = []
64+
65+
// 深度遍历配置树, 摘取叶子节点作为路由部分
66+
function deepTravel(config, fuc) {
67+
if (Array.isArray(config)) {
68+
config.forEach(subConfig => {
69+
deepTravel(subConfig, fuc)
70+
})
71+
} else if (config.children) {
72+
config.children.forEach(subConfig => {
73+
deepTravel(subConfig, fuc)
74+
})
75+
} else {
76+
fuc(config)
77+
}
78+
}
79+
80+
deepTravel(this.sidebarList, viewConfig => {
81+
// 构造舞台view路由
82+
if (viewConfig.title.includes(val)) {
83+
const viewRouter = {}
84+
viewRouter.path = viewConfig.path
85+
viewRouter.title = viewConfig.title
86+
viewRouter.key = Math.random()
87+
this.groups.push(viewRouter)
88+
}
89+
})
90+
},
91+
},
92+
}
93+
</script>
94+
95+
<style lang="scss" scoped>
96+
.search-display {
97+
position: relative;
98+
width: 80%;
99+
margin: 0 auto;
100+
height: 36px;
101+
border-bottom: 1px rgb(185, 190, 195) solid;
102+
cursor: pointer;
103+
104+
.el-icon-search {
105+
position: absolute;
106+
left: 1px;
107+
top: 10px;
108+
color: rgb(185, 190, 195);
109+
}
110+
}
111+
112+
.search {
113+
width: 80%;
114+
}
115+
</style>

src/component/layout/sidebar/sidebar.vue

Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,7 @@
22
<div class="app-sidebar">
33
<logo :elMenuCollapse="elMenuCollapse" />
44
<div style="margin-bottom:50px">
5-
<div v-if="showSidebarSearch" style="margin-top: 15px">
6-
<div class="search-display" v-if="!showSearchList" @click="toSearch"><i class="el-icon-search"></i></div>
7-
<el-select
8-
v-if="showSearchList"
9-
size="medium"
10-
filterable
11-
clearable
12-
:filter-method="search"
13-
v-model="sidebar"
14-
@change="handleChange"
15-
class="search"
16-
placeholder="请输入关键字"
17-
ref="searchInput"
18-
>
19-
<el-option v-for="item in groups" :key="item.key" :label="item.title" :value="item.path"> </el-option>
20-
</el-select>
21-
</div>
5+
<search></search>
226
<el-menu
237
ref="meun"
248
class="el-menu-vertical-demo"
@@ -36,21 +20,13 @@
3620

3721
<script>
3822
import { mapGetters } from 'vuex'
39-
import emitter from 'lin/util/emitter'
40-
import Config from '@/config/index'
41-
import MenuTree from './menu-tree'
23+
4224
import Logo from './logo'
25+
import Search from './search'
26+
import MenuTree from './menu-tree'
4327
4428
export default {
45-
components: { MenuTree, Logo },
46-
data() {
47-
return {
48-
sidebar: '',
49-
groups: [],
50-
showSearchList: false,
51-
showSidebarSearch: Config.showSidebarSearch,
52-
}
53-
},
29+
components: { MenuTree, Logo, Search },
5430
props: {
5531
isPhone: {
5632
type: Boolean,
@@ -61,69 +37,11 @@ export default {
6137
default: false,
6238
},
6339
},
64-
mounted() {
65-
emitter.on('removeSidebarSearch', () => {
66-
this.showSidebarSearch = false
67-
})
68-
emitter.on('showSidebarSearch', () => {
69-
if (Config.showSidebarSearch) {
70-
this.showSidebarSearch = true
71-
}
72-
})
73-
},
74-
methods: {
75-
handleChange(val) {
76-
this.groups = []
77-
this.sidebar = ''
78-
this.showSearchList = false
79-
this.$router.push(val)
80-
},
81-
toSearch() {
82-
this.showSearchList = true
83-
setTimeout(() => {
84-
this.$refs.searchInput.focus()
85-
}, 200)
86-
},
87-
search(val) {
88-
// if (!val) {
89-
// this.showSearchList = false
90-
// return
91-
// }
92-
this.groups = []
93-
94-
// 深度遍历配置树, 摘取叶子节点作为路由部分
95-
function deepTravel(config, fuc) {
96-
if (Array.isArray(config)) {
97-
config.forEach(subConfig => {
98-
deepTravel(subConfig, fuc)
99-
})
100-
} else if (config.children) {
101-
config.children.forEach(subConfig => {
102-
deepTravel(subConfig, fuc)
103-
})
104-
} else {
105-
fuc(config)
106-
}
107-
}
108-
109-
deepTravel(this.sidebarList, viewConfig => {
110-
// 构造舞台view路由
111-
if (viewConfig.title.includes(val)) {
112-
const viewRouter = {}
113-
viewRouter.path = viewConfig.path
114-
viewRouter.title = viewConfig.title
115-
viewRouter.key = Math.random()
116-
this.groups.push(viewRouter)
117-
}
118-
})
119-
},
120-
},
12140
computed: {
12241
elMenuCollapse() {
12342
if (this.isPhone) {
12443
return false
12544
}
126-
12745
return this.isCollapse
12846
},
12947
/**
@@ -151,26 +69,5 @@ export default {
15169
width: 0px;
15270
height: 0px;
15371
}
154-
155-
.search-display {
156-
position: relative;
157-
width: 80%;
158-
margin: 0 auto;
159-
height: 36px;
160-
border-bottom: 1px rgb(185, 190, 195) solid;
161-
cursor: pointer;
162-
163-
.el-icon-search {
164-
position: absolute;
165-
left: 1px;
166-
top: 10px;
167-
color: rgb(185, 190, 195);
168-
}
169-
}
170-
171-
.search {
172-
// margin-top: 20px;
173-
width: 80%;
174-
}
17572
}
17673
</style>

0 commit comments

Comments
 (0)