import { useId, type FormHTMLAttributes, type ReactNode } from "react"; import type { AsyncState } from "../../application/view-models/async-state.ts"; import type { AppFailure } from "../../contracts/errors.ts"; import { AsyncSurface, TerminalErrorSurface } from "../components/async-surface.tsx"; import type { HeadingLevel } from "../components/state-surfaces.tsx"; import { TextField } from "../components/ui/text-field.ts"; import { useLocale, type ParameterlessMessageKey } from "../i18n/index.ts"; import type { FieldErrors, FieldName, FormResult, FormResultState, FormValues, } from "./form-contracts.ts"; export function Form( props: FormHTMLAttributes & Readonly<{ pending?: boolean }>, ) { const { pending = false, children, ...formProps } = props; return (
{children}
); } export function FormField( props: React.ComponentProps, ) { return ; } export function ErrorSummary(props: Readonly<{ fieldErrors: FieldErrors; formErrors?: readonly string[]; fieldLabels: Readonly, string>>; fieldId(name: FieldName): string; onFocusField?(name: FieldName): void; /** 08-05. Below the heading of the card or section that holds it. */ headingLevel?: HeadingLevel | undefined; }>) { const { message } = useLocale(); const { fieldErrors, formErrors = [], fieldLabels, fieldId, onFocusField, headingLevel = 2, } = props; const headingId = useId(); const Heading = `h${headingLevel}` as const; const entries = Object.entries(fieldErrors) as [ FieldName, string, ][]; if (entries.length === 0 && formErrors.length === 0) return null; return (
{message("form.errorSummary")} {entries.length > 0 ? ( ) : null} {formErrors.map((message) => (

{message}

))}
); } /** * 08-01. A save that failed for a reason other than the input (sign-in, * permission, retry later, unavailable) is announced here with its own action, not in * ErrorSummary. Pass `useAppForm().failure`. */ export function FormFailureStatus(props: Readonly<{ failure: AppFailure | null; onAction?: () => void; }>) { const { message } = useLocale(); const { failure, onAction } = props; if (!failure) return null; return ( ); } export function FormActions(props: Readonly<{ children: ReactNode; sticky?: boolean; }>) { return (
{props.children}
); } /** What FormSaveFeedback reads of a `useAppForm` result given the `mutation` option. */ export type FormSaveFeedbackForm = Readonly<{ effectUnknown: boolean; otherSaveRunning: boolean; mutationState: AsyncState | undefined; failure: AppFailure | null; result: FormResultState; notice: ParameterlessMessageKey | null; supportReference: string | undefined; submitForm(): Promise; confirmUnknownEffect(): Promise; resendUnknownEffect( then?: (outcome: FormResult | null) => FormResult | null, ): Promise | null>; }>; /** * 1112/01-01. The feedback of a form that saves through a mutation * (`useAppForm` with `mutation`), in the order a page used to write out by * hand: the unknown result (confirm it was applied, or resend it with the * same key), then a save failure that is not an input error, then a * conflict, then a save of the mutation this form did not start. Put it in * the page template's feedback area (`FormPage` `feedback`, `feedbackId`). */ export function FormSaveFeedback(props: Readonly<{ form: FormSaveFeedbackForm; /** After "it was applied" has settled the request (a page opens the list). */ onConfirmed?: () => void; /** Sees a resend's outcome first (a page opens what was saved). */ onResent?: (outcome: FormResult | null) => FormResult | null; /** The action of a failure that is not "try again" (sign in, for example). */ onFailureAction?: () => void; }>) { const { message } = useLocale(); const { form, onConfirmed, onResent, onFailureAction } = props; const notice = form.notice ? message(form.notice) : undefined; if (form.effectUnknown && form.mutationState) { return ( { void form.confirmUnknownEffect().then(() => onConfirmed?.()); }} onResendUnknownEffect={() => { void form.resendUnknownEffect(onResent); }} state={form.mutationState} supportReference={form.supportReference} /> ); } if (form.failure) { return ( void form.submitForm() : onFailureAction} /> ); } if (form.result === "conflict" && notice) { return

{notice}

; } if (form.otherSaveRunning && form.mutationState && form.result !== "effect-unknown") { return ; } return null; }