search-bar.vue
2.23 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
<template>
<view class="search-bar">
<view class="search-bar__icon">
<zui-icon name="search"></zui-icon>
</view>
<view v-if="keyboard" class="search-bar__input" @click="onKeyboard">
<text v-if="model">
<text>{{ model }}</text>
</text>
<text v-else class="placeholder">{{ placeholder }}</text>
</view>
<input v-else class="search-bar__input" :type="type" confirm-type="search" :placeholder="placeholder" :value="model" @input="onInput" @confirm="onConfirm" />
<view v-if="model" class="search-bar__icon clear" @click="onClear">
<zui-icon name="close"></zui-icon>
</view>
</view>
</template>
<script>
let inputTimer = null;
export default {
props: {
value: String,
type: {
type: String,
default: 'search',
},
placeholder: {
type: String,
default: '请输入搜索内容'
},
auto: {
type: Boolean,
default: false
},
keyboard: {
type: Boolean,
default: false
},
},
data: function() {
return {
model: this.value
};
},
watch: {
value: function(val) {
this.model = val;
}
},
methods: {
onKeyboard: function() {
this.$emit('keyboard');
},
onInput: function(e) {
this.model = e.target.value;
this.$emit('input', e.target.value);
if (this.auto) {
if (inputTimer) {
clearTimeout(inputTimer)
}
inputTimer = setTimeout(() => {
this.$emit('confirm');
}, 500);
}
},
onClear: function() {
this.model = '';
this.$emit('input', '');
this.$emit('clear', '');
this.$nextTick(() => {
this.$emit('confirm');
});
},
onConfirm: function() {
this.$emit('confirm');
},
}
}
</script>
<style lang="scss">
.search-bar {
background-color: $color-field;
height: 70upx;
border-radius: 35upx;
display: flex;
align-items: center;
padding: 0 $padding-md;
&__icon {
font-size: $font-lg;
color: $color-text;
&.clear {
color: $color-border;
}
}
&__input {
flex: auto;
padding: 0 10upx;
font-size: $font-md;
background-color: transparent;
.placeholder {
color: $color-text-minor;
}
}
}
</style>