fix(presentation): localize default fallbacks and keep SearchField focus (Q-6.8-13c)
The render boundary's default fallback reads the current locale instead of formatting ko-KR, and resolveMessage's "message unavailable" fallback comes from the requested locale's catalog (ko-KR only as a last resort). SearchField's clear button moves focus to the input before it unmounts, so focus no longer drops to the body. tests/unit/i18n-contract.test.ts expected the Korean fallback for en-US, which encoded the bug; it now expects the English copy and keeps a ko-KR case. New component cases for the boundary fallback and SearchField (RED: 3 failed before the change); lane gates pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cc1c7d879c
commit
72b1a72c81
@@ -1,7 +1,7 @@
|
||||
import { Component, type ReactNode } from "react";
|
||||
|
||||
import type { RenderFailureReport } from "../../application/ports/in/application-api.ts";
|
||||
import { formatMessage } from "../i18n/index.ts";
|
||||
import { useLocale } from "../i18n/index.ts";
|
||||
|
||||
export type RenderBoundaryProps = Readonly<{
|
||||
children: ReactNode;
|
||||
@@ -50,20 +50,26 @@ export class RenderErrorBoundary extends Component<
|
||||
render(): ReactNode {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
this.props.fallback ?? (
|
||||
<section role="alert">
|
||||
<p>{formatMessage("ko-KR", "error.render_failure")}</p>
|
||||
<button type="button" onClick={this.reset}>
|
||||
{formatMessage("ko-KR", "action.retry")}
|
||||
</button>
|
||||
</section>
|
||||
)
|
||||
this.props.fallback ?? <DefaultRenderFailure onRetry={this.reset} />
|
||||
);
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
/** The default fallback, in the current locale. */
|
||||
function DefaultRenderFailure({ onRetry }: Readonly<{ onRetry(): void }>) {
|
||||
const { message } = useLocale();
|
||||
return (
|
||||
<section role="alert">
|
||||
<p>{message("error.render_failure")}</p>
|
||||
<button type="button" onClick={onRetry}>
|
||||
{message("action.retry")}
|
||||
</button>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export function RouteBoundary(
|
||||
props: Omit<RenderBoundaryProps, "boundaryName">,
|
||||
) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
forwardRef,
|
||||
useEffect,
|
||||
useId,
|
||||
useImperativeHandle,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
@@ -373,6 +374,8 @@ export const SearchField = forwardRef<HTMLInputElement, SearchFieldProps>(
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
useImperativeHandle(ref, () => inputRef.current as HTMLInputElement);
|
||||
return (
|
||||
<div className={`ui-search-field ${className}`.trim()}>
|
||||
<SearchIcon />
|
||||
@@ -390,7 +393,7 @@ export const SearchField = forwardRef<HTMLInputElement, SearchFieldProps>(
|
||||
aria-invalid={invalid || undefined}
|
||||
className="ui-field__input"
|
||||
id={controlId}
|
||||
ref={ref}
|
||||
ref={inputRef}
|
||||
type="search"
|
||||
value={value}
|
||||
/>
|
||||
@@ -399,7 +402,12 @@ export const SearchField = forwardRef<HTMLInputElement, SearchFieldProps>(
|
||||
{String(value ?? "").length > 0 ? (
|
||||
<IconButton
|
||||
accessibleName={clearLabel}
|
||||
onClick={onClear}
|
||||
onClick={() => {
|
||||
onClear();
|
||||
// The button unmounts once the value is empty; keep focus in
|
||||
// the field instead of dropping it to the body.
|
||||
inputRef.current?.focus();
|
||||
}}
|
||||
variant="ghost"
|
||||
>
|
||||
<CloseIcon />
|
||||
|
||||
@@ -72,7 +72,8 @@ export function resolveMessage(
|
||||
const catalog =
|
||||
MESSAGE_CATALOGS[sourceLocale] ??
|
||||
MESSAGE_CATALOGS[FALLBACK_LOCALE];
|
||||
const fallback = KO_MESSAGES["common.unavailable"];
|
||||
const fallback =
|
||||
catalog["common.unavailable"] ?? KO_MESSAGES["common.unavailable"];
|
||||
const canonicalKey =
|
||||
MESSAGE_KEY_ALIASES[key as keyof typeof MESSAGE_KEY_ALIASES] ?? key;
|
||||
const template = catalog[canonicalKey as MessageKey] ?? fallback;
|
||||
|
||||
@@ -23,12 +23,41 @@ import {
|
||||
Pagination,
|
||||
Popover,
|
||||
RadioGroup,
|
||||
SearchField,
|
||||
Switch,
|
||||
Tabs,
|
||||
ToastProvider,
|
||||
useToast,
|
||||
} from "../../src/presentation/design-system/index.ts";
|
||||
|
||||
describe("SearchField", () => {
|
||||
it("returns focus to the input when the clear button removes itself", async () => {
|
||||
const user = userEvent.setup();
|
||||
function ControlledSearch() {
|
||||
const [value, setValue] = useState("query");
|
||||
return (
|
||||
<SearchField
|
||||
clearLabel="Clear search"
|
||||
label="Search"
|
||||
onChange={(event) => setValue(event.currentTarget.value)}
|
||||
onClear={() => setValue("")}
|
||||
value={value}
|
||||
/>
|
||||
);
|
||||
}
|
||||
render(<ControlledSearch />);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Clear search" }));
|
||||
|
||||
const input = screen.getByRole("searchbox", { name: "Search" });
|
||||
expect(input).toHaveValue("");
|
||||
expect(input).toHaveFocus();
|
||||
expect(
|
||||
screen.queryByRole("button", { name: "Clear search" }),
|
||||
).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe("design-system platform interactions", () => {
|
||||
it("keeps decorative icons out of the accessibility tree and names icon actions", () => {
|
||||
render(
|
||||
|
||||
@@ -10,6 +10,7 @@ import { deriveAsyncState } from "../../src/application/view-models/async-state.
|
||||
import { AsyncSurface } from "../../src/presentation/components/async-surface.tsx";
|
||||
import { BootErrorShell } from "../../src/presentation/boundaries/boot-error-shell.tsx";
|
||||
import { FeatureBoundary } from "../../src/presentation/boundaries/render-error-boundary.tsx";
|
||||
import { LocaleProvider } from "../../src/presentation/i18n/index.ts";
|
||||
|
||||
function Defect(): ReactNode {
|
||||
throw new Error("raw render stack");
|
||||
@@ -123,4 +124,18 @@ describe("render recovery boundaries", () => {
|
||||
);
|
||||
expect(await screen.findByText("next route")).toBeVisible();
|
||||
});
|
||||
|
||||
it("renders the default fallback in the current locale", () => {
|
||||
render(
|
||||
<LocaleProvider initialLocale="en-US">
|
||||
<FeatureBoundary routeId="APP_HOME" buildId="build-a">
|
||||
<Defect />
|
||||
</FeatureBoundary>
|
||||
</LocaleProvider>,
|
||||
);
|
||||
expect(screen.getByRole("alert")).toHaveTextContent(
|
||||
"The page could not be displayed.",
|
||||
);
|
||||
expect(screen.getByRole("button", { name: "Try again" })).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,10 +52,14 @@ describe("internationalization message contract", () => {
|
||||
expect(normalizeLocale("fr-FR")).toBe("ko-KR");
|
||||
expect(resolveMessage("fr-FR", "action.retry")).toBe("다시 시도");
|
||||
expect(resolveMessage("en-US", "action.login")).toBe("Sign in");
|
||||
// The fallback copy follows the requested locale.
|
||||
expect(resolveMessage("en-US", "backend.stack.trace")).toBe(
|
||||
"요청한 문구를 표시할 수 없습니다.",
|
||||
"This message is unavailable.",
|
||||
);
|
||||
expect(resolveMessage("en-US", "action.closeNamed")).toBe(
|
||||
"This message is unavailable.",
|
||||
);
|
||||
expect(resolveMessage("ko-KR", "backend.stack.trace")).toBe(
|
||||
"요청한 문구를 표시할 수 없습니다.",
|
||||
);
|
||||
expect(resolveMessage("en-US", "backend.stack.trace")).not.toContain(
|
||||
|
||||
Reference in New Issue
Block a user