Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
"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 (
|
|
<button className="sm-btn sm-btn--primary" type="submit" disabled={pending}>
|
|
{pending ? "Speichert …" : label}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
/** Formular mit Speicherleiste und Rückmeldung. Felder als children. */
|
|
export function AdminForm({
|
|
action,
|
|
children,
|
|
submitLabel = "Speichern",
|
|
extraActions,
|
|
className,
|
|
}: {
|
|
action: (prev: ActionResult, fd: FormData) => Promise<ActionResult>;
|
|
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 (
|
|
<form action={formAction} className={`ad-form ${className ?? ""}`} noValidate>
|
|
{typeof children === "function" ? children(state) : children}
|
|
<div className="ad-savebar">
|
|
<span className="ad-savebar__msg" role="status" aria-live="polite">
|
|
{visible && state?.message && (
|
|
<span className={state.ok ? "ad-ok" : "ad-err"}>
|
|
<Icon name={state.ok ? "check" : "alert"} size="sm" /> {state.message}
|
|
</span>
|
|
)}
|
|
</span>
|
|
{extraActions}
|
|
<SaveButton label={submitLabel} />
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
/** 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<void>;
|
|
confirm: string;
|
|
children: React.ReactNode;
|
|
className?: string;
|
|
hidden?: Record<string, string>;
|
|
title?: string;
|
|
inForm?: boolean;
|
|
}) {
|
|
const [armed, setArmed] = useState(false);
|
|
const inner = armed ? (
|
|
<span style={{ display: "inline-flex", gap: 8, alignItems: "center" }}>
|
|
<span className="body-sm">{confirm}</span>
|
|
<button className="sm-btn sm-btn--sm" type="submit" formAction={action} formNoValidate style={{ background: "var(--danger)", color: "var(--surface-raised)", borderColor: "var(--danger)" }}>Ja</button>
|
|
<button className="sm-btn sm-btn--sm sm-btn--secondary" type="button" onClick={() => setArmed(false)}>Nein</button>
|
|
</span>
|
|
) : (
|
|
<button className={className} type="button" onClick={() => setArmed(true)} title={title} aria-label={title}>{children}</button>
|
|
);
|
|
if (inForm) return inner;
|
|
return (
|
|
<form action={action} style={{ display: "inline" }}>
|
|
{hidden && Object.entries(hidden).map(([k, v]) => <input key={k} type="hidden" name={k} value={v} />)}
|
|
{inner}
|
|
</form>
|
|
);
|
|
}
|