import React, { useCallback, useMemo } from "react"; import { Entity, EntityCollection, EntityReference, FieldProps, ResolvedProperty } from "../../types"; import { ReferencePreview } from "../../preview"; import { FieldHelperText, FormikArrayContainer, LabelWithIcon } from "../components"; import { ErrorView } from "../../components"; import { getIconForProperty, getReferenceFrom } from "../../util"; import { useNavigationController, useReferenceDialog } from "../../hooks"; import { Button, cn, ExpandablePanel, fieldBackgroundMixin } from "@firecms/ui"; import { useClearRestoreValue } from "../useClearRestoreValue"; type ArrayOfReferencesFieldProps = FieldProps; /** * This field allows selecting multiple references. * * This is one of the internal components that get mapped natively inside forms * and tables to the specified properties. * @group Form fields */ export function ArrayOfReferencesFieldBinding({ propertyKey, value, error, showError, disabled, isSubmitting, tableMode, property, includeDescription, setValue, setFieldValue }: ArrayOfReferencesFieldProps) { const ofProperty = property.of as ResolvedProperty; if (ofProperty.dataType !== "reference") { throw Error("ArrayOfReferencesField expected a property containing references"); } const expanded = property.expanded === undefined ? true : property.expanded; const selectedEntityIds = value && Array.isArray(value) ? value.map((ref) => ref.id) : []; useClearRestoreValue({ property, value, setValue }); const navigationController = useNavigationController(); const collection: EntityCollection | undefined = useMemo(() => { return ofProperty.path ? navigationController.getCollection(ofProperty.path) : undefined; }, [ofProperty.path]); if (!collection) { throw Error(`Couldn't find the corresponding collection for the path: ${ofProperty.path}`); } const onMultipleEntitiesSelected = useCallback((entities: Entity[]) => { console.debug("onMultipleEntitiesSelected", entities); setValue(entities.map(e => getReferenceFrom(e))); }, [setValue]); const referenceDialogController = useReferenceDialog({ multiselect: true, path: ofProperty.path, collection, onMultipleEntitiesSelected, selectedEntityIds, forceFilter: ofProperty.forceFilter } ); const onEntryClick = useCallback((e: React.SyntheticEvent) => { e.preventDefault(); referenceDialogController.open(); }, [referenceDialogController]); const buildEntry = useCallback((index: number, internalId: number) => { const entryValue = value && value.length > index ? value[index] : undefined; if (!entryValue) return
Internal ERROR
; return ( ); }, [ofProperty.path, ofProperty.previewProperties, value]); const title = ( ); const body = <> {!collection && } {collection &&
} ; return ( <> {!tableMode && {body} } {tableMode && body} ); }