跳转到内容
Nacre
组件配色
EN

搜索框 SearchField

stableSearchField · KeywordSearch在画廊运行
SearchField
在浏览器里运行真实组件。

放大镜 + 输入 + 有字时的清除钮。只给 onChange 是即时过滤;给 onCommit 是提交式(回车、清空时提交,走服务端)。记录页按字段搜用 KeywordSearch。

状态:stable · 组件:SearchField、KeywordSearch

import { SearchField, KeywordSearch } from 'nacre'
import { Text, View, signal } from 'nsh'
import { SearchField, TEXT_SIZE, TOUCH } from 'nacre'
const sessions = () => [
'林嘉怡',
'陈智源',
'周可欣',
'Michael Wong',
]
export function SearchFieldUsage() {
const SESSIONS = sessions()
const keyword = signal('')
const matched = () => SESSIONS.filter((name) => name.toLowerCase().includes(keyword().trim().toLowerCase()))
return (
<View class={TOUCH ? 'w-full gap-2' : 'w-[240px] max-w-full gap-2'}>
<SearchField value={keyword()} onChange={(value) => keyword.set(value)} placeholder="搜索会话" />
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{matched().join('、') || '没有匹配的会话'}</Text>
</View>
)
}

onCommit 在回车、点清除、删空时触发,值已去掉首尾空格;适合请求服务端的搜索。

手机:onCommit 在键盘上点搜索、点清除、删空时触发,值已去掉首尾空格;适合请求服务端的搜索。

import { Text, View, signal } from 'nsh'
import { SearchField, TEXT_SIZE, TOUCH } from 'nacre'
export function SearchFieldCommit() {
const keyword = signal('')
const committed = signal('')
return (
<View class="flex-row flex-wrap items-center gap-3">
<SearchField
class={TOUCH ? 'w-full' : 'w-[220px] max-w-full'}
value={keyword()}
onChange={(value) => keyword.set(value)}
onCommit={(value) => committed.set(value)}
placeholder={TOUCH ? 'ID 或手机号' : '客户 ID 或手机号'}
/>
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$已提交:${committed() || '—'}`}</Text>
</View>
)
}

xs 24 / sm 28(默认)/ md 32 / lg 36,与 TextField 同一套;独立成条(查找栏)用 md。

手机:手机档 xs 32 / sm 36(默认,iOS 搜索栏)/ md 40 / lg 44,圆角统一 10;框外缘上下 4 内点按也会聚焦输入。

import { View } from 'nsh'
import { SearchField, TOUCH } from 'nacre'
export function SearchFieldSizes() {
return (
<View class="gap-2">
{(['xs', 'sm', 'md', 'lg'] as const).map((size) => (
<SearchField class={TOUCH ? 'w-full' : 'w-[220px] max-w-full'} size={size} value="" onChange={() => {}} placeholder="搜索" accessibilityLabel={`$搜索 ${size}`} />
))}
</View>
)
}

KeywordSearch 把几个文本字段合成一个框,左侧切换搜哪个字段;回车、清空时交出整份筛选值。

手机:KeywordSearch 把几个文本字段合成一个框,左侧切换搜哪个字段;键盘上点搜索或清空时交出整份筛选值。

import { Text, View, signal } from 'nsh'
import { KeywordSearch, TEXT_SIZE } from 'nacre'
import type { FilterField, FilterValues } from 'nacre'
const fields = (): FilterField[] => [
{ key: 'customer', label: '客户 ID', type: 'text' },
{ key: 'phone', label: '手机号', type: 'int' },
]
export function SearchFieldKeyword() {
const field = signal('customer')
const values = signal<FilterValues>({})
return (
<View class="gap-2">
<KeywordSearch fields={fields()} field={field()} onField={(key) => field.set(key)} values={values()} onSearch={(next) => values.set(next)} />
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$查询:${Object.keys(values()).length ? JSON.stringify(values()) : '—'}`}</Text>
</View>
)
}
键 行为
Return 提交(onCommit;给了 onSubmit 时交给它)
Esc 有字时清空,空时失焦
  • 读屏名默认是「搜索」,placeholder 不算名字;一页有多个搜索框时用 accessibilityLabel 区分。
  • 清除钮的读屏名是「清除」。

该用

  • 列表顶部的即时过滤
  • 服务端搜索用回车提交(onCommit)
  • 占位写搜什么(「搜索会话」)

不该用

  • 在搜索框里放筛选条件(用筛选芯片)
  • 每输入一个字就请求服务端
属性 类型 默认 说明
value * string —
onChange * (value: string) => void —
onCommit (value: string) => void — 给了就是提交式(回车、清空、删空时提交,走服务端的搜索用);不给就只有 onChange 实时过滤
placeholder string —
class string — 宽度与外边距;不给就撑满父容器
testID string —
autoFocus boolean —
inputRef (node: InputRef) => void —
leading JSX.Element — 放大镜之前的附加控件(如搜索字段的切换)
trailing JSX.Element — 清除钮之后的附加控件(查找栏的「3/12」与上下跳转)
onSubmit (value: string) => void — 回车:给了就交给它(查找栏跳到下一处),不再按提交式处理
onBlur () => void — 失焦(查找栏失焦时收起高亮)
size 'xs' | 'sm' | 'md' | 'lg' 'sm' xs 24 / sm 28(默认)12pt;md 32 / lg 36 13pt(独立成条时,如查找栏,用 md)
accessibilityLabel string — 读屏名;不给时是「搜索」(placeholder 不算名字)

* 必填

属性 类型 默认 说明
fields * FilterField[] — 文本类字段(text / int)
field * string — 选中的字段 key,空时取第一个;由调用方持久化
onField * (key: string) => void —
values * FilterValues —
onSearch * (values: FilterValues) => void — 回车、清空时:文本字段只保留选中的那一个
inputRef (node: InputRef) => void —

* 必填