Combobox
Searchable selection: a search field appears with more than 10 options, options can be grouped, and individual ones disabled. Single selection in forms uses SelectMenu (whose popover is a Combobox); Combobox’s multiple selection (handed over only when you click “Apply”) is used by the filter chip’s multi-select editor; see the FilterBar page.
Status: experimental · Components: Combobox, SelectMenu, ComboboxPopover, TouchComboboxSheet, TouchValueRow
Import
Section titled “Import”import { Combobox, SelectMenu, ComboboxPopover, TouchComboboxSheet, TouchValueRow } from 'nacre'import { signal } from 'nsh'import { SelectMenu, SelectTrigger } from 'nacre'
const agents = () => { const tier1 = 'Tier 1' const tier2 = 'Tier 2' const tier3 = 'Tier 3' return [ { value: 'a', label: 'Lin Jiayi', group: tier1 }, { value: 'b', label: 'Chen Zhiyuan', group: tier1 }, { value: 'c', label: 'Zhou Kexin', group: tier2 }, { value: 'd', label: 'Michael Wong', group: tier2 }, { value: 'e', label: 'Former agent', group: tier2, disabled: true }, { value: 'f', label: 'Wang Yutong', group: tier3 }, { value: 'g', label: 'Huang Zixuan', group: tier3 }, { value: 'h', label: 'Christina Lau', group: tier3 }, { value: 'i', label: 'Ouyang Xiaoqing (HK Institutional Client Services · High-net-worth priority line)', group: tier3 }, { value: 'j', label: 'Liu Siyuan', group: tier3 }, { value: 'k', label: 'Zheng Jiahao', group: tier3 }, ]}
export function ComboboxUsage() { const AGENTS = agents() const agent = signal('a') return ( <SelectMenu testID="demo-combo" accessibilityLabel="Agent" options={AGENTS} value={agent()} onChange={(value) => agent.set(value)} searchable trigger={(open) => <SelectTrigger class="w-48" expanded={open()} value={AGENTS.find((a) => a.value === agent())?.label} accessibilityLabel="Agent" />} /> )}
type Client = { id: string; name: string }Examples
Section titled “Examples”Grouped multiple selection on a phone
Section titled “Grouped multiple selection on a phone”The value row sits in a group; the sheet has search and a leading dot for each label’s tone, and a read-only row is dimmed and not tappable. Tap “Apply” to commit; dismissing the sheet discards unconfirmed changes.
import { View, signal } from 'nsh'import { TouchValueRow, TouchComboboxSheet, TouchGroup } from 'nacre'
const chatLabels = () => [ { value: 'new', tone: 'info', label: 'New customer' }, { value: 'follow', tone: 'warning', label: 'Follow up' }, { value: 'done', tone: 'success', label: 'Complete' },] as const
function ChatLabelIcon(props: { value: string }) { const tag = () => chatLabels().find((item) => item.value === props.value) return <If when={tag()}>{(t) => <View class="w-2 h-2 rounded-full" classList={{ 'bg-info': t().tone === 'info', 'bg-warning': t().tone === 'warning', 'bg-success': t().tone === 'success' }} />}</If>}
export function ComboboxTouchLabels() { const open = signal(false) const selected = signal<string[]>([]) const title = () => 'Chat labels' return <View> <TouchGroup> <TouchValueRow testID="demo-touch-tags" label={title()} value={chatLabels().filter((item) => selected().includes(item.value)).map((item) => item.label).join('、')} placeholder="Select" onPress={() => open.set(true)} /> <TouchValueRow disabled label="Archived labels" value="Read only" /> </TouchGroup> <TouchComboboxSheet testID="demo-touch-options" open={open()} title={title()} options={chatLabels()} leading={(value) => <ChatLabelIcon value={value} />} searchable multiple value={selected()} onChange={(value) => selected.set(value)} onDone={() => open.set(false)} /> </View>}Multiple selection on a phone
Section titled “Multiple selection on a phone”Tap the value row to open the sheet. Apply commits the selection. Dismissing preserves the previous value.
import { View, signal } from 'nsh'import { TouchValueRow, TouchComboboxSheet } from 'nacre'
const channels = () => [ { value: 'app', label: 'Longbridge App', keywords: ['longbridge', 'app'] }, { value: 'web', label: 'Web live chat', keywords: ['web', 'h5'] }, { value: 'wechat', label: 'WeChat Official Account', keywords: ['wechat', 'weixin'] }, { value: 'whatsapp', label: 'WhatsApp', keywords: ['wa'] }, { value: 'email', label: 'Email', keywords: ['email', 'mail'] }, { value: 'phone', label: 'Phone callback', keywords: ['phone', 'call'] }, { value: 'line', label: 'LINE', keywords: ['line'] }, { value: 'telegram', label: 'Telegram', keywords: ['tg'] }, { value: 'pc', label: 'Desktop', keywords: ['pc', 'desktop'] }, { value: 'pro', label: 'Pro', keywords: ['pro'] }, { value: 'api', label: 'Open Platform', keywords: ['openapi', 'api'] },]
export function ComboboxTouchForm() { const open = signal(false) const value = signal<string[]>([]) const title = () => 'Channels' return <View> <TouchValueRow label={title()} value={channels().filter((c) => value().includes(c.value)).map((c) => c.label).join(', ')} onPress={() => open.set(true)} /> <TouchComboboxSheet testID="demo-touch-channels" title={title()} open={open()} options={channels()} multiple value={value()} onChange={(v) => value.set(v)} onDone={() => open.set(false)} /> </View>}Responsive selection trigger
Section titled “Responsive selection trigger”Use one trigger for a desktop popover and a bottom sheet on a phone. The selected value is reflected in the trigger and its accessible name.
import { signal } from 'nsh'import { SelectTrigger, ComboboxPopover } from 'nacre'
const channels = () => [ { value: 'app', label: 'Longbridge App', keywords: ['longbridge', 'app'] }, { value: 'web', label: 'Web live chat', keywords: ['web', 'h5'] }, { value: 'wechat', label: 'WeChat Official Account', keywords: ['wechat', 'weixin'] }, { value: 'whatsapp', label: 'WhatsApp', keywords: ['wa'] }, { value: 'email', label: 'Email', keywords: ['email', 'mail'] }, { value: 'phone', label: 'Phone callback', keywords: ['phone', 'call'] }, { value: 'line', label: 'LINE', keywords: ['line'] }, { value: 'telegram', label: 'Telegram', keywords: ['tg'] }, { value: 'pc', label: 'Desktop', keywords: ['pc', 'desktop'] }, { value: 'pro', label: 'Pro', keywords: ['pro'] }, { value: 'api', label: 'Open Platform', keywords: ['openapi', 'api'] },]
export function ComboboxTouchTrigger() { const value = signal<string[]>([]) const title = () => 'Channel' return <ComboboxPopover testID="demo-responsive-channel" title={title()} options={channels()} value={value()} onChange={(v) => value.set(v)} trigger={<SelectTrigger accessibilityLabel={title()} placeholder={title()} value={channels().find((c) => c.value === value()[0])?.label} />} />}Search by alias
Section titled “Search by alias”keywords adds searchable text to an option (English name, pinyin, former name), matched together with label.
import { signal } from 'nsh'import { SelectMenu, SelectTrigger } from 'nacre'
const channels = () => [ { value: 'app', label: 'Longbridge App', keywords: ['longbridge', 'app'] }, { value: 'web', label: 'Web live chat', keywords: ['web', 'h5'] }, { value: 'wechat', label: 'WeChat Official Account', keywords: ['wechat', 'weixin'] }, { value: 'whatsapp', label: 'WhatsApp', keywords: ['wa'] }, { value: 'email', label: 'Email', keywords: ['email', 'mail'] }, { value: 'phone', label: 'Phone callback', keywords: ['phone', 'call'] }, { value: 'line', label: 'LINE', keywords: ['line'] }, { value: 'telegram', label: 'Telegram', keywords: ['tg'] }, { value: 'pc', label: 'Desktop', keywords: ['pc', 'desktop'] }, { value: 'pro', label: 'Pro', keywords: ['pro'] }, { value: 'api', label: 'Open Platform', keywords: ['openapi', 'api'] },]
export function ComboboxKeywords() { const CHANNELS = channels() const channel = signal('app') return ( <SelectMenu testID="example-combobox-channel" accessibilityLabel="Channel" options={CHANNELS} value={channel()} onChange={(value) => channel.set(value)} trigger={(open) => <SelectTrigger class="w-48" expanded={open()} value={CHANNELS.find((c) => c.value === channel())?.label} accessibilityLabel="Channel" />} /> )}No search field
Section titled “No search field”Purely numeric lists such as page size or hour pass searchable={false} even with more than 10 items; once open, use the arrow keys directly.
Phone: Purely numeric lists such as page size or hour pass searchable={false} even with more than 10 items; pick directly in the sheet.
import { signal } from 'nsh'import { SelectMenu, SelectTrigger } from 'nacre'
const pageSize = (n: string) => `${n} $/ page`
const pageSizes = () => ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200'].map((n) => ({ value: n, label: pageSize(n) }))
export function ComboboxNoSearch() { const size = signal('20') return ( <SelectMenu testID="example-combobox-page-size" accessibilityLabel="Items per page" options={pageSizes()} value={size()} onChange={(value) => size.set(value)} searchable={false} trigger={(open) => <SelectTrigger class="w-32" expanded={open()} value={pageSize(size())} accessibilityLabel="Items per page" />} /> )}Disabled
Section titled “Disabled”SelectMenu with disabled no longer opens; the trigger also takes disabled to draw the disabled style.
import { SelectMenu, SelectTrigger } from 'nacre'
const agents = () => { const tier1 = 'Tier 1' const tier2 = 'Tier 2' const tier3 = 'Tier 3' return [ { value: 'a', label: 'Lin Jiayi', group: tier1 }, { value: 'b', label: 'Chen Zhiyuan', group: tier1 }, { value: 'c', label: 'Zhou Kexin', group: tier2 }, { value: 'd', label: 'Michael Wong', group: tier2 }, { value: 'e', label: 'Former agent', group: tier2, disabled: true }, { value: 'f', label: 'Wang Yutong', group: tier3 }, { value: 'g', label: 'Huang Zixuan', group: tier3 }, { value: 'h', label: 'Christina Lau', group: tier3 }, { value: 'i', label: 'Ouyang Xiaoqing (HK Institutional Client Services · High-net-worth priority line)', group: tier3 }, { value: 'j', label: 'Liu Siyuan', group: tier3 }, { value: 'k', label: 'Zheng Jiahao', group: tier3 }, ]}
export function ComboboxDisabled() { return ( <SelectMenu testID="example-combobox-disabled" accessibilityLabel="Assigned agent" options={agents()} value="b" onChange={() => {}} disabled trigger={<SelectTrigger class="w-48" value="Chen Zhiyuan" disabled accessibilityLabel="Assigned agent" />} /> )}
type Client = { id: string; name: string }Server search
Section titled “Server search”onSearch turns off local filtering: every keystroke calls back, the caller debounces, requests and swaps the results into options; loading shows a spinner at the end of the search row. status is a hint row at the top of the list (searching, nothing found); a hint with an action is selectable and ⏎ runs it (retry) without closing. renderOption draws a custom row (highlight matches with Highlight) while the combobox still gives the current-row fill and the trailing check. With no query it lists a “Recent” group. Try typing “Chen” or “500”.
import { View, signal } from 'nsh'import { Combobox, Highlight, MENU_CURRENT_TEXT, Popover, SelectTrigger, TEXT_SIZE } from 'nacre'
type Client = { id: string; name: string }
const directory = (): Client[] => [ { id: '18630613', name: 'Chen Siyuan' }, { id: '18630777', name: 'Chen Jiayi' }, { id: '18631024', name: 'Michael Wong' },]
export function ComboboxAsync() { const clients = directory() const picked = signal('') const open = signal(false) const query = signal('') const loading = signal(false) const failed = signal(false) const results = signal<Client[]>([]) const recent = signal<Client[]>([clients[2]]) let timer: ReturnType<typeof setTimeout> | undefined const run = () => { if (timer) clearTimeout(timer) const q = query().trim() failed.set(false) loading.set(q !== '') if (!q) return timer = setTimeout(() => { loading.set(false) if (q === '500') return failed.set(true) results.set(clients.filter((c) => c.name.toLowerCase().includes(q.toLowerCase()) || c.id.includes(q))) }, 300) } const browsing = () => query().trim() === '' const shown = () => (browsing() ? recent() : loading() || failed() ? [] : results()) const status = () => failed() ? { text: 'Search failed. Press ⏎ to retry', action: run, testID: 'demo-combo-async-retry' } : loading() ? { text: 'Searching…' } : shown().length === 0 ? { text: 'No customers found' } : undefined const name = () => clients.find((c) => c.id === picked())?.name return ( <Popover surface="menu" flush open={open()} onOpenChange={(next) => open.set(next)} trigger={({ open, ref }) => ( <SelectTrigger class="w-48" testID="demo-combo-async-trigger" accessibilityLabel="Customer" placeholder="Search customers" value={name()} expanded={open()} viewRef={ref} /> )} > <Combobox testID="demo-combo-async" accessibilityLabel="Customer" searchPlaceholder="Name or client ID" minWidth={280} options={shown().map((c) => ({ value: c.id, label: c.name, group: browsing() ? 'Recent' : undefined }))} value={picked() ? [picked()] : []} onSearch={(q) => { query.set(q) run() }} loading={loading()} status={status()} renderOption={(option) => ( <View class="flex-1 min-w-0 flex-row items-center gap-2"> <Highlight class={`shrink ${TEXT_SIZE.body} text-popover-foreground ${MENU_CURRENT_TEXT}`} text={option.label} keyword={query()} /> <Highlight class={`shrink-0 ${TEXT_SIZE.small} tabular-nums text-muted-foreground ${MENU_CURRENT_TEXT}`} text={option.value} keyword={query()} /> </View> )} onChange={(values) => { const client = clients.find((c) => c.id === values[0]) if (!client) return false picked.set(client.id) recent.set([client, ...recent().filter((c) => c.id !== client.id)].slice(0, 8)) }} onDone={() => open.set(false)} /> </Popover> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| 输入 | Filter the options and highlight the first one |
| ↑ ↓ | Move between options (skipping disabled ones) |
| Return | Select the highlighted option; single selection then closes |
| Return(提示行) | In async mode, on a hint row with an action: run it (retry) and stay open |
| Esc | Clear the query first if there is one, then close |
| Tab / ⇧Tab | In single selection, close and move focus away |
Accessibility
Section titled “Accessibility”- The list’s screen reader name is accessibilityLabel (the field name); the trigger’s is “field name: current value”.
- The selected option gets a checkmark at the end of the row, not just a fill; truncated long options show the full text on hover.
- In async mode the list is announced busy while searching; the hint row is an item in the list, so “Searching” and “Nothing found” are read out.
Do / Don’t
Section titled “Do / Don’t”Do
- Show a search field above 10 options
- Truncate long options and show the full text on hover
- Group adjacent options of the same kind under a subheading with group
- For server search use onSearch + status, and give a retryable hint row on failure
Don’t
- Show a search field for only a few options
- Use it to trigger actions (use DropdownMenu)
- Keep the previous round’s results selectable by Return while searching (clear or disable stale rows)
Property descriptions follow the Chinese source comments.
Combobox
Section titled “Combobox”| Prop | Type | Default | Description |
|---|---|---|---|
testID * |
string |
— | |
options * |
readonly ComboboxOption[] |
— | |
value * |
readonly string[] |
— | |
multiple |
boolean |
— | |
onChange * |
(values: string[]) => boolean | void |
— | 单选点一项即交;多选点「应用」才交。返回 false 时留在浮层(如超出上限) |
onDone * |
(reason: 'select' | 'cancel' | 'tab') => void |
— | Tab 离开时保留正常焦点顺序,确认或取消时可把焦点交还给入口 |
searchPlaceholder |
string |
— | |
accessibilityLabel |
string |
— | |
anchorWidth |
number |
0 |
锚点宽度;浮层取它与 minWidth(默认 220)的较大值,最大 320(minWidth 超过 320 时以 minWidth 为准:自定义的宽行) |
minWidth |
number |
0 |
|
matchAnchor |
boolean |
— | 选择框用(SelectMenu):浮层与触发器同宽,内容放不下才按最长选项加宽(最大 320;触发器本身更宽时随触发器)。 不给时(筛选片这类窄锚点)按 minWidth 默认 220 起 |
searchable |
boolean |
— | 是否出搜索行;默认选项多于 10 个才出(小时、页大小这类纯数字列表传 false) |
leading |
(value: string) => JSX.Element |
— | 行内 label 左侧(在线点、头像);宽度由调用方给 |
trailing |
(value: string) => JSX.Element |
— | 行内 label 右侧、行尾勾的左边(接待量、已满);固定宽度 lane 由调用方给。里面的文字加 MENU_CURRENT_TEXT,当前行使用同一正文色 |
onSearch |
(query: string) => void |
— | 异步搜索(服务端答):给了就总出搜索行、不在本地过滤,每次输入回调(防抖由调用方做),结果换进 options |
loading |
boolean |
— | 搜索中:搜索行尾转圈,读屏报忙,不出「暂无选项」 |
status |
ComboboxStatus |
— | 列表顶上的一行提示;给了 action 时可用 ↑↓ 落上去、⏎ 执行(重试) |
renderOption |
(option: ComboboxOption, state: ComboboxOptionState) => JSX.Element |
— | 自定义行内容,替换 leading / label / trailing;行高(至少 32)、内边距、当前行淡底和行尾勾仍由组合框给,行内文字加 MENU_CURRENT_TEXT |
rowHeight |
(option: ComboboxOption) => number |
32 |
自定义行的估高(pt),异步模式用来算列表会不会滚 @default 32 |
* required
SelectMenu
Section titled “SelectMenu”| Prop | Type | Default | Description |
|---|---|---|---|
testID * |
string |
— | Base test identifier for the menu and its options. |
accessibilityLabel * |
string |
— | Spoken field name and title of the touch sheet. |
options * |
readonly ComboboxOption[] |
— | Choices in display order. |
value * |
string |
— | The currently selected option value. |
onChange * |
(value: string) => void |
— | Called when a different option is selected. |
trigger * |
JSX.Element | ((open: () => boolean) => JSX.Element) |
— | Element that opens the menu. The function form receives its open state to pass to SelectTrigger.expanded. |
minWidth |
number |
— | Minimum popup width. By default it matches the trigger and grows for long content, up to 320 points. |
searchable |
boolean |
— | Show the search field. By default it appears for more than ten options. |
placement |
'top' | 'bottom' |
'bottom' |
Open below the trigger by default. Use top for a field near the bottom of a window. |
disabled |
boolean |
— | Prevents the menu from opening. Set disabled on the trigger to show its disabled appearance. |
block |
boolean |
— | Fill the row. The trigger also needs flexible width. |
* required
ComboboxPopover
Section titled “ComboboxPopover”| Prop | Type | Default | Description |
|---|---|---|---|
testID * |
string |
— | |
options * |
readonly ComboboxOption[] |
— | |
value * |
readonly string[] |
— | |
multiple |
boolean |
— | |
onChange * |
(values: string[]) => boolean | void |
— | 单选点一项即交;多选点「应用」才交。返回 false 时留在浮层(如超出上限) |
searchPlaceholder |
string |
— | |
accessibilityLabel |
string |
— | |
anchorWidth |
number |
— | 锚点宽度;浮层取它与 minWidth(默认 220)的较大值,最大 320(minWidth 超过 320 时以 minWidth 为准:自定义的宽行) |
minWidth |
number |
— | |
matchAnchor |
boolean |
— | 选择框用(SelectMenu):浮层与触发器同宽,内容放不下才按最长选项加宽(最大 320;触发器本身更宽时随触发器)。 不给时(筛选片这类窄锚点)按 minWidth 默认 220 起 |
searchable |
boolean |
— | 是否出搜索行;默认选项多于 10 个才出(小时、页大小这类纯数字列表传 false) |
leading |
(value: string) => JSX.Element |
— | 行内 label 左侧(在线点、头像);宽度由调用方给 |
trailing |
(value: string) => JSX.Element |
— | 行内 label 右侧、行尾勾的左边(接待量、已满);固定宽度 lane 由调用方给。里面的文字加 MENU_CURRENT_TEXT,当前行使用同一正文色 |
onSearch |
(query: string) => void |
— | 异步搜索(服务端答):给了就总出搜索行、不在本地过滤,每次输入回调(防抖由调用方做),结果换进 options |
loading |
boolean |
— | 搜索中:搜索行尾转圈,读屏报忙,不出「暂无选项」 |
status |
ComboboxStatus |
— | 列表顶上的一行提示;给了 action 时可用 ↑↓ 落上去、⏎ 执行(重试) |
renderOption |
(option: ComboboxOption, state: ComboboxOptionState) => JSX.Element |
— | 自定义行内容,替换 leading / label / trailing;行高(至少 32)、内边距、当前行淡底和行尾勾仍由组合框给,行内文字加 MENU_CURRENT_TEXT |
rowHeight |
(option: ComboboxOption) => number |
32 |
自定义行的估高(pt),异步模式用来算列表会不会滚 @default 32 |
trigger * |
JSX.Element |
— | 触发器(芯片、按钮);按下开合 |
title * |
string |
— | 触控端面板标题:字段名 |
disabled |
boolean |
— |
* required
TouchComboboxSheet
Section titled “TouchComboboxSheet”| Prop | Type | Default | Description |
|---|---|---|---|
testID * |
string |
— | |
options * |
readonly ComboboxOption[] |
— | |
value * |
readonly string[] |
— | |
multiple |
boolean |
— | |
onChange * |
(values: string[]) => boolean | void |
— | 单选点一项即交;多选点「应用」才交。返回 false 时留在浮层(如超出上限) |
onDone * |
(reason: 'select' | 'cancel' | 'tab') => void |
— | Tab 离开时保留正常焦点顺序,确认或取消时可把焦点交还给入口 |
searchPlaceholder |
string |
— | |
accessibilityLabel |
string |
— | |
anchorWidth |
number |
— | 锚点宽度;浮层取它与 minWidth(默认 220)的较大值,最大 320(minWidth 超过 320 时以 minWidth 为准:自定义的宽行) |
minWidth |
number |
— | |
matchAnchor |
boolean |
— | 选择框用(SelectMenu):浮层与触发器同宽,内容放不下才按最长选项加宽(最大 320;触发器本身更宽时随触发器)。 不给时(筛选片这类窄锚点)按 minWidth 默认 220 起 |
searchable |
boolean |
— | 是否出搜索行;默认选项多于 10 个才出(小时、页大小这类纯数字列表传 false) |
leading |
(value: string) => JSX.Element |
— | 行内 label 左侧(在线点、头像);宽度由调用方给 |
trailing |
(value: string) => JSX.Element |
— | 行内 label 右侧、行尾勾的左边(接待量、已满);固定宽度 lane 由调用方给。里面的文字加 MENU_CURRENT_TEXT,当前行使用同一正文色 |
onSearch |
(query: string) => void |
— | 异步搜索(服务端答):给了就总出搜索行、不在本地过滤,每次输入回调(防抖由调用方做),结果换进 options |
loading |
boolean |
— | 搜索中:搜索行尾转圈,读屏报忙,不出「暂无选项」 |
status |
ComboboxStatus |
— | 列表顶上的一行提示;给了 action 时可用 ↑↓ 落上去、⏎ 执行(重试) |
renderOption |
(option: ComboboxOption, state: ComboboxOptionState) => JSX.Element |
— | 自定义行内容,替换 leading / label / trailing;行高(至少 32)、内边距、当前行淡底和行尾勾仍由组合框给,行内文字加 MENU_CURRENT_TEXT |
rowHeight |
(option: ComboboxOption) => number |
32 |
自定义行的估高(pt),异步模式用来算列表会不会滚 @default 32 |
open * |
boolean |
— | |
title * |
string |
— | 面板标题:字段名 |
* required
TouchValueRow
Section titled “TouchValueRow”| Prop | Type | Default | Description |
|---|---|---|---|
testID |
string |
— | |
label * |
string |
— | |
value |
string |
— | 当前值;空时显示 placeholder(灰) |
placeholder |
string |
— | |
labelAccessory |
JSX.Element |
— | 标签后的小标记(AI 回填) |
invalid |
boolean |
— | |
disabled |
boolean |
— | |
onPress |
() => void |
— |
* required