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事件 - 唯讀 / 停用態三小程式端同樣不顯示清除按鈕(純函式派生一致)

