"use client"; import { useActionState, useEffect, useState } from "react"; import { useFormStatus } from "react-dom"; import type { ActionResult } from "@/lib/action-result"; import { Icon } from "../Icon"; function SaveButton({ label }: { label: string }) { const { pending } = useFormStatus(); return ( ); } /** Formular mit Speicherleiste und Rückmeldung. Felder als children. */ export function AdminForm({ action, children, submitLabel = "Speichern", extraActions, className, }: { action: (prev: ActionResult, fd: FormData) => Promise; children: React.ReactNode | ((state: ActionResult) => React.ReactNode); submitLabel?: string; extraActions?: React.ReactNode; className?: string; }) { const [state, formAction] = useActionState(action, null); const [visible, setVisible] = useState(false); useEffect(() => { if (!state) return; setVisible(true); if (state.ok) { const t = setTimeout(() => setVisible(false), 4000); return () => clearTimeout(t); } }, [state]); return (
{typeof children === "function" ? children(state) : children}
{visible && state?.message && ( {state.message} )} {extraActions}
); } /** Knopf mit Rückfrage (z. B. Löschen). `inForm`: sitzt in einem anderen Formular und nutzt dessen Felder. */ export function ConfirmButton({ action, confirm, children, className = "sm-btn sm-btn--ghost", hidden, title, inForm, }: { action: (fd: FormData) => Promise; confirm: string; children: React.ReactNode; className?: string; hidden?: Record; title?: string; inForm?: boolean; }) { const [armed, setArmed] = useState(false); const inner = armed ? ( {confirm} ) : ( ); if (inForm) return inner; return (
{hidden && Object.entries(hidden).map(([k, v]) => )} {inner}
); }