"use client"; import { Button } from "@azmr/ui/button"; import { Field } from "@azmr/ui/field"; import { ErrorSummary } from "@azmr/ui/form"; import { Input } from "@azmr/ui/input"; import { type FormEvent, useState } from "react"; type Errors = Partial>; function validate(form: HTMLFormElement): Errors { const data = new FormData(form); const errors: Errors = {}; if (String(data.get("name")).trim() === "") errors.name = "Enter your full name"; if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(data.get("email")))) { errors.email = "Enter an email address like aroha@example.co.nz"; } return errors; } export default function FormErrorSummary() { const [errors, setErrors] = useState({}); const [submitCount, setSubmitCount] = useState(0); const [sent, setSent] = useState(false); function onSubmit(event: FormEvent) { event.preventDefault(); const found = validate(event.currentTarget); setErrors(found); setSubmitCount((count) => count + 1); setSent(Object.keys(found).length === 0); } return (

{sent ? "Saved." : ""}

); }