search-bar.vue
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<template>
<div class="search-slider">
<div class="search-block">
<input-search ref="search" class="search" v-model="keyword" :loading="product.isSearching" @enter="enter"></input-search>
<button class="btn btn-trans btn-cancel" @click="cancel">取消</button>
</div>
<div class="search-content" v-show="extend">
<div class="search-list" v-if="!searchEmpty">
<ul>
<li
v-for="item in searchList"
:key="item.keyword"
@click="toSearch(item.keyword)">
{{item.keyword}}
<i class="icon icon-right"></i>
</li>
</ul>
</div>
<div class="search-tip" v-else>
<p class="chg">
未找到相关商品
</p>
<p class="eng">
Unfortunately there are no results
matching your request.
</p>
</div>
</div>
</div>
</template>
<script>
import InputSearch from '../input/input-search';
import {
FETCH_SEARCH_REQUEST
} from 'store/product/types';
import {mapState} from 'vuex';
import {debounce} from 'common/utils';
export default {
name: 'SearchBar',
props: {
full: Boolean,
value: String
},
data() {
return {
searchList: [],
searchEmpty: false
};
},
computed: {
...mapState(['product']),
extend() {
if (this.full) {
return true;
}
return this.searchEmpty || this.searchList.length;
},
keyword: {
get() {
return this.value;
},
set(val) {
this.$emit('input', val);
if (val) {
this.searchDebounce(val);
} else {
this.clear();
}
}
}
},
created() {
this.searchDebounce = debounce(300, this.search);
},
methods: {
cancel() {
this.$emit('cancel');
},
clear() {
this.searchEmpty = false;
this.searchList = [];
},
enter() {
this.toSearch(this.value);
},
toSearch(searchKey) {
this.clear();
this.$router.push(`/channel/search?query=${searchKey}`);
},
focus() {
this.$refs.search.focus();
},
async search(val) {
const result = await this.$store.dispatch(FETCH_SEARCH_REQUEST, {keyword: val});
if (result && this.value) {
if (result.data.length) {
this.searchEmpty = false;
this.searchList = result.data;
} else {
this.searchEmpty = true;
}
}
}
},
components: {InputSearch}
};
</script>
<style lang="scss">
.search-slider {
.search-block {
position: relative;
width: 100%;
height: 90px;
padding: 14px 20px;
background-color: #fff;
display: flex;
border-bottom: solid 1px #eee;
z-index: 999;
.search {
flex: 1;
}
.btn-cancel {
width: 125px;
padding-right: 0px;
}
}
.search-content {
position: absolute;
top: 90px;
left: 0;
right: 0;
bottom: 0;
z-index: 998;
background: rgba(255, 255, 255, 0.9);
.search-tip {
width: 358px;
height: 380px;
margin: 380px auto;
padding-top: 225px;
background: url('~statics/img/search-tip.png') no-repeat;
background-repeat: no-repeat;
background-position: top center;
background-size: 205px 201px;
color: #b0b0b0;
text-align: center;
.chg {
font-size: 34px;
}
.eng {
font-size: 12PX;
}
}
.search-list {
li {
width: 100%;
line-height: 80px;
padding: 0 30px;
font-size: 30px;
border-bottom: solid 1px #eee;
background-color: #fff;
i {
float: right;
display: flex;
align-items: center;
justify-content: center;
height: 80px;
}
}
}
}
}
</style>