-
Notifications
You must be signed in to change notification settings - Fork 10
/
Search.vue
146 lines (146 loc) · 3.59 KB
/
Search.vue
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
<template>
<div class='wt-search'>
<div class="placeholder" v-show="!isblur" @click="handle" >
<i class="icon-search"></i>
<span>{{placeholder}}</span>
</div>
<i class="icon-search"></i>
<form action="" @submit="submit">
<input type="search" ref="search" v-model="val"/>
</form>
<i class="icon-close-fill" @click="clear"></i>
<div class="btn" @click="cancel">取消</div>
</div>
</template>
<script>
import { getCurrentInstance, ref } from 'vue';
export default {
props: {
placeholder: {
type: String,
default: () => {
return '搜索';
}
},
disable: {
type: String,
default: () => {
return undefined;
}
}
},
setup (props, {emit}) {
const { ctx } = getCurrentInstance()
const val = ref("")
const isblur = ref(false)
const handle =()=> {
if (props.disable !== undefined) return;
isblur.value = true;
ctx.$refs.search.focus();
}
const submit =(event) =>{
event.preventDefault();
event.stopPropagation();
emit('submit', val.value);
return false;
}
// 清理输入内容
const clear = () =>{
val.value = '';
}
// 取消
const cancel =()=> {
isblur.value = false;
val.value = '';
emit('cancel');
}
// 暴露给模板
return {
val,
isblur,
handle,
submit,
clear,
cancel
};
}
};
</script>
<style lang='less' rel='stylesheet/less' scoped>
.wt-search {
height: 1.5rem;
position: relative;
padding: 0.5rem;
background: #EFEFF4;
display: flex;
.icon-search {
font-size: 0.8rem;
width: 1rem;
line-height: 1.5rem;
color: #999;
margin-left: 0.2rem;
padding: 0 0.2rem;
background: #fff;
border-top-left-radius: 0.4rem;
border-bottom-left-radius: 0.4rem;
}
.icon-close-fill {
background: #fff;
border-top-right-radius: 0.4rem;
border-bottom-right-radius: 0.4rem;
width: 1.5rem;
line-height: 1.5rem;
color: #cacaca;
text-align: center;
}
.btn {
font-size: 0.7rem;
min-width: 2rem;
line-height: 1.5rem;
text-align: center;
}
form {
background: #fff;
border: 0;
line-height: 1.5rem;
width: 100%;
display: block;
}
input {
outline: none;
-webkit-appearance: none;
background: #fff;
border: 0;
height: 1.5rem;
width: 100%;
display: block;
&::-webkit-search-cancel-button{
display: none; // 默认边框和删除按钮
}
}
.placeholder {
position: absolute;
width: 100%;
width: calc(100% - 1rem);
top: 0.5rem;
font-size: 0.8rem;
color: #999;
box-sizing: border-box;
display: flex;
justify-content: center;
height: 1.5rem;
align-items: center;
background: #fff;
left: 0.5rem;
border-radius: 0.4rem;
i {
display: inline-block;
margin-right: 5px;
}
span {
display: inline-block;
font-size: 0.7rem;
}
}
}
</style>