Update hivegrid-cloud reference to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
davidandClaude Fable 5 committed 2026-07-08 16:15:37 +02:00
1 parent e98d38e716
commit 7b21524c86
3781 files changed
+373484 -225607

No files matched your search

@@ -0,0 +1,328 @@
import type * as React from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useFetcher } from "react-router";
import {
filterVisibleFields,
HONEYPOT_FIELD_KEY,
type FormDefinition,
type FormField,
type FormTheme,
} from "@hivegrid/form-widget";
import Captcha from "~/components/captcha/Captcha";
export type PublicFormRendererProps = {
formKey: string;
definition: FormDefinition;
theme: FormTheme;
captcha: { enabled: boolean; provider?: "mcaptcha" | "cap"; instanceUrl: string | undefined; sitekey: string | undefined };
};
function themeCssVars(theme: FormTheme): React.CSSProperties {
const vars: Record<string, string> = {};
if (theme.primaryColor) vars["--tcform-primary"] = theme.primaryColor;
if (theme.backgroundColor) vars["--tcform-bg"] = theme.backgroundColor;
if (theme.textColor) vars["--tcform-text"] = theme.textColor;
if (theme.fontFamily) vars["--tcform-font"] = theme.fontFamily;
if (theme.borderRadius) vars["--tcform-radius"] = theme.borderRadius;
return vars as React.CSSProperties;
}
function postToParent(message: Record<string, unknown>): void {
if (typeof window === "undefined" || !window.parent || window.parent === window) return;
try {
window.parent.postMessage(message, "*");
} catch {
/* ignore - sandboxed parent without same-origin scripting */
}
}
function FieldRenderer({
field,
value,
onChange,
}: {
field: FormField;
value: string | boolean | string[];
onChange: (next: string | boolean | string[]) => void;
}): React.JSX.Element | null {
const inputId = `tcform-${field.key}`;
if (field.type === "honeypot") {
return (
<input
type="text"
name={field.key}
tabIndex={-1}
aria-hidden="true"
autoComplete="off"
value={typeof value === "string" ? value : ""}
onChange={(e) => onChange(e.target.value)}
style={{ position: "absolute", left: "-9999px", width: 1, height: 1, opacity: 0 }}
/>
);
}
const labelEl = (
<label htmlFor={inputId} className="tcform-label">
{field.label}
{field.required ? <span aria-hidden="true"> *</span> : null}
</label>
);
const helpEl = field.helpText ? (
<div className="tcform-help" id={`${inputId}-help`}>
{field.helpText}
</div>
) : null;
if (field.type === "textarea") {
return (
<div className="tcform-row">
{labelEl}
<textarea
id={inputId}
name={field.key}
required={field.required}
placeholder={field.placeholder}
maxLength={field.maxLength}
minLength={field.minLength}
aria-describedby={helpEl ? `${inputId}-help` : undefined}
value={typeof value === "string" ? value : ""}
onChange={(e) => onChange(e.target.value)}
rows={4}
/>
{helpEl}
</div>
);
}
if (field.type === "select" || field.type === "multiselect") {
const options = field.options ?? [];
return (
<div className="tcform-row">
{labelEl}
<select
id={inputId}
name={field.key}
required={field.required}
multiple={field.type === "multiselect"}
value={Array.isArray(value) ? value : typeof value === "string" ? value : ""}
onChange={(e) =>
onChange(
field.type === "multiselect"
? Array.from(e.currentTarget.selectedOptions).map((opt) => opt.value)
: e.currentTarget.value,
)
}
>
{field.type === "select" ? <option value="">{field.placeholder ?? ""}</option> : null}
{options.map((opt) => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
{helpEl}
</div>
);
}
if (field.type === "boolean") {
return (
<div className="tcform-row tcform-checkbox">
<label className="tcform-label">
<input
type="checkbox"
name={field.key}
checked={value === true}
onChange={(e) => onChange(e.target.checked)}
required={field.required}
/>
<span>{field.label}</span>
</label>
{helpEl}
</div>
);
}
if (field.type === "file" || field.type === "photo") {
return (
<div className="tcform-row">
{labelEl}
<input
id={inputId}
type="file"
name={field.key}
required={field.required}
accept={field.type === "photo" ? "image/*" : field.validationRules?.acceptedFileTypes?.join(",")}
aria-describedby={helpEl ? `${inputId}-help` : undefined}
/>
{helpEl}
</div>
);
}
const inputType =
field.type === "email"
? "email"
: field.type === "number"
? "number"
: field.type === "date"
? "date"
: field.type === "time"
? "time"
: field.type === "phone"
? "tel"
: "text";
return (
<div className="tcform-row">
{labelEl}
<input
id={inputId}
type={inputType}
name={field.key}
required={field.required}
placeholder={field.placeholder}
maxLength={field.maxLength}
minLength={field.minLength}
aria-describedby={helpEl ? `${inputId}-help` : undefined}
value={typeof value === "string" ? value : ""}
onChange={(e) => onChange(e.target.value)}
/>
{helpEl}
</div>
);
}
export function PublicFormRenderer({ formKey, definition, theme, captcha }: PublicFormRendererProps): React.JSX.Element {
const fetcher = useFetcher<{ ok?: boolean; error?: string }>();
const containerRef = useRef<HTMLDivElement | null>(null);
const [values, setValues] = useState<Record<string, string | boolean | string[]>>(() => {
const init: Record<string, string | boolean | string[]> = {};
for (const f of definition.fields) {
init[f.key] = f.type === "boolean" ? false : f.type === "multiselect" ? [] : "";
}
return init;
});
const isSubmitting = fetcher.state !== "idle";
const submittedOk = fetcher.data?.ok === true;
const styleVars = useMemo(() => themeCssVars(theme), [theme]);
const idempotencyKey = useMemo(() => {
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
return crypto.randomUUID();
}
return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
}, []);
useEffect(() => {
if (typeof window === "undefined") return;
function emitResize() {
const h = containerRef.current?.offsetHeight ?? 0;
postToParent({ type: "tcform:resize", height: h, formKey });
}
emitResize();
const observer = typeof ResizeObserver !== "undefined" ? new ResizeObserver(emitResize) : null;
if (observer && containerRef.current) observer.observe(containerRef.current);
return () => observer?.disconnect();
}, [formKey]);
useEffect(() => {
if (fetcher.state === "idle" && fetcher.data) {
if (fetcher.data.ok) {
postToParent({ type: "tcform:submitted", formKey });
} else if (fetcher.data.error) {
postToParent({ type: "tcform:error", formKey, error: fetcher.data.error });
}
}
}, [fetcher.state, fetcher.data, formKey]);
function handleSubmit(event: React.FormEvent<HTMLFormElement>): void {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const captchaToken = formData.get("cap-token") ?? formData.get("mcaptcha__token");
const visibleFields = filterVisibleFields(definition.fields, values);
const visibleValues = Object.fromEntries(
visibleFields.map((field) => {
if (field.type === "file" || field.type === "photo") {
const file = formData.get(field.key);
return [field.key, file instanceof File && file.size > 0 ? file.name : ""];
}
return [field.key, values[field.key]];
}),
);
const submitData = new FormData();
submitData.set(
"__payload",
JSON.stringify({
values: visibleValues,
gdprAccepted: values.__gdprAccepted === true,
captchaToken: typeof captchaToken === "string" ? captchaToken : undefined,
idempotencyKey,
}),
);
for (const field of visibleFields) {
if (field.type !== "file" && field.type !== "photo") continue;
const file = formData.get(field.key);
if (file instanceof File && file.size > 0) {
submitData.append(field.key, file);
}
}
fetcher.submit(submitData, {
method: "post",
action: `/api/embed/forms/${encodeURIComponent(formKey)}/submit`,
encType: "multipart/form-data",
});
}
return (
<div ref={containerRef} className="tcform-root" style={styleVars}>
<style>{`
.tcform-root { font-family: var(--tcform-font, system-ui, sans-serif); background: var(--tcform-bg, #fff); color: var(--tcform-text, #111); padding: 16px; box-sizing: border-box; }
.tcform-root h1 { font-size: 1.25rem; margin: 0 0 0.25em; }
.tcform-root p.tcform-description { margin: 0 0 1em; opacity: 0.8; }
.tcform-row { display: block; margin-bottom: 12px; }
.tcform-root .tcform-label { display: block; font-weight: 500; margin-bottom: 4px; }
.tcform-root input[type=text], .tcform-root input[type=email], .tcform-root input[type=number], .tcform-root textarea, .tcform-root select { width: 100%; padding: 8px 10px; border: 1px solid #ccc; border-radius: var(--tcform-radius, 6px); font: inherit; color: inherit; background: #fff; box-sizing: border-box; }
.tcform-root textarea { min-height: 100px; resize: vertical; }
.tcform-root .tcform-help { font-size: 0.85em; opacity: 0.7; margin-top: 4px; }
.tcform-root button { background: var(--tcform-primary, #2563eb); color: #fff; border: 0; padding: 10px 16px; border-radius: var(--tcform-radius, 6px); cursor: pointer; font: inherit; }
.tcform-root button[disabled] { opacity: 0.6; cursor: progress; }
.tcform-root .tcform-success { padding: 12px; border-radius: var(--tcform-radius, 6px); background: #ecfdf5; color: #065f46; }
.tcform-root .tcform-error { padding: 12px; border-radius: var(--tcform-radius, 6px); background: #fef2f2; color: #991b1b; margin-bottom: 12px; }
`}</style>
<h1>{definition.title}</h1>
{definition.description ? <p className="tcform-description">{definition.description}</p> : null}
{submittedOk ? (
<div className="tcform-success" role="status">
{definition.successMessage}
</div>
) : (
<form onSubmit={handleSubmit} noValidate={false}>
{fetcher.data?.error ? (
<div className="tcform-error" role="alert">
{fetcher.data.error}
</div>
) : null}
{filterVisibleFields(definition.fields, values).map((field) => (
<FieldRenderer
key={field.key}
field={field}
value={values[field.key] ?? (field.type === "boolean" ? false : field.type === "multiselect" ? [] : "")}
onChange={(next) => setValues((prev) => ({ ...prev, [field.key]: next }))}
/>
))}
<div className="tcform-row tcform-checkbox">
<label className="tcform-label">
<input
type="checkbox"
name="__gdprAccepted"
checked={values.__gdprAccepted === true}
onChange={(e) => setValues((prev) => ({ ...prev, __gdprAccepted: e.target.checked }))}
required
/>
<span>{definition.gdprConsentLabel}</span>
</label>
</div>
<Captcha enabled={captcha.enabled} provider={captcha.provider} instanceUrl={captcha.instanceUrl} sitekey={captcha.sitekey} />
<input type="hidden" name="__hp_key" value={HONEYPOT_FIELD_KEY} />
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? "..." : definition.submitLabel}
</button>
</form>
)}
</div>
);
}