Skip to content

Commit

Permalink
fix: ComposerRoot onSubmit called using keyboard shortcuts (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 18, 2024
1 parent 890db7d commit a73b50f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-gorillas-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: ComposerRoot onSubmit should be called when using keyboard shortcuts
15 changes: 7 additions & 8 deletions packages/react/src/primitives/composer/ComposerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ComposerInput = forwardRef<
{ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest },
forwardedRef,
) => {
const { useThread, useViewport } = useThreadContext();
const { useThread } = useThreadContext();
const { useComposer, type } = useComposerContext();

const value = useComposer((c) => {
Expand All @@ -39,28 +39,27 @@ export const ComposerInput = forwardRef<

const Component = asChild ? Slot : TextareaAutosize;

const textareaRef = useRef<HTMLTextAreaElement>(null);
const ref = useComposedRefs(forwardedRef, textareaRef);

const handleKeyPress = (e: KeyboardEvent) => {
if (disabled) return;

const composer = useComposer.getState();
if (e.key === "Escape") {
if (useComposer.getState().cancel()) {
const composer = useComposer.getState();
if (composer.cancel()) {
e.preventDefault();
}
} else if (e.key === "Enter" && e.shiftKey === false) {
const isRunning = useThread.getState().isRunning;
if (!isRunning) {
e.preventDefault();
composer.send();

useViewport.getState().scrollToBottom();
textareaRef.current?.closest("form")?.requestSubmit();
}
}
};

const textareaRef = useRef<HTMLTextAreaElement>(null);
const ref = useComposedRefs(forwardedRef, textareaRef);

const autoFocusEnabled = autoFocus && !disabled;
const focus = useCallback(() => {
const textarea = textareaRef.current;
Expand Down

0 comments on commit a73b50f

Please sign in to comment.