# 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事件 - 唯讀 / 停用態三小程式端同樣不顯示清除按鈕(純函式派生一致)
Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 |
苏公网安备32040002010537