Search 搜索框
搜索框:input 的搜索语义壳(形状 + 搜索键 + 取消)。防抖触发(窗口内多次输入 合并为一次 update:modelValue)、搜索键判定(Enter / 小程序 confirm-type)、 类名与清除显隐全部消费 @lh-ui/share/core/search 纯逻辑;清除按钮复用 input 的 __clear 视觉口径。
基础用法
基础用法(防抖回写 + Enter 提交)
当前值:,已提交 0 次(尚未提交)
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhSearch } from '@lh-ui/vue3'
const value = ref('')
const times = ref(0)
const last = ref('')
function onSearch(v: string) {
times.value += 1
last.value = `提交了「${v}」`
}
function onClear() {
last.value = '已清除'
}
</script>
<template>
<LhSearch v-model="value" placeholder="请输入关键词" @search="onSearch" @clear="onClear" />
</template>形状与取消按钮
shape="square" 走方角;cancelText 传入非空串才渲染取消按钮(点击触发 cancel 事件,业务侧常配合失焦收起键盘)。
方角 + 取消按钮
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhSearch } from '@lh-ui/vue3'
const value2 = ref('口红')
const cancelTip = ref('')
function onCancel() {
cancelTip.value = '点击了取消'
}
</script>禁用与只读
禁用不可聚焦交互;只读可聚焦但不可输入(清除按钮两种形态都不显示)。
vue
<LhSearch model-value="固定词" disabled />
<LhSearch model-value="只读词" readonly />Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| modelValue | 搜索词(受控,v-model) | string | '' |
| placeholder | 占位文案 | string | '' |
| shape | 形状 | "round" | "square" | 'round' |
| clearable | 有值时显示清除按钮 | boolean | true |
| disabled | 禁用 | boolean | false |
| readonly | 只读(可聚焦不可输入) | boolean | false |
| maxLength | 最大输入长度 | number | — |
| cancelText | 取消按钮文案(不传 / 空串 = 不渲染) | string | — |
| ariaLabel | 无障碍名称(未传取 placeholder / 值兜底) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
Events
| 事件 | 说明 | 载荷 |
|---|---|---|
| update:modelValue | 搜索词变化(防抖合并后回调;v-model 回写路径) | value: string |
| search | 提交搜索(Enter 键 / 小程序搜索键) | value: string |
| cancel | 取消按钮点击 | — |
| clear | 清除按钮点击(清除后回写空串) | — |
| focus | 聚焦 | — |
| blur | 失焦(失焦前 flush 防抖待发值) | — |
Slots
| 名称 | 说明 |
|---|---|
| prefix | 前缀区(默认搜索图标 ⌕) |
无障碍
根节点 aria-label 未传时按 ariaLabel > placeholder > 值 兜底;清除按钮 带独立 aria-label="清除";输入元素本身不重复声明名称。
跨端差异
- web 三端:Enter 提交 + 防抖回写;uniapp / wxapp / alipay 用原生 input 的
confirm-type="search"+bindconfirm映射到search事件 - 只读 / 禁用态三小程序端同样不显示清除按钮(纯函数派生一致)

