import { useId, type FormHTMLAttributes, type ReactNode } from "react";
import type { AppFailure } from "../../contracts/errors.ts";
import { TerminalErrorSurface } from "../components/async-surface.tsx";
import { TextField } from "../components/ui/text-field.ts";
import { useLocale } from "../i18n/index.ts";
import type {
FieldErrors,
FieldName,
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;
}>) {
const { message } = useLocale();
const {
fieldErrors,
formErrors = [],
fieldLabels,
fieldId,
onFocusField,
} = props;
const headingId = useId();
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}
);
}