import { Button } from "@repo/shadcn-ui/components/button"; import { useId } from "react"; import { useDragReorder } from "../hooks/use-drag-reorder"; import { PlusIcon } from "../lib/icons"; import { removeTemplateSection } from "../lib/template-sections"; import type { FieldSection } from "./field-sections"; import { FormSectionTitle } from "./form-section-title"; import { ReorderDropIndicator } from "./reorder-drop-indicator"; import { ReorderHandle } from "./reorder-handle"; import { TemplateSectionEditor } from "./template-section-editor"; import { TemplateSectionFields } from "./template-section-fields"; export type SectionBinding = { fieldId: string; required: boolean; sectionId?: string | undefined }; export type SectionEditorLabels = { title: string; expand: string; collapse: string; fields: string; reorder: string; reorderHelp: string; add: string; name: string; description: string; ungrouped: string; moveUp: string; moveDown: string; remove: string; removeField: string; required: string; assign: string; help: string; empty: string; }; export function TemplateSectionsEditor({ sections, fields, fieldNames, fieldNotes, labels, disabled = false, createId, onChange, addFieldAction, fieldLayout = "cards", sectionLayout = "stack", }: { fieldLayout?: "cards" | "compact"; sectionLayout?: "stack" | "responsive"; sections: readonly FieldSection[]; fields: readonly SectionBinding[]; fieldNames: Readonly>; fieldNotes?: Readonly> | undefined; labels: SectionEditorLabels; disabled?: boolean; createId: () => string; addFieldAction?: { label: string; onClick: (sectionId: string | undefined) => void }; onChange: (value: { sections: FieldSection[]; fields: SectionBinding[] }) => void; }) { const prefix = useId(); const saveSections = (next: FieldSection[]) => onChange({ sections: next, fields: [...fields] }); const { getHandleProps, getItemProps, getDropPlacement } = useDragReorder({ items: sections, getId: (section) => section.id, disabled, onChange: saveSections, }); const moveSection = (index: number, offset: number) => { const next = [...sections]; const first = next[index]; const second = next[index + offset]; if (first && second) { next[index] = second; next[index + offset] = first; saveSections(next); } }; const removeSection = (id: string) => onChange(removeTemplateSection(sections, fields, id)); const fieldList = (sectionId?: string | undefined) => ( addFieldAction.onClick(sectionId), } : undefined } disabled={disabled} fieldNames={fieldNames} fieldNotes={fieldNotes} fields={fields} labels={labels} layout={fieldLayout} onChange={(next) => onChange({ sections: [...sections], fields: next })} sectionId={sectionId} sections={sections} /> ); return (
{labels.title}

{labels.reorderHelp}

{sections.map((section, index) => (
{section.name || labels.name} } index={index} labels={labels} onChange={(next) => saveSections(sections.map((item) => (item.id === section.id ? next : item))) } onMove={(offset) => moveSection(index, offset)} onRemove={() => removeSection(section.id)} section={section} sectionCount={sections.length} > {fieldList(section.id)}
))}
{fieldList()}
); }