Search
搜索框:input 的搜索语义壳(形状 + 搜索键 + 取消)。防抖触发(窗口内多次输入
合并为一次 onChange)、搜索键判定(Enter / 小程序 confirm-type=search)、
类名与清除显隐全部消费 @lh-ui/share/core/search 纯逻辑;清除按钮复用 input
的 __clear 视觉口径。
基础用法
搜索框(防抖回写 + Enter 提交 + 清除 / 取消)
当前值:(空)|已提交 0 次:尚未提交
import { useState } from 'react'
import { LhSearch } from '@lh-ui/react'
export default function Demo() {
const [value, setValue] = useState('')
return (
<LhSearch
modelValue={value}
onChange={setValue}
placeholder="请输入关键词"
onSearch={(v) => console.log('提交', v)}
/>
)
}
禁用与只读
<LhSearch modelValue="固定词" disabled />
<LhSearch modelValue="只读词" readonly />
Search API
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| modelValue | 搜索词(受控) | 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 | — |
| prefix | 前缀区(plus #prefix;默认搜索图标 ⌕) | ReactNode | — |
Events
| 属性 | 说明 | 类型 |
|---|---|---|
| onChange | 搜索词变化(防抖合并后回调;modelValue 回写路径) | (value: string) => void |
| onSearch | 提交搜索(Enter 键 / 小程序搜索键) | (value: string) => void |
| onCancel | 取消按钮点击 | () => void |
| onClear | 清除按钮点击(清除后回写空串) | () => void |
| onFocus | 聚焦 | () => void |
| onBlur | 失焦(失焦前 flush 防抖待发值) | () => void |
Accessibility
- 根节点
aria-label未传时按ariaLabel > placeholder > 值兜底;清除按钮 带独立aria-label="清除"
Form differences across platforms
- plus / vue2: v-model 回写 + Enter 提交,prefix 为具名插槽
- React: v-model 拆为
modelValue+onChange,prefix 为 ReactNode prop - uniapp / wxapp / alipay: 原生 input
confirm-type="search"+bindconfirm映射到 search 事件(见各端文档)