"use client"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { Textarea } from "~/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "~/components/ui/select"; import type { FormField } from "~/lib/types/forms"; import { FORM_FIELD_TYPES } from "~/lib/types/forms"; interface FormFieldRendererProps { field: FormField; value: unknown; onChange: (value: unknown) => void; mode: "preview" | "data-entry" | "participant"; index: number; error?: string; disabled?: boolean; } export function FormFieldRenderer({ field, value, onChange, mode, index, error, disabled = false, }: FormFieldRendererProps) { const handleChange = (val: unknown) => { if (!disabled) { onChange(val); } }; const commonProps = { disabled, className: error ? "border-destructive" : "", }; const scale = field.settings?.scale ?? 5; switch (field.type) { case "text": return ( handleChange(e.target.value)} placeholder="Enter your response..." /> ); case "textarea": return ( handleChange(e.target.value)} placeholder="Enter your response..." /> ); case "multiple_choice": { const containerClass = mode === "participant" ? `mt-2 space-y-2 ${error ? "border-destructive rounded-md border p-2" : ""}` : "space-y-2"; return ( {field.options?.map((opt, i) => ( handleChange(opt)} disabled={disabled} className="h-4 w-4" /> {opt} ))} ); } case "checkbox": return ( handleChange(e.target.checked)} disabled={disabled} className="h-4 w-4 rounded border-gray-300" /> {mode === "participant" && ( Yes, I agree )} ); case "yes_no": if (mode === "data-entry") { return ( handleChange(val)} disabled={disabled} > Yes No ); } return ( handleChange("yes")} disabled={disabled} className="h-4 w-4" /> Yes handleChange("no")} disabled={disabled} className="h-4 w-4" /> No ); case "rating": { const scale = field.settings?.scale || 5; if (mode === "data-entry") { return ( handleChange(parseInt(val))} disabled={disabled} > {Array.from({ length: scale }, (_, i) => ( {i + 1} ))} ); } if (mode === "participant") { return ( {Array.from({ length: scale }, (_, i) => ( handleChange(i + 1)} disabled={disabled} className="peer sr-only" /> {i + 1} ))} ); } return ( {Array.from({ length: scale }, (_, i) => ( {i + 1} ))} ); } case "date": return ( handleChange(e.target.value)} /> ); case "signature": return ( handleChange(e.target.value)} placeholder={ mode === "participant" ? "Type your full name as signature" : "Type name as signature..." } /> By entering your name above, you confirm that the information provided is accurate. ); default: return null; } } interface FormFieldLabelProps { field: FormField; index: number; showIndex?: boolean; } export function FormFieldLabel({ field, index, showIndex = true, }: FormFieldLabelProps) { const fieldType = FORM_FIELD_TYPES.find((f) => f.value === field.type); return ( {showIndex && `${index + 1}. `} {field.label} {field.required && *} ); }
By entering your name above, you confirm that the information provided is accurate.