Skip to content
Nacre
Color palette
中文

SearchField

stableSearchField · KeywordSearchOpen in playground
SearchField
Runs the real component in your browser.

A magnifier, an input, and a clear button once there’s text. With only onChange it filters instantly; with onCommit it submits (on Return and on clear, for server-side search). Records pages search by field with KeywordSearch.

Status: stable · Components: SearchField, KeywordSearch

import { SearchField, KeywordSearch } from 'nacre'
import { Text, View, signal } from 'nsh'
import { SearchField, TEXT_SIZE, TOUCH } from 'nacre'
const sessions = () => [
'Lin Jiayi',
'Chen Zhiyuan',
'Zhou Kexin',
'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="Search chats" />
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{matched().join(', ') || 'No matching chats'}</Text>
</View>
)
}

onCommit fires on Return, on pressing clear and on deleting all text, with the value trimmed; suited to searches that hit the server.

Phone: onCommit fires when you tap the keyboard’s search key, tap clear or delete everything, with surrounding spaces trimmed; suited to server-side search.

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 or phone' : 'Customer ID or phone number'}
/>
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$Submitted: ${committed() || '—'}`}</Text>
</View>
)
}

xs 24 / sm 28 (default) / md 32 / lg 36, the same scale as TextField; use md when it stands alone as a bar (a find bar).

Phone: On phones: xs 32 / sm 36 (default, the iOS search bar) / md 40 / lg 44, all with radius 10; a tap within 4 pt of the field still focuses it.

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="Search" accessibilityLabel={`$Search ${size}`} />
))}
</View>
)
}

KeywordSearch combines several text fields into one box, with a switcher on the left for the field to search; it hands over the full filter values on Return and on clear.

Phone: KeywordSearch merges several text fields into one box with a field switcher on the left; it hands over the full filter value when you tap the keyboard’s search key or clear it.

import { Text, View, signal } from 'nsh'
import { KeywordSearch, TEXT_SIZE } from 'nacre'
import type { FilterField, FilterValues } from 'nacre'
const fields = (): FilterField[] => [
{ key: 'customer', label: 'Customer ID', type: 'text' },
{ key: 'phone', label: 'Phone number', 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`}>{`$Query: ${Object.keys(values()).length ? JSON.stringify(values()) : '—'}`}</Text>
</View>
)
}
Key Action
Return Submit (onCommit; onSubmit takes it when given)
Esc Clear when there’s text; blur when empty
  • The screen reader name defaults to “Search”, and the placeholder doesn’t count as a name; when a page has several search fields, tell them apart with accessibilityLabel.
  • The clear button’s screen reader name is “Clear”.

Do

  • Instant filtering at the top of a list
  • Submit server-side searches on Return (onCommit)
  • Say what is searched in the placeholder (“Search chats”)

Don’t

  • Put filter conditions in the search field (use filter chips)
  • Hit the server on every keystroke

Property descriptions follow the Chinese source comments.

Prop Type Default Description
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 不算名字)

* required

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

* required