Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/elements/__tests__/prompt-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,51 @@ describe("promptInputTextarea", () => {
expect(onSubmit).not.toHaveBeenCalled();
});

it("does not submit on Enter while streaming - #439", async () => {
setupPromptInputTests();
const onSubmit = vi.fn();
const user = userEvent.setup();

render(
<PromptInput onSubmit={onSubmit}>
<PromptInputBody>
<PromptInputTextarea />
<PromptInputSubmit onStop={vi.fn()} status="streaming" />
</PromptInputBody>
</PromptInput>
);

const textarea = screen.getByPlaceholderText(
"What would you like to know?"
);
await user.type(textarea, "Test");
await user.keyboard("{Enter}");

expect(onSubmit).not.toHaveBeenCalled();
});

it("does not submit on Enter without a submit button - #439", async () => {
setupPromptInputTests();
const onSubmit = vi.fn();
const user = userEvent.setup();

render(
<PromptInput onSubmit={onSubmit}>
<PromptInputBody>
<PromptInputTextarea />
</PromptInputBody>
</PromptInput>
);

const textarea = screen.getByPlaceholderText(
"What would you like to know?"
);
await user.type(textarea, "Test");
await user.keyboard("{Enter}");

expect(onSubmit).not.toHaveBeenCalled();
});

it("does not submit on Enter during IME composition - #21", () => {
setupPromptInputTests();
const onSubmit = vi.fn();
Expand Down
7 changes: 5 additions & 2 deletions packages/elements/src/prompt-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -983,12 +983,15 @@ export const PromptInputTextarea = ({
}
e.preventDefault();

// Check if the submit button is disabled before submitting
// Check if the submit button is disabled before submitting.
// No submit button in the form means the input is not submittable
// right now (e.g. PromptInputSubmit renders as a stop button while
// streaming), so block Enter in that case too.
const { form } = e.currentTarget;
const submitButton = form?.querySelector(
'button[type="submit"]'
) as HTMLButtonElement | null;
if (submitButton?.disabled) {
if (!submitButton || submitButton.disabled) {
return;
}

Expand Down