SearchBar.vue
4.96 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<template>
<view
class="search-bar flex items-center"
:class="containerClass"
>
<!-- Search Icon -->
<IconFont
name="search"
:size="iconSize"
:color="iconColor"
class="flex-shrink-0 mr-[16rpx]"
/>
<!-- Input -->
<input
v-model="internalValue"
:type="inputType"
:placeholder="placeholder"
:placeholder-class="placeholderClass"
:class="inputClass"
:disabled="disabled"
@focus="handleFocus"
@blur="handleBlur"
@input="handleInput"
@confirm="handleSearch"
/>
<!-- Clear Button -->
<IconFont
v-if="showClear && internalValue"
name="close"
:size="clearIconSize"
:color="clearIconColor"
class="flex-shrink-0 ml-[16rpx]"
@tap="handleClear"
/>
</view>
</template>
<script setup>
import { ref, watch, computed } from 'vue'
import IconFont from '@/components/IconFont.vue'
/**
* SearchBar 组件
*
* @description 可复用的搜索栏组件,支持多种样式变体
* @author Claude Code
* @example
* <SearchBar
* v-model="searchValue"
* placeholder="搜索资料..."
* variant="rounded"
* @search="handleSearch"
* />
*/
const props = defineProps({
/**
* 绑定值
* @type {string|number}
*/
modelValue: {
type: [String, Number],
default: ''
},
/**
* 占位文本
* @type {string}
* @default '搜索...'
*/
placeholder: {
type: String,
default: '搜索...'
},
/**
* 样式变体
* @type {'normal' | 'rounded'}
* @default 'normal'
*/
variant: {
type: String,
default: 'normal',
validator: (value) => ['normal', 'rounded'].includes(value)
},
/**
* 是否显示边框
* @type {boolean}
* @default false
*/
showBorder: {
type: Boolean,
default: false
},
/**
* 是否显示清除按钮
* @type {boolean}
* @default false
*/
showClear: {
type: Boolean,
default: false
},
/**
* 是否禁用
* @type {boolean}
* @default false
*/
disabled: {
type: Boolean,
default: false
},
/**
* 输入框类型
* @type {string}
* @default 'text'
*/
inputType: {
type: String,
default: 'text'
},
/**
* 图标大小
* @type {number|string}
* @default 18
*/
iconSize: {
type: [Number, String],
default: 18
},
/**
* 图标颜色
* @type {string}
* @default '#9CA3AF'
*/
iconColor: {
type: String,
default: '#9CA3AF'
},
/**
* 清除图标大小
* @type {number|string}
* @default 16
*/
clearIconSize: {
type: [Number, String],
default: 16
},
/**
* 清除图标颜色
* @type {string}
* @default '#9CA3AF'
*/
clearIconColor: {
type: String,
default: '#9CA3AF'
}
})
const emit = defineEmits({
/**
* 更新绑定值
* @event update:modelValue
* @param {string|number} value - 新值
*/
'update:modelValue': (value) => true,
/**
* 搜索事件(按下回车)
* @event search
* @param {string|number} value - 当前值
*/
search: (value) => true,
/**
* 输入事件
* @event input
* @param {string|number} value - 当前值
*/
input: (value) => true,
/**
* 获得焦点
* @event focus
*/
focus: () => true,
/**
* 失去焦点
* @event blur
*/
blur: () => true,
/**
* 清除
* @event clear
*/
clear: () => true
})
// 内部值
const internalValue = ref(props.modelValue)
// 容器样式类
const containerClass = computed(() => {
const base = [
'bg-white',
'shadow-sm',
'px-[40rpx]', // 左右 padding
props.variant === 'rounded' ? 'rounded-full' : 'rounded-[20rpx]'
]
if (props.showBorder) {
base.push('border', 'border-gray-200')
} else {
base.push('border', 'border-gray-50')
}
// 高度样式
if (props.variant === 'rounded') {
base.push('h-[88rpx]')
} else {
base.push('py-[24rpx]')
}
return base.join(' ')
})
// 占位符样式类
const placeholderClass = 'text-gray-400 text-[28rpx]'
// 输入框样式类
const inputClass = computed(() => {
return [
'flex-1',
'text-[28rpx]',
'bg-transparent',
'outline-none',
props.disabled ? 'opacity-50' : ''
].filter(Boolean).join(' ')
})
// 监听 modelValue 变化
watch(() => props.modelValue, (newValue) => {
internalValue.value = newValue
})
// 监听内部值变化,触发更新
watch(internalValue, (newValue) => {
emit('update:modelValue', newValue)
})
/**
* 处理获得焦点
*/
function handleFocus() {
emit('focus')
}
/**
* 处理失去焦点
*/
function handleBlur() {
emit('blur')
}
/**
* 处理输入
*/
function handleInput(e) {
emit('input', internalValue.value)
}
/**
* 处理搜索(回车)
*/
function handleSearch() {
emit('search', internalValue.value)
}
/**
* 清除输入
*/
function handleClear() {
internalValue.value = ''
emit('clear')
emit('update:modelValue', '')
}
</script>
<style lang="less" scoped>
/* 样式通过 TailwindCSS 类控制 */
</style>