113 lines
2.1 KiB
Vue
Raw Normal View History

2026-04-02 15:39:27 +08:00
<template>
<div class="search-input">
<div class="input-wrapper">
<div class="input-block">
<van-icon class="search-icon" name="search" />
2026-04-07 14:26:35 +08:00
<input
class="inner-input"
v-model="modelValue"
:placeholder="placeholder"
/>
<van-icon
class="close-icon"
name="clear"
v-if="modelValue !== ''"
@click="clearInput"
/>
</div>
<!-- 右侧插槽用于放置全部和筛选按钮 -->
<div class="slot-wrapper" v-if="$slots.extra">
<slot name="extra"></slot>
2026-04-02 15:39:27 +08:00
</div>
</div>
</div>
</template>
2026-04-07 14:26:35 +08:00
2026-04-02 15:39:27 +08:00
<script setup>
import { onMounted, ref } from 'vue'
const modelValue = defineModel('modelValue')
const props = defineProps({
placeholder: {
type: String,
default: '请输入关键词'
}
})
2026-04-07 14:26:35 +08:00
// 清空输入框
const clearInput = () => {
modelValue.value = ''
}
2026-04-02 15:39:27 +08:00
</script>
2026-04-07 14:26:35 +08:00
2026-04-02 15:39:27 +08:00
<style scoped lang="scss">
.search-input {
position: relative;
height: 40px;
width: 100%;
margin-top: 8px;
box-sizing: border-box;
}
.input-wrapper {
2026-04-07 14:26:35 +08:00
display: flex;
align-items: center;
2026-04-02 15:39:27 +08:00
width: 100%;
height: 100%;
background-color: #fff;
border-radius: 4px;
2026-04-07 14:26:35 +08:00
gap: 12px; // 输入框和按钮组间距
padding-right: 12px; // 右侧内边距
box-sizing: border-box;
2026-04-02 15:39:27 +08:00
}
.input-block {
position: relative;
2026-04-07 14:26:35 +08:00
flex: 1;
height: 100%;
2026-04-02 15:39:27 +08:00
display: flex;
justify-content: center;
box-sizing: border-box;
.search-icon, .close-icon {
position: absolute;
top: 50%;
left: 20px;
transform: translateY(-50%);
font-size: 20px;
color: #9b9b9b;
2026-04-07 14:26:35 +08:00
pointer-events: none; // 搜索图标不阻挡点击
2026-04-02 15:39:27 +08:00
}
2026-04-07 14:26:35 +08:00
2026-04-02 15:39:27 +08:00
.close-icon {
left: unset;
right: 20px;
2026-04-07 14:26:35 +08:00
pointer-events: auto; // 清空图标可点击
cursor: pointer;
&:active {
opacity: 0.6;
}
2026-04-02 15:39:27 +08:00
}
}
.inner-input {
outline: none;
border: none;
padding: 0;
margin: 0;
2026-04-07 14:26:35 +08:00
width: 100%;
2026-04-02 15:39:27 +08:00
height: 100%;
text-align: center;
font-size: 14px;
2026-04-07 14:26:35 +08:00
background: transparent;
2026-04-02 15:39:27 +08:00
}
2026-04-07 14:26:35 +08:00
.slot-wrapper {
2026-04-02 15:39:27 +08:00
display: flex;
align-items: center;
2026-04-07 14:26:35 +08:00
gap: 8px; // 按钮间距
flex-shrink: 0; // 防止压缩
2026-04-02 15:39:27 +08:00
}
2026-04-07 14:26:35 +08:00
</style>