Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions web/src/pages/setup-wizard/StepFrame.test.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<StepFrame title="Server" lede="" busy={busy} onSubmit={(e) => e.preventDefault()}>
<input aria-label="Server name" />
<button type="button" role="switch" aria-checked="false" aria-label="Remote access" />
</StepFrame>
);
}

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();
});
});
36 changes: 34 additions & 2 deletions web/src/pages/setup-wizard/StepFrame.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Element | null>(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 = (
<div className="setup-actions">
<Button
Expand Down Expand Up @@ -71,14 +89,28 @@ export function StepFrame({
<h1 className="setup-step-title">{title}</h1>
<p className="setup-step-lede">{lede}</p>
</header>
<div className="setup-step-body">{children}</div>
<div className="setup-step-body">
{/* 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. */}
<fieldset disabled={busy} className={cn("contents", busy && "pointer-events-none")}>
Comment thread
Quick104 marked this conversation as resolved.
Resolved
{children}
</fieldset>
</div>
{actions}
</>
);

if (onSubmit) {
return (
<form onSubmit={onSubmit} className="setup-step">
<form
onSubmit={(event) => {
focusAtSubmit.current = document.activeElement;
onSubmit(event);
}}
className="setup-step"
>
{body}
</form>
);
Expand Down
Loading