import React from "react"; import { FieldProps, Properties, ResolvedProperties } from "../../types"; import { ErrorBoundary } from "../../components"; import { getIconForProperty, isHidden, pick } from "../../util"; import { FieldHelperText, LabelWithIcon } from "../components"; import { PropertyFieldBinding } from "../PropertyFieldBinding"; import { ExpandablePanel, InputLabel, Select, SelectItem } from "@firecms/ui"; /** * Field that renders the children property fields * * This is one of the internal components that get mapped natively inside forms * and tables to the specified properties. * @group Form fields */ export function MapFieldBinding({ propertyKey, value, showError, error, disabled, property, setValue, partOfBlock, tableMode, includeDescription, underlyingValueHasChanged, autoFocus, context }: FieldProps>) { const pickOnlySomeKeys = property.pickOnlySomeKeys || false; const expanded = (property.expanded === undefined ? true : property.expanded) || autoFocus; if (!property.properties) { throw Error(`You need to specify a 'properties' prop (or specify a custom field) in your map property ${propertyKey}`); } let mapProperties: ResolvedProperties; if (pickOnlySomeKeys) { if (value) { mapProperties = pick(property.properties, ...Object.keys(value) .filter(key => key in property.properties!) ); } else { mapProperties = {}; } } else { mapProperties = property.properties; } const mapFormView = <>
{Object.entries(mapProperties) .filter(([_, property]) => !isHidden(property)) .map(([entryKey, childProperty], index) => { const fieldProps = { propertyKey: `${propertyKey}.${entryKey}`, disabled, property: childProperty, includeDescription, underlyingValueHasChanged, context, tableMode: false, partOfArray: false, partOfBlock: false, autoFocus: autoFocus && index === 0 }; return (
); } )}
{/*{pickOnlySomeKeys && buildPickKeysSelect(disabled, property.properties, setValue, value)}*/} ; const title = ( ); return ( {!tableMode && !partOfBlock && {mapFormView}} {(tableMode || partOfBlock) && mapFormView} ); } const buildPickKeysSelect = (disabled: boolean, properties: Properties, setValue: (value: any) => void, value: any) => { const keys = Object.keys(properties) .filter((key) => !value || !(key in value)); const handleAddProperty = (updatedValue: string | string[]) => { setValue({ ...value, [updatedValue as string]: null }); }; if (!keys.length) return <>; return
Add property
; };