AI助手与对话
EditComposer
@mox-hub/patterns · AI · 助手与对话 · canonical
edit-composer@mox-hub/patternsAI Executioncanonicalsrc/components/edit-composer.tsx变体演示edit-composer
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value* | string | — | — |
| onValueChange* | (value: string) => void | — | — |
| onSubmit* | (value: string) => void | — | — |
| onCancel* | () => void | — | — |
| busy* | boolean | false | — |
| placeholder* | string | — | — |
| labels* | Partial<EditComposerLabels> | — | — |
| className* | string | — | — |
源码
import { useId, type ComponentProps, type KeyboardEvent } from 'react';
import { cn } from '@mox-hub/ui';
import { Button } from '@mox-hub/ui/components/button';
import { Textarea } from '@mox-hub/ui/components/textarea';
import { usePatternLabels } from '../lib/i18n';
/**
* EditComposer —— 就地编辑用户消息(对话平面)。
*
* 编辑后重发的输入壳:
* - Esc 取消、Ctrl/Cmd+Enter 保存(Enter 保持换行,避免误提交长文本);
* - 空白内容不可保存;
* - `busy` 时保存键禁用并显示提交中语义;
* - 只持有输入展示,值由消费方受控;提交后是否截断后续分支由消费方决定。
*/
export interface EditComposerLabels {
region: string;
save: string;
cancel: string;
placeholder: string;
busy: string;
}
export const EDIT_COMPOSER_DEFAULT_LABELS: EditComposerLabels = {
region: '编辑消息',
save: '保存并重新发送',
cancel: '取消',
placeholder: '修改你的消息…',
busy: '提交中…',
};
export interface EditComposerProps
extends Omit<ComponentProps<'form'>, 'onSubmit' | 'children'> {
value: string;
onValueChange: (value: string) => void;
/** 保存(已 trim;空串不会触发) */
onSubmit: (value: string) => void;
onCancel: () => void;
busy?: boolean;
placeholder?: string;
labels?: Partial<EditComposerLabels>;
}
export function EditComposer({
value,
onValueChange,
onSubmit,
onCancel,
busy = false,
placeholder,
labels,
className,
...props
}: EditComposerProps) {
const D = usePatternLabels('editComposer', EDIT_COMPOSER_DEFAULT_LABELS);
const text: EditComposerLabels = {
region: labels?.region ?? D.region,
save: labels?.save ?? D.save,
cancel: labels?.cancel ?? D.cancel,
placeholder: labels?.placeholder ?? D.placeholder,
busy: labels?.busy ?? D.busy,
};
const inputId = useId();
const canSave = value.trim().length > 0 && !busy;
const save = () => {
const next = value.trim();
if (!next || busy) return;
onSubmit(next);
};
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === 'Escape') {
event.preventDefault();
onCancel();
return;
}
if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
event.preventDefault();
save();
}
};
return (
<form
role="group"
aria-label={text.region}
data-ai-component="edit.composer"
className={cn(
'rounded-lg border border-accent-blue/40 bg-background p-2 shadow-xs',
className,
)}
onSubmit={(event) => {
event.preventDefault();
save();
}}
{...props}
>
<label htmlFor={inputId} className="sr-only">
{text.region}
</label>
<Textarea
id={inputId}
autoFocus
value={value}
placeholder={placeholder ?? text.placeholder}
disabled={busy}
className="min-h-16 border-0 bg-transparent p-1 shadow-none focus-visible:ring-2 focus-visible:ring-ring/50 dark:bg-transparent"
onChange={(event) => onValueChange(event.target.value)}
onKeyDown={handleKeyDown}
/>
<div className="mt-1 flex items-center justify-end gap-1.5">
{busy ? (
<span className="me-auto text-2xs text-content-text-muted">
{text.busy}
</span>
) : null}
<Button type="button" variant="ghost" size="xs" onClick={onCancel}>
{text.cancel}
</Button>
<Button type="submit" size="xs" disabled={!canSave}>
{text.save}
</Button>
</div>
</form>
);
}
可访问性与 RTL
- 键盘:交互件保留
:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。 - RTL:间距、对齐与方向性图标使用逻辑属性 (
ms-*/text-start/rtl:-scale-x-100);通过ThemeProvider或I18nProvider的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。 - 文案:优先用组件
labels/translate/translatorprops,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。 - 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。
用法说明
用户消息就地编辑:Esc 取消、Ctrl/Cmd+Enter 保存(Enter 保持换行);空白不可保存,busy 时保存禁用。