π OpenHands: AI-Driven Development
fix(frontend): auto-focus chat input on mount (#518)
Hiep Lecd7def89db68
diff --git a/__tests__/components/features/chat/components/chat-input-field.test.tsx b/__tests__/components/features/chat/components/chat-input-field.test.tsxnew file mode 100644index 0000000000..2d93981591--- /dev/null+++ b/__tests__/components/features/chat/components/chat-input-field.test.tsx@@ -0,0 +1,41 @@+import React from "react";+import { screen } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import { renderWithProviders } from "test-utils";+import { ChatInputField } from "#/components/features/chat/components/chat-input-field";++function Harness({ disabled }: { disabled: boolean }) {+ const ref = React.useRef<HTMLDivElement | null>(null);+ return (+ <ChatInputField+ chatInputRef={ref}+ disabled={disabled}+ onInput={vi.fn()}+ onPaste={vi.fn()}+ onKeyDown={vi.fn()}+ />+ );+}++describe("ChatInputField auto-focus", () => {+ it("focuses the chat input on mount when enabled", () => {+ renderWithProviders(<Harness disabled={false} />);++ expect(screen.getByTestId("chat-input")).toBe(document.activeElement);+ });++ it("does not focus the chat input on mount when disabled", () => {+ renderWithProviders(<Harness disabled />);++ expect(screen.getByTestId("chat-input")).not.toBe(document.activeElement);+ });++ it("does not steal focus when disabled flips from true to false", () => {+ const { rerender } = renderWithProviders(<Harness disabled />);+ document.body.focus();++ rerender(<Harness disabled={false} />);++ expect(screen.getByTestId("chat-input")).not.toBe(document.activeElement);+ });+});diff --git a/src/components/features/chat/components/chat-input-field.tsx b/src/components/features/chat/components/chat-input-field.tsxindex f42db5b0c4..3494a63284 100644--- a/src/components/features/chat/components/chat-input-field.tsx+++ b/src/components/features/chat/components/chat-input-field.tsx@@ -31,6 +31,14 @@ export function ChatInputField({const isPlanMode = conversationMode === "plan";+ React.useEffect(() => {+ if (!disabled) {+ chatInputRef.current?.focus();+ }+ // Focus on mount only β re-focusing on later `disabled` transitions+ // would steal focus from a user who has clicked elsewhere.+ }, []);+return (<divclassName="box-border content-stretch flex flex-row items-center justify-start min-h-6 p-0 relative shrink-0 flex-1"