通用的输入外框:圆角卡片、焦点描边、可删的引用 chip、随内容长高的多行输入、行内发送 / 停止钮与禁用态。用于 Copilot 提问、备注这类「打一段字发出去」的地方;会话输入区在它上面加了收起 / 展开(ChatComposerFrame)。不拥有草稿与发送逻辑。
状态:experimental · 组件:ComposerFrame
引入
定位到「引入」import { ComposerFrame } from 'nacre/chat'用法
定位到「用法」受控的 value / onChange;Return 走 onSubmit,Shift+Return 换行。输入从一行长到 maxHeight(默认 108)后内部滚动。给 onSend 才有行内发送钮,canSend 为 false 时禁用;chips 排在输入上方,每枚可删。
import { Text, View, signal } from 'nsh'import { TEXT_SIZE, TOUCH } from 'nacre'import { ComposerFrame } from 'nacre/chat'import type { ComposerChip } from 'nacre/chat'
export function ComposerUsage() { const text = signal('') const sent = signal<string[]>([]) const chips = signal<ComposerChip[]>([ { id: 'q1', text: '那中签率大概多少?' }, ]) const canSend = () => text().trim() !== '' || chips().length > 0 const send = () => { if (!canSend()) return sent.update((list) => [...list, text()]) text.set('') chips.set([]) } return ( <View class="w-[420px] max-w-full gap-2"> <ComposerFrame testID="demo-composer-frame" value={text()} onChange={(value) => text.set(value)} onSubmit={send} placeholder="问点什么" chips={chips()} onRemoveChip={(id) => chips.update((list) => list.filter((chip) => chip.id !== id))} onSend={send} sendIcon="sparkles" canSend={canSend()} /> <Text testID="demo-composer-frame-sent" class={`${TOUCH ? 'px-4' : ''} ${TEXT_SIZE.small} text-muted-foreground`}>{`$已发送 ${sent().length}`}</Text> </View> )}示例
定位到「示例」停止
定位到「停止」生成中(streaming)且给了 onStop 时,行尾的发送钮换成停止钮;停下后换回发送。
import { View, signal } from 'nsh'import { ComposerFrame } from 'nacre/chat'
export function ComposerStreaming() { const text = signal('') const streaming = signal(true) return ( <View class="w-[420px] max-w-full"> <ComposerFrame testID="demo-composer-streaming" value={text()} onChange={(value) => text.set(value)} placeholder="继续追问" streaming={streaming()} onStop={() => streaming.set(false)} onSend={() => streaming.set(true)} canSend={text().trim() !== ''} /> </View> )}禁用
定位到「禁用」disabled 只作用于输入区:底换 secondary、去掉描边,内置输入不可编辑、chip 不能删、内置发送不可点;放在 trailing / footer 里的应用按钮照常可点(会话结束时的「发起会话」)。
import { View, signal } from 'nsh'import { Button } from 'nacre'import { ComposerFrame } from 'nacre/chat'
export function ComposerDisabled() { const started = signal(false) return ( <View class="w-[420px] max-w-full h-[120px]"> <ComposerFrame testID="demo-composer-disabled" class="flex-1" disabled={!started()} stacked placeholder="会话已结束" footer={ <> <View class="flex-1" /> <Button testID="demo-composer-start" variant="primary" size="sm" onPress={() => started.set(true)}> 发起会话 </Button> </> } /> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Return | 在输入框里:提交(onSubmit) |
| Shift+Return | 在输入框里:换行 |
| Tab | 在 chip 的删除钮、输入框与发送钮之间移动 |
无障碍
定位到「无障碍」- 输入框读 accessibilityLabel(默认同 placeholder)。
- 发送、停止、chip 删除钮各有读屏名,默认取库内文案(发送、停止、移除)。
- 禁用时内置输入与发送报为不可用;外框本身不进读屏树。
该用 / 不该用
定位到「该用 / 不该用」该用
- 能不能发由应用判断,传 canSend
- chip 文字截断时悬停看全文(库已做)
- 禁用后在 trailing / footer 里给出下一步(发起会话)
不该用
- 只靠 disabled 外观拦住发送(应用的发送逻辑也要拦)
- 把草稿、队列逻辑放进外框
- 会话输入区直接用它(用 ChatComposerFrame,带收起 / 展开)
API
定位到「API」ComposerFrame
定位到「ComposerFrame」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
focused |
boolean |
— | 编辑器或框里的按钮有焦点:描边换 ring 50% + 15% 光晕。不给时跟内置输入框的焦点走 |
disabled |
boolean |
— | 输入区禁用:底换 secondary、去掉描边,内置输入框不可编辑、chip 不能删、内置发送不可点; leading / trailing / footer 里应用自己的按钮不受影响(会话已结束时的「发起会话」) |
align |
'end' | 'center' |
— | 一行里各件的纵向对齐:end(默认,输入长高时按钮留在末行)/ center(固定一行高的收起态) |
overlay |
JSX.Element |
— | 叠在框上的浮层锚点(联想、写作助手);放在最前,不占排版 |
chips |
readonly ComposerChip[] |
— | 可删的引用 chip,排在输入上方 |
onRemoveChip |
(id: string) => void |
— | |
removeChipLabel |
string |
— | chip 删除钮的读屏名,默认「移除」 |
header |
JSX.Element |
— | chip 区下方、输入上方的自由内容(引用条、待发附件) |
leading |
JSX.Element |
— | 横排时输入前(选图 +) |
trailing |
JSX.Element |
— | 横排时输入后、内置发送钮前(快捷键提示、应用自己的发送) |
stacked |
boolean |
— | 竖排:chip 与 header、输入、footer 上下排,leading / trailing / 内置发送不显示;默认横排一行 |
footer |
JSX.Element |
— | 竖排时的底部一行(工具栏、发送提示、发送) |
onFooterWidth |
(width: number) => void |
— | 底部一行的宽(窄时应用可以藏掉发送提示) |
onPress |
() => void |
— | 点框里的空白处:应用把焦点交给编辑器 |
value |
string |
— | 内置输入框的值(受控);给了 children 时不用 |
onChange |
(value: string) => void |
— | |
onSubmit |
() => void |
— | Return 提交(Shift+Return 换行);能不能发由应用判断 |
placeholder |
string |
— | |
accessibilityLabel |
string |
— | 内置输入框的读屏名,默认同 placeholder |
inputRef |
(node: InputRef) => void |
— | |
maxHeight |
number |
108 |
内置输入框随内容长高的上限(pt),超过后内部滚动 |
onSend |
() => void |
— | 给了就在行尾放内置发送钮(主色圆钮) |
sendLabel |
string |
— | 发送钮读屏名,默认「发送」 |
sendIcon |
IconName |
'send' |
发送钮图标,默认 send |
canSend |
boolean |
— | 能不能发(有内容、身份就绪);false 时发送钮禁用 |
streaming |
boolean |
— | 生成中:有 onStop 时行尾换成停止钮 |
onStop |
() => void |
— | |
stopLabel |
string |
— | 停止钮读屏名,默认「停止」 |
class |
string |
— | 根节点的布局类(外边距、flex),不改颜色 |
testID |
string |
— | 根节点;输入 ${testID}-input、发送 ${testID}-send、停止 ${testID}-stop、chip 区 ${testID}-chips、chip ${testID}-chip、删除 ${testID}-chip-remove |
children |
JSX.Element |
— | 自带的编辑器(富文本);不给时用内置的多行输入框 |
* 必填