筛选栏 FilterBar
查询页的筛选:关键字搜索(KeywordSearch)+ 一行筛选芯片(FilterChip),放不下的收进「更多 N」;选中即生效,没有「查询」按钮。芯片按字段 type 自动配编辑器:select 单选、multi 多选(MultiEditor)、tree 树形(TreeEditor)、intRange 区间(RangeEditor)、dateRange / dateTimeRange 日期(DateRangeEditor)。可服务表格、列表与卡片。
状态:stable · 组件:FilterBar、FilterChip、KeywordSearch、MultiEditor、TreeEditor、RangeEditor、DateRangeEditor、TextEditor、SkinnedCalendar
引入
定位到「引入」import { FilterBar, FilterChip, KeywordSearch, MultiEditor, TreeEditor, RangeEditor, DateRangeEditor, TextEditor, SkinnedCalendar } from 'nacre'用法
定位到「用法」fields 描述字段,values 是「字段 key → 字符串值」;text / int 字段并进左侧搜索框(keywordField 记住搜哪个),其余各成一枚芯片。
import { Text, View, signal } from 'nsh'import { FilterBar, TEXT_SIZE, TOUCH } from 'nacre'import type { FilterField, FilterValues } from 'nacre'
const issueTypes = () => [ { value: 'open', label: '开户' }, { value: 'deposit', label: '入金' }, { value: 'withdraw', label: '出金' }, { value: 'trade', label: '交易' }, { value: 'security', label: '账户安全' },]
export function FilterBarUsage() { const fields: FilterField[] = [ { key: 'customer', label: '客户', type: 'text' }, { key: 'ticketNo', label: '工单号', type: 'text' }, { key: 'state', label: '状态', type: 'select', options: [ { value: 'open', label: '处理中' }, { value: 'closed', label: '已完结' }, ], }, { key: 'issue', label: '问题类型', type: 'multi', options: issueTypes() }, { key: 'createdOn', label: '创建日期', type: 'dateRange' }, ] const values = signal<FilterValues>({}) const keywordField = signal('customer') return ( <View class="gap-2"> <View class="rounded-xl border border-border"> <FilterBar fields={fields} values={values()} onChange={(next) => values.set(next)} keywordField={keywordField()} onKeywordField={(key) => keywordField.set(key)} onRefresh={() => {}} onClear={() => values.set({})} /> </View> <Text class={`${TOUCH ? 'px-4' : ''} ${TEXT_SIZE.small} text-muted-foreground`}>{`$已生效 ${Object.keys(values()).length}$ 个条件`}</Text> </View> )}示例
定位到「示例」芯片与编辑器
定位到「芯片与编辑器」有值时芯片写「标签:值」:长值截断并悬停看全文,多选 / 树形写「首项 +N」。多选、树形、区间勾完点「应用」才生效,日期可选预设或在日历上选区间;text 字段给 chip: true 会使用 TextEditor,回车提交;DateRangeEditor 使用 SkinnedCalendar 提供本地化日历。
手机:手机上芯片是 32 高的胶囊、长值截断(点开编辑器看全文),点击区补到 44;多选、树形、区间勾完点「应用」才生效。
import { View, signal } from 'nsh'import { FilterChip } from 'nacre'import type { FilterField } from 'nacre'
const issueTypes = () => [ { value: 'open', label: '开户' }, { value: 'deposit', label: '入金' }, { value: 'withdraw', label: '出金' }, { value: 'trade', label: '交易' }, { value: 'security', label: '账户安全' },]
const teams = () => [ { value: 'cs', label: '客服中心' }, { value: 'cs-hk', label: '香港团队', parent: 'cs' }, { value: 'cs-sg', label: '新加坡团队', parent: 'cs' }, { value: 'compliance', label: '合规部' }, { value: 'aml', label: '反洗钱组', parent: 'compliance' },]
function Chip(props: { field: FilterField; initial: string }) { const value = signal(props.initial) const open = signal(false) return ( <FilterChip field={props.field} value={value()} open={open()} onOpenChange={(next) => open.set(next)} onChange={(next) => value.set(next ?? '')} onRemove={() => value.set('')} /> )}
export function FilterChipEditors() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Chip field={{ key: 'customerName', label: '客户名', type: 'text', chip: true }} initial="长桥证券(香港)有限公司机构客户服务部高净值客户专属通道" /> <Chip field={{ key: 'issueType', label: '问题类型', type: 'multi', options: issueTypes() }} initial="deposit,withdraw,trade" /> <Chip field={{ key: 'ownerTeam', label: '所属团队', type: 'tree', options: teams() }} initial="cs-hk" /> <Chip field={{ key: 'waitMinutes', label: '排队时长', type: 'intRange' }} initial="5," /> <Chip field={{ key: 'createdRange', label: '创建日期', type: 'dateRange' }} initial="2026-11-01,2026-11-20" /> </View> )}状态
定位到「状态」空值只有描边;有值换填充,悬停或聚焦时右侧出清除钮;required 的芯片没有清除;disabled 的不能打开也不能清除。
手机:空值只有描边;有值换填充,右侧常显清除钮;required 的芯片没有清除;disabled 的不能打开也不能清除。
import { View, signal } from 'nsh'import { FilterChip } from 'nacre'import type { FilterField } from 'nacre'
function Chip(props: { field: FilterField; initial: string }) { const value = signal(props.initial) const open = signal(false) return ( <FilterChip field={props.field} value={value()} open={open()} onOpenChange={(next) => open.set(next)} onChange={(next) => value.set(next ?? '')} onRemove={() => value.set('')} /> )}
export function FilterChipStates() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Chip field={{ key: 'channelType', label: '渠道', type: 'select', options: [{ value: 'app', label: 'App' }, { value: 'web', label: '网页' }] }} initial="" /> <Chip field={{ key: 'period', label: '统计周期', type: 'dateRange', required: true }} initial="2026-11-01,2026-11-24" /> <Chip field={{ key: 'market', label: '市场', type: 'select', disabled: true, options: [{ value: 'hk', label: '港股' }] }} initial="hk" /> </View> )}紧凑尺寸与再点清空
定位到「紧凑尺寸与再点清空」size=“xs” 高 20、字 caption,放在面板或卡片的一行小字里(右栏的记录筛选);筛选栏里用默认的 sm。clearOnReselect 让单选再点已选项清空。禁用时给 disabledReason,悬停芯片说明为什么不能选。值模型不变:多选仍是逗号拼接的字符串,数组与它的转换留在调用方。
手机:手机上 xs 是 28 高的胶囊、字 12;clearOnReselect 与值模型同桌面。
import { View, signal } from 'nsh'import { FilterChip } from 'nacre'
export function FilterChipCompact() { const channel = signal('app') const channelOpen = signal(false) const status = signal('') const statusOpen = signal(false) const statuses = () => [ { value: 'resolved', label: '已解决' }, { value: 'unresolved', label: '未解决' }, ] return ( <View class="flex-row flex-wrap items-center gap-1"> <FilterChip testID="filter-compact-channel" size="xs" clearOnReselect field={{ key: 'compactChannel', label: '渠道', type: 'select', options: [{ value: 'app', label: 'App' }, { value: 'web', label: '网页' }] }} value={channel()} open={channelOpen()} onOpenChange={(next) => channelOpen.set(next)} onChange={(next) => channel.set(next ?? '')} onRemove={() => channel.set('')} /> <FilterChip testID="filter-compact-status" size="xs" field={{ key: 'compactStatus', label: '状态', type: 'multi', options: statuses() }} value={status()} open={statusOpen()} onOpenChange={(next) => statusOpen.set(next)} onChange={(next) => status.set(next ?? '')} onRemove={() => status.set('')} /> <FilterChip testID="filter-compact-locked" size="xs" field={{ key: 'compactLocked', label: '结束原因', type: 'multi', disabled: true, options: statuses() }} disabledReason="只有 AI 会话有结束原因" value="" open={false} onOpenChange={() => {}} onChange={() => {}} onRemove={() => {}} /> </View> )}关键字搜索 KeywordSearch
定位到「关键字搜索 KeywordSearch」多个文本字段合成一个搜索框,左侧切换搜哪个;回车、清空时查询,int 字段只收数字。FilterBar 已内置,单独用时才直接写它。
手机:多个文本字段合成一个搜索框,左侧切换搜哪个;键盘上点搜索或清空时查询,int 字段只收数字。
import { Text, View, signal } from 'nsh'import { KeywordSearch, TEXT_SIZE } from 'nacre'import type { FilterField, FilterValues } from 'nacre'
export function KeywordSearchUsage() { const fields: FilterField[] = [ { key: 'nickname', label: '客户昵称', type: 'text' }, { key: 'accountNo', label: '账户号', type: 'int' }, ] const values = signal<FilterValues>({}) const field = signal('nickname') 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.entries(values()).map(([key, value]) => `${key}=${value}`).join(',') || '—'}`}</Text> </View> )}收起档
定位到「收起档」mode=“collapsed” 给卡片、抽屉这类窄区域:工具行只有一个「筛选」按钮(带生效条件数),点开挑字段,挑中的字段以芯片出现在下一行并打开它的编辑器;已生效的条件是可删的芯片,没有条件时不出这一行。不带外边距,放在哪由调用方定。
import { View, signal } from 'nsh'import { FilterBar, TOUCH } from 'nacre'import type { FilterField, FilterValues } from 'nacre'
const issueTypes = () => [ { value: 'open', label: '开户' }, { value: 'deposit', label: '入金' }, { value: 'withdraw', label: '出金' }, { value: 'trade', label: '交易' }, { value: 'security', label: '账户安全' },]
export function FilterBarCollapsed() { const fields: FilterField[] = [ { key: 'status', label: '状态', type: 'select', options: [ { value: 'open', label: '处理中' }, { value: 'closed', label: '已完结' }, ], }, { key: 'issue', label: '问题类型', type: 'multi', options: issueTypes() }, { key: 'createdOn', label: '创建日期', type: 'dateRange' }, ] const values = signal<FilterValues>({ status: 'open' }) return ( <View testID="filter-collapsed" class={`${TOUCH ? 'w-full' : 'w-[360px] max-w-full rounded-xl border border-border'} p-3`}> <FilterBar mode="collapsed" fields={fields} values={values()} onChange={(next) => values.set(next)} /> </View> )}放不下时
定位到「放不下时」芯片默认一行,放不下的收进「更多 N」,点开展开、再点收起;有值的芯片一定留在可见行,pinned 的优先留下。memoryKey 让展开态在本次运行内按页记住。
import { View, signal } from 'nsh'import { FilterBar } from 'nacre'import type { FilterField, FilterValues } from 'nacre'
export function FilterBarOverflow() { const option = (value: string, label = value) => ({ value, label }) const fields: FilterField[] = [ { key: 'sessionId', label: '会话 ID', type: 'text' }, { key: 'agentName', label: '坐席', type: 'select', options: [option('林嘉怡', '林嘉怡'), option('陈智源', '陈智源')], pinned: true, }, { key: 'source', label: '来源', type: 'select', options: [option('App'), option('网页', '网页'), option('WhatsApp')] }, { key: 'priority', label: '优先级', type: 'select', options: [option('高', '高'), option('中', '中'), option('低', '低')], }, { key: 'region', label: '地区', type: 'select', options: [option('香港', '香港'), option('新加坡', '新加坡'), option('美国', '美国')], }, { key: 'language', label: '语言', type: 'select', options: [option('中文', '中文'), option('English')] }, { key: 'satisfaction', label: '满意度', type: 'select', options: [option('满意', '满意'), option('一般', '一般'), option('不满意', '不满意')], }, { key: 'tagName', label: '标签', type: 'multi', options: [option('开户', '开户'), option('入金', '入金'), option('投诉', '投诉')], }, { key: 'duration', label: '时长(分)', type: 'intRange', required: true }, { key: 'closedAt', label: '完结时间', type: 'dateTimeRange' }, ] const values = signal<FilterValues>({}) const keywordField = signal('sessionId') return ( <View class="rounded-xl border border-border"> <FilterBar fields={fields} values={values()} onChange={(next) => values.set(next)} keywordField={keywordField()} onKeywordField={(key) => keywordField.set(key)} memoryKey="docs-filter-overflow" onClear={() => values.set({})} /> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Tab | 在搜索框、芯片、「更多」与刷新 / 清空之间移动 |
| Return | 在搜索框、文本编辑器、区间编辑器里:提交 |
| Backspace / Delete | 清除当前聚焦芯片的值(required、disabled 除外) |
| Esc | 收起编辑器,焦点回到芯片 |
无障碍
定位到「无障碍」- 芯片的读屏名是「标签:值」(「问题类型:入金 +2」),空值时只读标签。
- 清除钮的读屏名是「清除」;关键字搜索左侧的字段切换读作按钮,名字是当前字段。
- 编辑器开着时,表格的全局 ↑↓ ⏎ 让给编辑器。
- 芯片读作按钮,读屏带「已展开」;禁用时读 disabled,disabledReason 作为悬停提示。
该用 / 不该用
定位到「该用 / 不该用」该用
- 选中值即时生效
- 长值截断并悬停看全文
- 常用的条件给 pinned,留在第一行
- 空结果时在表格里给「清空筛选」
不该用
- 把筛选做成表单 + 查询按钮
- 把必填条件做成可清除
- 一页放两个筛选栏
API
定位到「API」FilterBar
定位到「FilterBar」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
fields * |
FilterField[] |
— | |
values * |
FilterValues |
— | |
onChange * |
(values: FilterValues) => void |
— | |
mode |
'inline' | 'collapsed' |
— | inline(默认):关键词框 + 平铺芯片,放不下的收进「更多 N」,查询页的筛选栏。 collapsed:工具行只有一个「筛选」按钮(带生效条件数),点开逐项挑字段再编辑,生效条件以可删芯片排在下一行、没有条件不出这一行; 不带外边距,嵌在卡片、抽屉这类窄区域里。创建时读一次 |
keywordField |
string |
— | 关键词框选中的字段,由调用方持久化(inline) |
onKeywordField |
(key: string) => void |
— | |
keywordRef |
(node: InputRef) => void |
— | |
memoryKey |
string |
— | 「更多」展开态在本次运行内按它记忆(每页一个),不进 Storage |
onRefresh |
() => void |
— | |
refreshing |
boolean |
— | |
onClear |
() => void |
— | |
leading |
JSX.Element |
— | |
tools |
JSX.Element |
— | 刷新与清空之间的工具位(常用筛选) |
trailing |
JSX.Element |
— | 右侧:表格设置、导出等 |
* 必填
FilterChip
定位到「FilterChip」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
field * |
FilterField |
— | |
value * |
string |
— | 字段值(同 FilterBar 的 values):多选 / 树形是逗号拼接,区间与日期是 start,end |
open * |
boolean |
— | |
onOpenChange * |
(open: boolean) => void |
— | |
onChange * |
(value: string | undefined) => void |
— | |
onRemove * |
() => void |
— | |
size |
'xs' | 'sm' |
'sm' |
sm 28(默认,筛选栏)/ xs 20(嵌在面板、卡片的一行小字里:右栏记录筛选),内边距与圆角读 xs 结构 token |
clearOnReselect |
boolean |
— | 单选(select)再点已选项清空(onChange(undefined));默认再点保持不变 |
disabledReason |
string |
— | 禁用(field.disabled)时悬停芯片看到的原因:「先清掉会话状态」这类 |
testID |
string |
— | 芯片的 testID,默认 filter-chip-${field.key};给了时编辑器是 ${testID}-options |
* 必填
KeywordSearch
定位到「KeywordSearch」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
fields * |
FilterField[] |
— | 文本类字段(text / int) |
field * |
string |
— | 选中的字段 key,空时取第一个;由调用方持久化 |
onField * |
(key: string) => void |
— | |
values * |
FilterValues |
— | |
onSearch * |
(values: FilterValues) => void |
— | 回车、清空时:文本字段只保留选中的那一个 |
inputRef |
(node: InputRef) => void |
— |
* 必填
MultiEditor
定位到「MultiEditor」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
field * |
FilterField |
— | |
value * |
string |
— | |
onChange * |
(value: string | undefined) => void |
— | |
onDone * |
() => void |
— | |
anchorWidth |
number |
— | |
testID |
string |
— | 编辑器根节点的 testID,默认 filter-editor-${field.key};多选的「应用」「清空」在它后面加 -apply / -clear |
* 必填
TreeEditor
定位到「TreeEditor」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
field * |
FilterField |
— | |
value * |
string |
— | |
onChange * |
(value: string | undefined) => void |
— | |
onDone * |
() => void |
— | |
anchorWidth |
number |
— | |
testID |
string |
— | 编辑器根节点的 testID,默认 filter-editor-${field.key};多选的「应用」「清空」在它后面加 -apply / -clear |
* 必填
RangeEditor
定位到「RangeEditor」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
field * |
FilterField |
— | |
value * |
string |
— | |
onChange * |
(value: string | undefined) => void |
— | |
onDone * |
() => void |
— | |
anchorWidth |
number |
— | |
testID |
string |
— | 编辑器根节点的 testID,默认 filter-editor-${field.key};多选的「应用」「清空」在它后面加 -apply / -clear |
* 必填
DateRangeEditor
定位到「DateRangeEditor」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
testID * |
string |
— | |
value * |
string |
— | start,end:日期 YYYY-MM-DD,带时间时 YYYY-MM-DD HH:mm |
withTime |
boolean |
— | |
granularity |
'minute' | 'hour' |
— | 带时间时的粒度:hour 只取整点(HH:00),结束默认 23:00 |
maxDate |
string |
— | 最晚可选的日期 YYYY-MM-DD,之后的日子置灰 |
isValid |
(draft: string) => boolean |
— | 草稿(同 value 的格式)不合法时「应用」置灰 |
presets * |
readonly DatePreset[] |
— | 空数组时不渲染快捷区间一行 |
onApply * |
(value: string | undefined) => void |
— | |
footnote |
JSX.Element |
— |
* 必填
TextEditor
定位到「TextEditor」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
field * |
FilterField |
— | |
value * |
string |
— | |
onChange * |
(value: string | undefined) => void |
— | |
onDone * |
() => void |
— | |
anchorWidth |
number |
— | |
testID |
string |
— | 编辑器根节点的 testID,默认 filter-editor-${field.key};多选的「应用」「清空」在它后面加 -apply / -clear |
* 必填
SkinnedCalendar
定位到「SkinnedCalendar」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
testID |
string |
— | |
range |
boolean |
— | |
value * |
CalendarValue |
— | |
numberOfMonths |
number |
1 |
|
maxDate |
string |
— | 最晚可选的日期 YYYY-MM-DD,之后的日子置灰、不可点 |
paint |
(date: string) => DayPaint |
— | 区间自己画(DateRangeEditor):给了就不用 Calendar 的选中样式 |
onHoverDate |
(date: string | null) => void |
— | |
onChange * |
(value: CalendarValue) => void |
— |
* 必填