import { Button } from "@repo/shadcn-ui/components/button"; import { useId } from "react"; import { useDragReorder } from "../hooks/use-drag-reorder"; import { ArrowDownIcon, ArrowUpIcon, PlusIcon, Trash2Icon } from "../lib/icons"; import type { FieldSection } from "./field-sections"; import { FormLabel } from "./form-label"; import { FormSectionTitle } from "./form-section-title"; import { ReorderDropIndicator } from "./reorder-drop-indicator"; import { ReorderHandle } from "./reorder-handle"; import type { SelectAction } from "./select-with-action"; import { SelectWithAction } from "./select-with-action"; import { SwitchField } from "./switch-field"; import type { SectionBinding, SectionEditorLabels } from "./template-sections-editor"; export function TemplateSectionFields({ sectionId, sections, fields, fieldNames, fieldNotes, labels, disabled, onChange, addAction, layout = "cards", }: { layout?: "cards" | "compact"; sectionId?: string | undefined; sections: readonly FieldSection[]; fields: readonly SectionBinding[]; fieldNames: Readonly>; fieldNotes?: Readonly> | undefined; labels: SectionEditorLabels; disabled: boolean; addAction?: SelectAction | undefined; onChange: (fields: SectionBinding[]) => void; }) { const prefix = useId(); const grouped = fields.filter((field) => field.sectionId === sectionId); const { getHandleProps, getItemProps, getDropPlacement } = useDragReorder({ items: grouped, getId: (field) => field.fieldId, disabled, onChange: (next) => { const reordered = next.values(); onChange( fields.map((field) => field.sectionId === sectionId ? (reordered.next().value ?? field) : field ) ); }, }); const move = (fieldId: string, offset: number) => { const position = grouped.findIndex((field) => field.fieldId === fieldId); const neighbor = grouped[position + offset]; const current = grouped[position]; if (!(neighbor && current)) { return; } const next = [...fields]; const index = fields.indexOf(current); const other = fields.indexOf(neighbor); next[index] = neighbor; next[other] = current; onChange(next); }; if (!(sectionId || grouped.length || (addAction && !sections.length))) { return null; } return (
{sectionId || !sections.length ? labels.fields : labels.ungrouped} {grouped.length}
{addAction && ( )}

{labels.reorderHelp}

{!grouped.length && (

{labels.empty}

)}
{grouped.map((field, index) => { const name = fieldNames[field.fieldId] ?? field.fieldId; const requiredSwitch = ( {labels.required} : {name} } onCheckedChange={(required) => onChange( fields.map((item) => item.fieldId === field.fieldId ? { ...item, required } : item ) ) } title={`${labels.required}: ${name}`} /> ); return (
{layout === "compact" && requiredSwitch}

{name}

{fieldNotes?.[field.fieldId] && (

{fieldNotes[field.fieldId]}

)}
{layout === "cards" && (
{requiredSwitch} {sections.length > 0 && (
{labels.assign} onChange( fields.map((item) => item.fieldId === field.fieldId ? { fieldId: item.fieldId, required: item.required, ...(section ? { sectionId: section } : {}), } : item ) ) } options={[ ...sections.map((section) => ({ value: section.id, label: section.name || labels.name, })), ]} placeholder={labels.ungrouped} value={field.sectionId ?? ""} />
)}
)}
); })}
); }