import { Button } from "@repo/shadcn-ui/components/button"; import { Input } from "@repo/shadcn-ui/components/input"; import { Label } from "@repo/shadcn-ui/components/label"; import { Textarea } from "@repo/shadcn-ui/components/textarea"; import { ArrowDownIcon, ArrowUpIcon, PlusIcon, Trash2Icon } from "lucide-react"; import { useId } from "react"; import { removeTemplateSection } from "../lib/template-sections"; import type { FieldSection } from "./field-sections"; import { TemplateSectionFields } from "./template-section-fields"; export type SectionBinding = { fieldId: string; required: boolean; sectionId?: string | undefined }; export type SectionEditorLabels = { title: 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, }: { sections: readonly FieldSection[]; fields: readonly SectionBinding[]; fieldNames: Readonly>; fieldNotes?: Readonly> | undefined; labels: SectionEditorLabels; disabled?: boolean; createId: () => string; onChange: (value: { sections: FieldSection[]; fields: SectionBinding[] }) => void; }) { const prefix = useId(); const saveSections = (next: FieldSection[]) => onChange({ sections: next, fields: [...fields] }); 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) => ( onChange({ sections: [...sections], fields: next })} sectionId={sectionId} sections={sections} /> ); return (

{labels.title}

{labels.help}

{sections.map((section, index) => (
{section.name || labels.name}
saveSections( sections.map((item) => item.id === section.id ? { ...item, name: event.target.value } : item ) ) } required value={section.name} />