# Search 搜索框

搜索框:input 的搜索语义壳(形状 + 搜索键 + 取消)。防抖触发(窗口内多次输入 合并为一次 update:modelValue)、搜索键判定(Enter / 小程序 confirm-type)、 类名与清除显隐全部消费 @lh-ui/share/core/search 纯逻辑。

搜索框(防抖回写 + Enter 提交 + 取消)
<template>
  <div class="demo-col" style="gap: 18px">
    <LhSearch v-model="value" placeholder="请输入关键词" @search="onSearch" @clear="onClear"></LhSearch>
    <LhSearch v-model="value2" shape="square" cancel-text="取消" @cancel="onCancel"></LhSearch>
    <p class="demo-note">
      当前值:{{ value || value2 ? value + ' / ' + value2 : '(空)' }}|已提交 {{ times }} 次:{{ last || '尚未提交' }}
    </p>
  </div>
</template>

<script>
export default {
  data: () => ({
    value: '',
    value2: '口红',
    times: 0,
    last: '',
  }),
  methods: {
    onSearch(v) {
      this.times += 1
      this.last = '提交了「' + v + '」'
    },
    onClear() {
      this.last = '已清除'
    },
    onCancel() {
      this.last = '点击了取消'
    },
  },
}
</script>

# 禁用与只读

禁用不可交互;只读可聚焦但不可输入(清除按钮两种形态都不显示)。

<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 事件
  • 只读 / 禁用态三小程序端同样不显示清除按钮(纯函数派生一致)
最后更新: 2026/12/4 00:49:57

Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 | 公安备案徽标苏公网安备32040002010537