import React, { useState } from "react"; import { CMSType, FieldProps, ResolvedProperty } from "../../types"; import { FieldHelperText, FormikArrayContainer, LabelWithIcon } from "../components"; import { ErrorBoundary } from "../../components"; import { getIconForProperty } from "../../util"; import { PropertyFieldBinding } from "../PropertyFieldBinding"; import { ExpandablePanel } from "@firecms/ui"; import { useClearRestoreValue } from "../useClearRestoreValue"; /** * Generic array field that allows reordering and renders the child property * as nodes. * * This is one of the internal components that get mapped natively inside forms * and tables to the specified properties. * @group Form fields */ export function RepeatFieldBinding>({ propertyKey, value, error, showError, isSubmitting, setValue, setFieldValue, tableMode, property, includeDescription, underlyingValueHasChanged, context, disabled }: FieldProps) { if (!property.of) throw Error("RepeatFieldBinding misconfiguration. Property `of` not set"); if (!property.resolvedProperties || !Array.isArray(property.resolvedProperties)) throw Error("RepeatFieldBinding - Internal error: Expected array in 'property.resolvedProperties'"); const expanded = property.expanded === undefined ? true : property.expanded; const ofProperty: ResolvedProperty = property.of as ResolvedProperty; const [lastAddedId, setLastAddedId] = useState(); useClearRestoreValue({ property, value, setValue }); const buildEntry = (index: number, internalId: number) => { const childProperty = property.resolvedProperties[index] ?? ofProperty; const fieldProps = { propertyKey: `${propertyKey}.${index}`, disabled, property: childProperty, includeDescription, underlyingValueHasChanged, context, tableMode: false, partOfArray: true, partOfBlock: false, autoFocus: internalId === lastAddedId }; return ; }; const arrayContainer = ; const title = (); return ( <> {!tableMode && {arrayContainer} } {tableMode && arrayContainer} ); }