From c8c3ee9c07a91e9a4229bca0ab5cc8845615df54 Mon Sep 17 00:00:00 2001 From: Dotta Date: Sat, 6 Jun 2026 18:41:37 +0000 Subject: [PATCH] Fix mobile inline selector focus Co-Authored-By: Paperclip --- .../components/InlineEntitySelector.test.tsx | 60 ++++++++++++++++++- ui/src/components/InlineEntitySelector.tsx | 10 +--- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/ui/src/components/InlineEntitySelector.test.tsx b/ui/src/components/InlineEntitySelector.test.tsx index f127f41c330..270adc54d8f 100644 --- a/ui/src/components/InlineEntitySelector.test.tsx +++ b/ui/src/components/InlineEntitySelector.test.tsx @@ -1,22 +1,33 @@ // @vitest-environment jsdom -import { act } from "react"; import { createRoot } from "react-dom/client"; +import { flushSync } from "react-dom"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { InlineEntitySelector } from "./InlineEntitySelector"; // eslint-disable-next-line @typescript-eslint/no-explicit-any (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; +async function act(callback: () => void | Promise) { + let result: void | Promise = undefined; + flushSync(() => { + result = callback(); + }); + await result; +} + describe("InlineEntitySelector", () => { let container: HTMLDivElement; + let originalMatchMedia: typeof window.matchMedia; beforeEach(() => { + originalMatchMedia = window.matchMedia; container = document.createElement("div"); document.body.appendChild(container); }); afterEach(() => { + window.matchMedia = originalMatchMedia; container.remove(); document.body.innerHTML = ""; }); @@ -77,4 +88,51 @@ describe("InlineEntitySelector", () => { root.unmount(); }); }); + + it("focuses the search input when opened on coarse pointers", async () => { + window.matchMedia = vi.fn().mockImplementation((query: string) => ({ + matches: query === "(pointer: coarse)", + media: query, + onchange: null, + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + addListener: vi.fn(), + removeListener: vi.fn(), + dispatchEvent: vi.fn(), + })); + + const root = createRoot(container); + + act(() => { + root.render( + , + ); + }); + + const trigger = container.querySelector("button") as HTMLButtonElement | null; + expect(trigger).not.toBeNull(); + + await act(async () => { + trigger?.dispatchEvent(new MouseEvent("click", { bubbles: true })); + }); + + const searchInput = document.querySelector('input[placeholder="Search assignees..."]') as HTMLInputElement | null; + expect(searchInput).not.toBeNull(); + expect(document.activeElement).toBe(searchInput); + + act(() => { + root.unmount(); + }); + }); }); diff --git a/ui/src/components/InlineEntitySelector.tsx b/ui/src/components/InlineEntitySelector.tsx index 0ac378b5fee..58fea66970b 100644 --- a/ui/src/components/InlineEntitySelector.tsx +++ b/ui/src/components/InlineEntitySelector.tsx @@ -135,15 +135,7 @@ export const InlineEntitySelector = forwardRef { event.preventDefault(); - // On touch devices, don't auto-focus the search input to avoid - // opening the virtual keyboard which reshapes the viewport and - // pushes the popover off-screen. - const isTouch = typeof window.matchMedia === "function" - ? window.matchMedia("(pointer: coarse)").matches - : false; - if (!isTouch) { - inputRef.current?.focus(); - } + inputRef.current?.focus(); }} onCloseAutoFocus={(event) => { if (!shouldPreventCloseAutoFocusRef.current) return;