import Contentarize from "./contentarize"; import { BsPlus } from "react-icons/bs"; import { Field, PickIdValuesFromType } from ".."; import { Fragment, ReactNode, useContext } from "react"; import { CMSContext } from "../cms-provider"; type PropsType = { id: string; inputs: Fields; disabled?: boolean; offset?: { top?: number; left?: number }; children: (value: PickIdValuesFromType, index: number) => ReactNode; }; export default function Repeater({ id, inputs, disabled, offset, children, }: PropsType) { const { editMode, pageData, setEditing } = useContext(CMSContext); if (pageData[id] && !Array.isArray(pageData[id])) { throw new Error("Repeater must be used with an array of items"); } const items = (pageData[id] as Array>) || []; return editMode ? ( <> {items.map((item, i) => ( {() => children(item, i)} ))}
{ e.stopPropagation(); e.preventDefault(); setEditing({ id: id + ".-1", inputs }); }} onKeyDown={() => {}} role="button" className="ctz:relative ctz:max-w-full ctz:overflow-hidden ctz:min-w-10" > {items.length ? (
{children(items[items.length - 1], items.length - 1)}
) : ( )} {/*
Add {_startCase(id)}
*/}
) : ( items.map((item, i) => {children(item, i)}) ); }