Search 搜尋框
搜尋框:input 的搜尋語意外殼(形狀 + 送出鍵 + 取消)。防抖觸發(視窗內多次 輸入合併為一次 update:modelValue)、類名與清除顯隱全部消費 @lh-ui/share/core/search 純邏輯;輸入為原生 input (confirm-type="search",@confirm 對應到 search 事件)。
平台差異(登記):送出鍵由原生 input 的
confirm-type="search"+@confirm承接(web 三端為 Enter 鍵);唯讀 / 禁用態清除按鈕同樣不顯示 (純函數派生一致)。
基本用法
vue
<template>
<lh-search
v-model="value"
placeholder="請輸入關鍵詞"
aria-label="搜尋商品"
@search="onSearch"
@clear="onClear"
/>
<text>當前值:{{ value || '(空)' }}|已送出 {{ times }} 次</text>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const value = ref('')
const times = ref(0)
function onSearch(v: string) {
times.value += 1
}
function onClear() {
value.value = ''
}
</script>方角與取消按鈕
vue
<lh-search v-model="value" shape="square" cancel-text="取消" @cancel="onCancel" />cancelText 傳入非空字串才渲染取消按鈕(點擊觸發 cancel 事件)。
禁用與唯讀
vue
<lh-search model-value="固定詞" disabled />
<lh-search 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 | — |
| customClass | 自訂類名(透傳到根節點) | string | — |
| sx | sx 快速佈局屬性 | LhSxProps | — |
Events
| 事件 | 說明 | 載荷 |
|---|---|---|
| update:modelValue | 搜尋詞變化(防抖合併後回呼;v-model 回寫路徑) | value: string |
| search | 送出搜尋(原生 input confirm-type=search) | value: string |
| cancel | 取消按鈕點擊 | — |
| clear | 清除按鈕點擊(清除後回寫空字串) | — |
| focus | 聚焦 | — |
| blur | 失焦(失焦前 flush 防抖待發值) | — |
無障礙
根節點 aria-label 未傳時按 ariaLabel > placeholder > 值 兜底;清除按鈕 帶獨立 aria-label="清除"。

