fix(vcs): branch pickers in the Git dialogs filter as you type (#3816)

Merge, Rebase (upstream and --onto), Pull's remote branch and New Worktree's checkout used plain non-searchable Selects, so repositories with hundreds of branches could only be scrolled. They now share a BranchSelect that is always searchable, and a searchable Select opens its list with the typed key as the query when a key is pressed on the closed trigger. The agents' base-branch picker is searchable too.

Closes #3788

Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com>
This commit is contained in:
Bert Verhelst
2026-11-17 01:58:49 +08:00
committed by GitHub
co-authored by bertyhell
parent 38dfa37c88
commit d84d8829c4
5 changed files with 114 additions and 10 deletions
+1
View File
@@ -280,6 +280,7 @@ export function HomeEditor({ docked = false, input }: { docked?: boolean; input?
<Select
variant="ghost"
aria-label="Base branch"
searchable
value={base}
options={branches.map((b) => ({ value: b.name, label: b.name, icon: "branch" }))}
onChange={(b) => updateDraft({ base: b === current ? null : b })}
@@ -11,7 +11,9 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
const ok = (data: unknown) => Promise.resolve({ status: "ok" as const, data });
const head = (repo: string | null) => (repo === "/p/b" ? "dev" : "main");
const local = (repo: string | null) =>
repo === "/p/b" ? ["dev", "feature-b", "shared"] : ["main", "feature-a", "shared"];
repo === "/p/b"
? ["dev", "feature-b", "shared"]
: ["main", "feature-a", "shared", "feature/REQ-126-明细汇总多选", "develop"];
return {
...real,
commands: {
@@ -104,3 +106,30 @@ it("clears the chosen branch when another repository is picked", async () => {
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(mocks.merges).toEqual([]);
});
it("filters the branch list as the user types and picks the top match with Enter", async () => {
useVcs.setState({ roots: ["/p"], repo: null });
render(<VcsDialogHost />);
await act(() => showMergeDialog());
expect(await screen.findByText("Merge into main")).toBeInTheDocument();
// Typing on the closed picker opens it with that text as the search.
const trigger = screen.getByRole("button", { name: "Branch to merge" });
trigger.focus();
fireEvent.keyDown(trigger, { key: "1" });
const search = await screen.findByRole("textbox");
expect(search).toHaveValue("1");
for (const query of ["126", "明细", "req"]) {
fireEvent.change(search, { target: { value: query } });
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["feature/REQ-126-明细汇总多选"]);
}
fireEvent.keyDown(search, { key: "Enter" });
expect(screen.queryByRole("listbox")).toBeNull();
expect(trigger).toHaveTextContent("feature/REQ-126-明细汇总多选");
// Enter chose the branch; it did not also run the dialog's Merge button.
expect(mocks.merges).toEqual([]);
fireEvent.click(screen.getByRole("button", { name: "Merge" }));
await waitFor(() => expect(mocks.merges).toHaveLength(1));
expect(mocks.merges[0]).toMatchObject({ branches: ["feature/REQ-126-明细汇总多选"] });
});
+17 -5
View File
@@ -17,6 +17,7 @@ import {
Tree,
useSanitizedField,
type SelectOption,
type SelectProps,
} from "@/ui";
import { sortLocal, sortRemote } from "./branches";
import { conflictActions, openConflictsInstead, validateBranchName } from "./branchOps";
@@ -119,6 +120,11 @@ function useBranches(repo: string | null): Branch[] {
const branchOptions = (list: readonly Branch[]): SelectOption[] =>
list.filter((b) => !b.name.endsWith("/HEAD")).map((b) => ({ value: b.name, label: b.name, icon: "branch" }));
/** Every branch choice in these dialogs: repositories hold hundreds of branches, so type to filter, Enter picks. */
function BranchSelect(props: Omit<SelectProps, "searchable">) {
return <Select {...props} searchable />;
}
/** Remote branches of `remote` without the remote prefix (`origin/main` → `main`). */
export function remoteBranchesOf(branches: readonly Branch[], remote: string): string[] {
const prefix = `${remote}/`;
@@ -255,7 +261,7 @@ function PullDialog({ initialRepo, onClose }: { initialRepo?: string | null; onC
options={(remotes ?? []).map((r) => ({ value: r.name, label: r.name, detail: r.url }))}
onChange={setRemote}
/>
<Select
<BranchSelect
aria-label="Remote branch"
className={styles.grow}
value={branch}
@@ -333,7 +339,7 @@ function MergeDialog({ initialRepo, onClose }: { initialRepo?: string | null; on
<div className={styles.fieldLabel}>
<T>Branch to merge:</T>
</div>
<Select
<BranchSelect
aria-label="Branch to merge"
value={branch}
placeholder="Select branch"
@@ -424,7 +430,7 @@ function RebaseDialog({ initialRepo, onClose }: { initialRepo?: string | null; o
<div className={styles.fieldLabel}>
{useOnto ? tx`Rebase commits of ${head ?? "HEAD"} not on:` : `Rebase ${head ?? "HEAD"} onto:`}
</div>
<Select
<BranchSelect
aria-label="Upstream"
value={upstream}
placeholder="Select branch"
@@ -433,7 +439,13 @@ function RebaseDialog({ initialRepo, onClose }: { initialRepo?: string | null; o
/>
<Checkbox label="--onto" checked={useOnto} onChange={setUseOnto} />
{useOnto && (
<Select aria-label="New base" value={onto} placeholder="New base" options={options} onChange={setOnto} />
<BranchSelect
aria-label="New base"
value={onto}
placeholder="New base"
options={options}
onChange={setOnto}
/>
)}
<Checkbox label="--rebase-merges" checked={rebaseMerges} onChange={setRebaseMerges} />
<Checkbox label="--keep-empty" checked={keepEmpty} onChange={setKeepEmpty} />
@@ -913,7 +925,7 @@ function NewWorktreeDialog({
<div className={styles.fieldLabel}>
<T>Check out:</T>
</div>
<Select
<BranchSelect
aria-label="Check out"
value={source}
placeholder="Select branch"
+33
View File
@@ -100,6 +100,39 @@ describe("Select", () => {
key("Enter");
expect(changed).toHaveBeenCalledExactlyOnceWith("other/BlueMoon");
});
it("opens a searchable list from a key typed on the closed trigger, with that key as the query", async () => {
const onChange = vi.fn();
render(
<Select
aria-label="Branch"
options={[
{ value: "main", label: "main" },
{ value: "develop", label: "develop" },
{ value: "feature/dev-tools", label: "feature/dev-tools" },
]}
value="main"
searchable
onChange={onChange}
/>,
);
const trigger = screen.getByRole("button", { name: "Branch" });
fireEvent.keyDown(trigger, { key: "d", ctrlKey: true });
fireEvent.keyDown(trigger, { key: "Shift" });
expect(screen.queryByRole("listbox")).toBeNull();
fireEvent.keyDown(trigger, { key: "d" });
const search = screen.getByRole("textbox");
expect(search).toHaveValue("d");
fireEvent.change(search, { target: { value: "dev" } });
expect(labels()).toEqual(["develop", "feature/dev-tools"]);
key("Enter", search);
expect(onChange).toHaveBeenCalledExactlyOnceWith("develop");
expect(screen.queryByRole("listbox")).toBeNull();
});
it("leaves typed keys to type-ahead when the list is not searchable", () => {
render(<Select aria-label="Model" options={options} value={null} onChange={() => {}} />);
fireEvent.keyDown(screen.getByRole("button", { name: "Model" }), { key: "b" });
expect(screen.queryByRole("listbox")).toBeNull();
});
it("does not confuse a heading value with a real option value", () => {
const changed = setup(false, [
{ value: "a", label: "Provider", section: true },
+33 -4
View File
@@ -34,7 +34,7 @@ export interface SelectProps<V extends string = string> {
"aria-label"?: string;
/** Custom trigger content. */
renderValue?: (option: SelectOption<V> | undefined) => ReactNode;
/** Show a search box in the popup (useful for long option lists). */
/** Show a search box in the popup (useful for long option lists); typing on the closed trigger opens it with that text. */
searchable?: boolean;
/** The option under the pointer or keyboard while the list is open; `null` once it closes. */
onPreview?: (value: V | null) => void;
@@ -59,6 +59,8 @@ export interface SelectListboxProps<V extends string = string> {
/** Called with the chosen option; closing, if any, is the caller's. */
onChoose: (value: V) => void;
searchable?: boolean;
/** Search text the list opens with. */
initialQuery?: string;
onPreview?: (value: V | null) => void;
/** Optional trailing action control rendered at the end of a non-section row. */
renderAction?: (option: SelectOption<V>) => ReactNode;
@@ -73,6 +75,7 @@ export function SelectListbox<V extends string = string>({
options,
onChoose,
searchable = false,
initialQuery = "",
onPreview,
renderAction,
listClassName,
@@ -86,7 +89,8 @@ export function SelectListbox<V extends string = string>({
options.findIndex((o) => !o.section && !o.disabled && o.value === value),
),
);
const [query, setQuery] = useState("");
const [query, setQuery] = useState(initialQuery);
const caretPlaced = useRef(false);
const typeAhead = useRef({ text: "", at: -Infinity });
const isSection = (o: SelectOption<V>) => !!o.section;
const unavailable = (o: SelectOption<V>) => !!(o.section || o.disabled);
@@ -208,6 +212,13 @@ export function SelectListbox<V extends string = string>({
value={query}
autoFocus
data-autofocus
onFocus={(e) => {
// Keep typing after the text that opened the list instead of before it.
if (caretPlaced.current) return;
caretPlaced.current = true;
const end = e.currentTarget.value.length;
e.currentTarget.setSelectionRange(end, end);
}}
placeholder={tx("Search…")}
onChange={(v) => {
setQuery(v);
@@ -295,9 +306,13 @@ export function Select<V extends string = string>({
}: SelectProps<V>) {
const given = useGivenTx();
const [open, setOpen] = useState(false);
const [initialQuery, setInitialQuery] = useState("");
const triggerRef = useRef<HTMLButtonElement>(null);
const current = options.find((o) => !o.section && o.value === value);
const openPopup = () => setOpen(true);
const openPopup = (query = "") => {
setInitialQuery(query);
setOpen(true);
};
// Selection can update the callback as the list unmounts; use the committed trigger props.
const previewRef = useRef(onPreview);
useLayoutEffect(() => {
@@ -318,9 +333,22 @@ export function Select<V extends string = string>({
className={clsx(styles.trigger, styles[variant], className)}
onClick={() => (open ? setOpen(false) : openPopup())}
onKeyDown={(e) => {
if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
if (open) return;
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
openPopup();
} else if (
searchable &&
e.key.length === 1 &&
e.key !== " " &&
!e.ctrlKey &&
!e.metaKey &&
!e.altKey &&
!e.nativeEvent.isComposing
) {
// Type to search: the key starts the query instead of being lost on the button.
e.preventDefault();
openPopup(e.key);
}
}}
>
@@ -348,6 +376,7 @@ export function Select<V extends string = string>({
value={value}
options={options}
searchable={searchable}
initialQuery={initialQuery}
onPreview={preview}
aria-label={aria["aria-label"]}
onChoose={(v) => {