Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix ChatComposerInput child onChange submit flow (#3640)

Dustin Persek4c1cbab4ea70
diff --git a/.changeset/chatcomposerinput-preserves-composer-submit-with-nby1.md b/.changeset/chatcomposerinput-preserves-composer-submit-with-nby1.md
new file mode 100644
index 0000000000..5ba1208b25
--- /dev/null
+++ b/.changeset/chatcomposerinput-preserves-composer-submit-with-nby1.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] ChatComposerInput preserves composer submit with child onChange (#2330)
+@syntaxsawdust
diff --git a/packages/core/src/Chat/ChatComposerInput.test.tsx b/packages/core/src/Chat/ChatComposerInput.test.tsx
index c7015bded5..a65c0d53a3 100644
--- a/packages/core/src/Chat/ChatComposerInput.test.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.test.tsx
@@ -2,6 +2,7 @@
import {describe, it, expect, vi, afterEach} from 'vitest';
import {render, screen, fireEvent} from '@testing-library/react';
+import {ChatComposer} from './ChatComposer';
import {ChatComposerInput} from './ChatComposerInput';
import type {
ChatComposerTrigger,
@@ -129,6 +130,29 @@ describe('ChatComposerInput', () => {
fireEvent.keyDown(textbox, {key: 'Enter'});
expect(onSubmit).not.toHaveBeenCalled();
});
+
+ it('keeps parent submit flow when child onChange observes input changes', () => {
+ const onSubmit = vi.fn();
+ const onInputChange = vi.fn();
+ render(
+ <ChatComposer
+ onSubmit={onSubmit}
+ input={<ChatComposerInput onChange={onInputChange} />}
+ />,
+ );
+
+ const textbox = screen.getByRole('textbox');
+ textbox.textContent = 'hello world';
+ fireEvent.input(textbox);
+
+ expect(onInputChange).toHaveBeenLastCalledWith('hello world');
+
+ fireEvent.keyDown(textbox, {key: 'Enter'});
+
+ expect(onSubmit).toHaveBeenCalledWith('hello world');
+ expect(onInputChange).toHaveBeenLastCalledWith('');
+ expect(textbox.textContent).toBe('');
+ });
});
// Controlled-value sync used to overwrite `textContent` on every
diff --git a/packages/core/src/Chat/ChatComposerInput.tsx b/packages/core/src/Chat/ChatComposerInput.tsx
index 25c129ffb6..6814ea6eef 100644
--- a/packages/core/src/Chat/ChatComposerInput.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.tsx
@@ -283,12 +283,13 @@ function serialize(node: Node): string {
export function ChatComposerInput(props: ChatComposerInputProps) {
const composerCtx = useChatComposerContext();
+ const hasControlledValueProp = props.value !== undefined;
const {
ref,
handleRef,
value: controlledValue = composerCtx?.value,
- onChange = composerCtx?.onChange,
+ onChange: onChangeProp,
placeholder = composerCtx?.placeholder ?? 'Type a message\u2026',
maxRows = 8,
triggers,
@@ -306,6 +307,21 @@ export function ChatComposerInput(props: ChatComposerInputProps) {
...rest
} = props;
+ const composerOnChange = composerCtx?.onChange;
+ const onChange = useCallback(
+ (nextValue: string) => {
+ if (hasControlledValueProp) {
+ onChangeProp?.(nextValue);
+ return;
+ }
+ composerOnChange?.(nextValue);
+ if (onChangeProp !== composerOnChange) {
+ onChangeProp?.(nextValue);
+ }
+ },
+ [composerOnChange, hasControlledValueProp, onChangeProp],
+ );
+
const editableRef = useRef<HTMLDivElement>(null);
const selfRef = useRef<ChatComposerInputHandle>(null);
const [isEmpty, setIsEmpty] = useState(true);