diff --git a/web/src/pages/setup-wizard/StepFrame.test.tsx b/web/src/pages/setup-wizard/StepFrame.test.tsx new file mode 100644 index 0000000000..a894f3703d --- /dev/null +++ b/web/src/pages/setup-wizard/StepFrame.test.tsx @@ -0,0 +1,46 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it } from "vitest"; + +import { StepFrame } from "./StepFrame"; + +function step(busy: boolean) { + return ( + e.preventDefault()}> + + + + ); +} + +describe("StepFrame", () => { + it("locks the step's fields while Continue is saving", () => { + const { rerender } = render(step(true)); + + // An edit made now would miss the batch being saved and the step would + // advance past it, so nothing in the step takes input until it settles. + const name = screen.getByLabelText("Server name"); + expect(name).toBeDisabled(); + expect(screen.getByRole("switch", { name: "Remote access" })).toBeDisabled(); + // A Radix select opens on pointerdown even inside a disabled fieldset. + expect(name.closest("fieldset")).toHaveClass("pointer-events-none"); + + rerender(step(false)); + expect(name).toBeEnabled(); + expect(screen.getByRole("switch", { name: "Remote access" })).toBeEnabled(); + expect(name.closest("fieldset")).not.toHaveClass("pointer-events-none"); + }); + + it("returns focus to the field Enter was pressed in when the save fails", () => { + const { rerender } = render(step(false)); + const name = screen.getByLabelText("Server name"); + name.focus(); + fireEvent.submit(name.closest("form")!); + + rerender(step(true)); + // Browsers drop focus from a control that becomes disabled. + name.blur(); + rerender(step(false)); + + expect(name).toHaveFocus(); + }); +}); diff --git a/web/src/pages/setup-wizard/StepFrame.tsx b/web/src/pages/setup-wizard/StepFrame.tsx index ba55d4951a..3adf7efd06 100644 --- a/web/src/pages/setup-wizard/StepFrame.tsx +++ b/web/src/pages/setup-wizard/StepFrame.tsx @@ -1,8 +1,10 @@ +import { useEffect, useRef } from "react"; import type { FormEvent, ReactNode } from "react"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { useT } from "@/i18n"; +import { cn } from "@/lib/utils"; interface StepFrameProps { title: string; @@ -44,6 +46,22 @@ export function StepFrame({ footnote, }: StepFrameProps) { const t = useT(); + // Locking the fields drops focus from the one Enter was pressed in. When the + // save fails and the step stays open, put the admin back where they were. + const focusAtSubmit = useRef(null); + useEffect(() => { + if (busy) return; + const target = focusAtSubmit.current; + focusAtSubmit.current = null; + if ( + target instanceof HTMLElement && + target.isConnected && + document.activeElement === document.body + ) { + target.focus(); + } + }, [busy]); + const actions = ( {title} {lede} - {children} + + {/* Lock the fields while the step saves: an edit made after Continue + would miss the saved batch and the step would advance past it. A + disabled fieldset does not stop a Radix select from opening on + pointerdown, so pointer input is cut off as well. */} + + {children} + + {actions} > ); if (onSubmit) { return ( - + { + focusAtSubmit.current = document.activeElement; + onSubmit(event); + }} + className="setup-step" + > {body} );
{lede}