Skip to content

Commit 42eb8ad

Browse files
committed
fix(whatif): preserve input focus while solving
1 parent 26862d1 commit 42eb8ad

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

app/e2e/mut/rebalance.e2e.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
import { expect, test } from "@playwright/test";
22
import { goto, toast, undoButton } from "./helpers";
33

4+
test("what-if target stays editable while the factory re-solves", async ({ page }) => {
5+
await goto(page, "/whatif");
6+
7+
const firstDemand = page.getByRole("spinbutton").first();
8+
await expect(firstDemand).toBeVisible();
9+
await firstDemand.selectText();
10+
await firstDemand.pressSequentially("12.34", { delay: 100 });
11+
12+
// Each character starts another solve. The demand list must remain mounted,
13+
// preserving both the in-progress value and keyboard focus throughout them.
14+
await expect(firstDemand).toBeFocused();
15+
await expect(firstDemand).toHaveValue("12.34");
16+
await expect(page.getByText("Block changes", { exact: false }).first()).toBeVisible({
17+
timeout: 15_000,
18+
});
19+
await expect(firstDemand).toBeFocused();
20+
await expect(firstDemand).toHaveValue("12.34");
21+
});
22+
423
/**
524
* What-if "Apply all" (whole-factory re-balance): overriding a final product's
625
* target surfaces the per-block changes, and Apply all commits every one of them

app/src/routes/whatif.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createFileRoute, Link } from "@tanstack/react-router";
2-
import { useQuery } from "@tanstack/react-query";
2+
import { keepPreviousData, useQuery } from "@tanstack/react-query";
33
import { useState } from "react";
44
import { factoryWhatIfFn } from "../server/factorio";
55
import { Icon, IconProvider } from "../lib/icons";
@@ -43,6 +43,9 @@ function WhatIf() {
4343
const wf = useQuery({
4444
queryKey: ["whatif", overrides],
4545
queryFn: () => factoryWhatIfFn({ data: { demands: overrides } }),
46+
// Changing a target starts a fresh whole-factory solve. Keep the demand rows
47+
// mounted while it runs so the active input does not lose focus mid-edit.
48+
placeholderData: keepPreviousData,
4649
});
4750
const r = wf.data;
4851
const changed = (r?.blocks ?? []).filter((b) => Math.abs(b.scale - 1) > SCALE_EPS);

0 commit comments

Comments
 (0)