diff --git a/src/presentation/boundaries/render-error-boundary.tsx b/src/presentation/boundaries/render-error-boundary.tsx index 782638ac..36e69ad8 100644 --- a/src/presentation/boundaries/render-error-boundary.tsx +++ b/src/presentation/boundaries/render-error-boundary.tsx @@ -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 ?? ( -
-

{formatMessage("ko-KR", "error.render_failure")}

- -
- ) + this.props.fallback ?? ); } return this.props.children; } } +/** The default fallback, in the current locale. */ +function DefaultRenderFailure({ onRetry }: Readonly<{ onRetry(): void }>) { + const { message } = useLocale(); + return ( +
+

{message("error.render_failure")}

+ +
+ ); +} + export function RouteBoundary( props: Omit, ) { diff --git a/src/presentation/design-system/primitives/forms.tsx b/src/presentation/design-system/primitives/forms.tsx index 56fe63e7..3f167359 100644 --- a/src/presentation/design-system/primitives/forms.tsx +++ b/src/presentation/design-system/primitives/forms.tsx @@ -2,6 +2,7 @@ import { forwardRef, useEffect, useId, + useImperativeHandle, useRef, useState, } from "react"; @@ -373,6 +374,8 @@ export const SearchField = forwardRef( }, ref, ) { + const inputRef = useRef(null); + useImperativeHandle(ref, () => inputRef.current as HTMLInputElement); return (
@@ -390,7 +393,7 @@ export const SearchField = forwardRef( 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( {String(value ?? "").length > 0 ? ( { + 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" > diff --git a/src/presentation/i18n/message-contract.ts b/src/presentation/i18n/message-contract.ts index a5dc2940..ac193b51 100644 --- a/src/presentation/i18n/message-contract.ts +++ b/src/presentation/i18n/message-contract.ts @@ -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; diff --git a/tests/component/design-system-platform.test.tsx b/tests/component/design-system-platform.test.tsx index 7cac5922..7a228c8e 100644 --- a/tests/component/design-system-platform.test.tsx +++ b/tests/component/design-system-platform.test.tsx @@ -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 ( + setValue(event.currentTarget.value)} + onClear={() => setValue("")} + value={value} + /> + ); + } + render(); + + 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( diff --git a/tests/component/render-boundary.test.tsx b/tests/component/render-boundary.test.tsx index 829c8ff2..63208a00 100644 --- a/tests/component/render-boundary.test.tsx +++ b/tests/component/render-boundary.test.tsx @@ -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( + + + + + , + ); + expect(screen.getByRole("alert")).toHaveTextContent( + "The page could not be displayed.", + ); + expect(screen.getByRole("button", { name: "Try again" })).toBeVisible(); + }); }); diff --git a/tests/unit/i18n-contract.test.ts b/tests/unit/i18n-contract.test.ts index 398a4b3f..42513ebd 100644 --- a/tests/unit/i18n-contract.test.ts +++ b/tests/unit/i18n-contract.test.ts @@ -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(