diff --git a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx b/apps/web/src/components/pullRequest/PullRequestListFilters.tsx index 479bd481e03aa..58dc689343bd1 100644 --- a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx +++ b/apps/web/src/components/pullRequest/PullRequestListFilters.tsx @@ -116,8 +116,10 @@ export function PullRequestSearchInput({ {busy ? : } + {/* Native search inputs clear on Escape even during IME composition. */} setDraft(value)} onBlur={() => setDraft(null)} @@ -129,9 +131,16 @@ export function PullRequestSearchInput({ } }} onKeyDown={(event) => { - if (event.key === "Enter" && !event.nativeEvent.isComposing) { - onSubmit(event.currentTarget.value); - } + const composing = event.nativeEvent.isComposing || event.keyCode === 229; + if (event.key === "Enter" && !composing) onSubmit(event.currentTarget.value); + if (event.key !== "Escape" || event.defaultPrevented) return; + if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return; + if (!composing && !event.currentTarget.value) return; + // The pull requests page leaves on an Escape that reaches it. + event.stopPropagation(); + if (composing) return; + setDraft(""); + onChange(""); }} placeholder={placeholder} aria-label={label} diff --git a/apps/web/src/components/pullRequest/PullRequestsColumn.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestsColumn.browser.test.tsx index 1e4de32289e69..fbf4c55d763c4 100644 --- a/apps/web/src/components/pullRequest/PullRequestsColumn.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestsColumn.browser.test.tsx @@ -1,9 +1,14 @@ -import { createRef, type RefObject } from "react"; -import { describe, expect, it } from "vite-plus/test"; +import { createRef, type RefObject, useState } from "react"; +import { page, userEvent } from "vite-plus/test/browser"; +import { describe, expect, it, vi } from "vite-plus/test"; -import { render } from "../../../test/render"; +import { act, render } from "../../../test/render"; +import { registerCommands } from "../../shortcuts/dispatcher"; +import { PullRequestSearchInput } from "./PullRequestListFilters"; import { PullRequestsColumn } from "./PullRequestsColumn"; +import "~/index.css"; + function createColumn(maximizedAway: boolean, scrollRef: RefObject) { return ( { .toHaveFocus(); }); }); + +function CondensedSearchColumn({ + scrollRef, + onSubmit, +}: { + scrollRef: RefObject; + onSubmit: (value: string) => void; +}) { + const [query, setQuery] = useState("label:bug"); + return ( +
+ {}} + searchValue={query} + involvement="all" + state="open" + host={undefined} + hostMenuOptions={[]} + onInvolvement={() => {}} + onState={() => {}} + onHost={() => {}} + searchInput={ + + } + sortMenu={null} + filtersMenu={null} + savedViewsMenu={null} + rightPanelControl={null} + titlebarControls={null} + rightPanelOpen={false} + maximizedAway={false} + listBody={
} + scrollRef={scrollRef} + /> + +
+ ); +} + +describe("PullRequestsColumn condensed search", () => { + it("Escape clears the topbar search before the page's Escape leaves", async () => { + const onSubmit = vi.fn(); + const scrollRef = createRef(); + await using _screen = await render( + , + ); + // Mirrors the /pull-requests observer, which leaves on an unclaimed Escape. + const leave = vi.fn(); + using stack = new DisposableStack(); + stack.defer( + registerCommands( + {}, + { + observe: (event, consumed) => { + if (!consumed && !event.repeat && !event.isComposing && event.key === "Escape") leave(); + }, + }, + ), + ); + scrollRef.current?.scrollTo({ top: 1000 }); + const searches = page.getByRole("searchbox", { name: "Search pull requests" }); + // The topbar copy comes first and mounts once the header condenses. + await expect.element(searches.nth(1)).toBeInTheDocument(); + const search = searches.nth(0); + // Safari ends the composition just before the confirming keydown, reporting only keyCode 229. + const pressDuringComposition = (key: string) => + act(() => { + for (const composition of [{ isComposing: true }, { keyCode: 229 }]) { + search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })); + search + .element() + .dispatchEvent(new KeyboardEvent("keydown", { key, ...composition, bubbles: true })); + } + }); + + await userEvent.click(search); + await pressDuringComposition("Enter"); + await pressDuringComposition("Escape"); + expect(onSubmit).not.toHaveBeenCalled(); + expect(leave).not.toHaveBeenCalled(); + await expect.element(search).toHaveValue("label:bug"); + + await userEvent.keyboard("{Escape}"); + await expect.element(search).toHaveValue(""); + await expect.element(search).toHaveFocus(); + expect(leave).not.toHaveBeenCalled(); + + await userEvent.keyboard("{Escape}"); + expect(leave).toHaveBeenCalledOnce(); + + await userEvent.click(page.getByRole("button", { name: "Elsewhere" })); + await expect + .element(page.getByRole("button", { name: "Search pull requests" })) + .toBeInTheDocument(); + }); +}); diff --git a/apps/web/src/components/pullRequest/PullRequestsColumn.tsx b/apps/web/src/components/pullRequest/PullRequestsColumn.tsx index d4ca6d0d3b7e2..6cd38913860f2 100644 --- a/apps/web/src/components/pullRequest/PullRequestsColumn.tsx +++ b/apps/web/src/components/pullRequest/PullRequestsColumn.tsx @@ -48,10 +48,14 @@ function ExpandableSearch({
onFocusWithin?.(true)} + onFocus={() => { + onFocusWithin?.(true); + // Open while focused, so Escape clearing the query does not unmount the field. + onOpenChange(true); + }} onBlur={() => { onFocusWithin?.(false); - if (searchValue.length === 0) onOpenChange(false); + onOpenChange(false); }} > {searchInput}