Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @vitest-environment jsdom
import { useState } from "react";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { MobileRemoteWorkspacePrompt } from "./MobileRemoteWorkspacePrompt";

afterEach(() => {
cleanup();
});

describe("MobileRemoteWorkspacePrompt", () => {
it("focuses paths textarea and moves caret to end after selecting a recent path", async () => {
const recentPath = "/Users/vlad/dev/codex-monitor/cm";
function PromptHarness() {
const [value, setValue] = useState("");
return (
<MobileRemoteWorkspacePrompt
value={value}
error={null}
recentPaths={[recentPath]}
onChange={setValue}
onRecentPathSelect={(path) => {
setValue((prev) => (prev.length > 0 ? `${prev}\n${path}` : path));
}}
onCancel={vi.fn()}
onConfirm={vi.fn()}
/>
);
}
render(<PromptHarness />);

const recentPathButton = screen.getByRole("button", { name: recentPath });
fireEvent.click(recentPathButton);

const textarea = screen.getByLabelText("Paths");
await waitFor(() => {
expect(document.activeElement).toBe(textarea);
const expectedPosition = recentPath.length;
expect((textarea as HTMLTextAreaElement).selectionStart).toBe(expectedPosition);
expect((textarea as HTMLTextAreaElement).selectionEnd).toBe(expectedPosition);
});
});
});
18 changes: 16 additions & 2 deletions src/features/workspaces/components/MobileRemoteWorkspacePrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ export function MobileRemoteWorkspacePrompt({
onConfirm,
}: MobileRemoteWorkspacePromptProps) {
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const focusTextareaAtEnd = () => {
const textarea = textareaRef.current;
if (!textarea) {
return;
}
textarea.focus();
const end = textarea.value.length;
textarea.setSelectionRange(end, end);
};

useEffect(() => {
textareaRef.current?.focus();
focusTextareaAtEnd();
}, []);

return (
Expand Down Expand Up @@ -63,7 +72,12 @@ export function MobileRemoteWorkspacePrompt({
key={path}
type="button"
className="mobile-remote-workspace-modal-recent-item"
onClick={() => onRecentPathSelect(path)}
onClick={() => {
onRecentPathSelect(path);
requestAnimationFrame(() => {
focusTextareaAtEnd();
});
}}
>
{path}
</button>
Expand Down
Loading