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 (
);
}
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