Skip to content

Commit

Permalink
Fix pr #4832: Fix issue #4830: [Bug]: Copy-paste into the "What do yo…
Browse files Browse the repository at this point in the history
…u want to build?" bar doesn't work
  • Loading branch information
openhands-agent committed Nov 7, 2024
1 parent 66dfedf commit 9987ff4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
35 changes: 34 additions & 1 deletion frontend/__tests__/components/chat/chat-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import userEvent from "@testing-library/user-event";
import { render, screen } from "@testing-library/react";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, afterEach, vi, it, expect } from "vitest";
import { ChatInput } from "#/components/chat-input";

Expand Down Expand Up @@ -158,4 +158,37 @@ describe("ChatInput", () => {
await user.tab();
expect(onBlurMock).toHaveBeenCalledOnce();
});

it("should handle text paste correctly", async () => {
const user = userEvent.setup();
const onChangeMock = vi.fn();
render(<ChatInput onSubmit={onSubmitMock} onChange={onChangeMock} />);
const textarea = screen.getByRole("textbox");

// Simulate pasting text using userEvent
await user.click(textarea);
await user.paste("Pasted text");

// onChange should be called with the pasted text
expect(onChangeMock).toHaveBeenCalledWith("Pasted text");
});

it("should handle image paste correctly", async () => {
const onImagePasteMock = vi.fn();
render(<ChatInput onSubmit={onSubmitMock} onImagePaste={onImagePasteMock} />);
const textarea = screen.getByRole("textbox");

// Create a mock image file
const imageFile = new File([''], 'test.png', { type: 'image/png' });
const clipboardData = {
files: [imageFile],
getData: () => '',
};

// Simulate paste event
fireEvent.paste(textarea, { clipboardData });

// onImagePaste should be called with the image file
expect(onImagePasteMock).toHaveBeenCalledWith([imageFile]);
});
});
18 changes: 2 additions & 16 deletions frontend/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,11 @@ export function ChatInput({
if (files.length > 0) {
event.preventDefault();
onImagePaste(files);
return;
}
}

// Handle text paste
const text = event.clipboardData.getData('text');
if (text && textareaRef.current) {
event.preventDefault();
const start = textareaRef.current.selectionStart;
const end = textareaRef.current.selectionEnd;
const currentValue = textareaRef.current.value;
const newValue = currentValue.substring(0, start) + text + currentValue.substring(end);
textareaRef.current.value = newValue;
// Update cursor position
const newPosition = start + text.length;
textareaRef.current.setSelectionRange(newPosition, newPosition);
// Trigger onChange if provided
onChange?.(newValue);
}
// For text paste, let the default behavior handle it
// The onChange handler will be called automatically
};

const handleDragOver = (event: React.DragEvent<HTMLTextAreaElement>) => {
Expand Down

0 comments on commit 9987ff4

Please sign in to comment.