import React, { useCallback, useEffect, useState } from "react"; import { Field, useFormex } from "@firecms/formex"; import { FieldHelperText, FormikArrayContainer, LabelWithIcon } from "../components"; import { PropertyFieldBinding } from "../PropertyFieldBinding"; import { EnumValuesChip } from "../../preview"; import { FieldProps, FormContext, PropertyFieldBindingProps, PropertyOrBuilder } from "../../types"; import { getDefaultValueFor, getIconForProperty, } from "../../util"; import { DEFAULT_ONE_OF_TYPE, DEFAULT_ONE_OF_VALUE } from "../../util/common"; import { cn, ExpandablePanel, paperMixin, Select, SelectItem, Typography } from "@firecms/ui"; import { useClearRestoreValue } from "../useClearRestoreValue"; /** * If the `oneOf` property is specified, this fields render each array entry as * a `type` select and the corresponding field widget to the selected `type. * * This is one of the internal components that get mapped natively inside forms * and tables to the specified properties. * @group Form fields */ export function BlockFieldBinding>({ propertyKey, value, error, showError, isSubmitting, setValue, setFieldValue, tableMode, property, includeDescription, underlyingValueHasChanged, context, disabled }: FieldProps) { if (!property.oneOf) throw Error("ArrayOneOfField misconfiguration. Property `oneOf` not set"); const expanded = property.expanded === undefined ? true : property.expanded; useClearRestoreValue({ property, value, setValue }); const [lastAddedId, setLastAddedId] = useState(); const buildEntry = useCallback((index: number, internalId: number) => { return ; }, [context, lastAddedId, property.oneOf, propertyKey, value]); const title = ( ); const firstOneOfKey = Object.keys(property.oneOf.properties)[0]; const body = ; return ( <> {!tableMode && {body} } {tableMode && body} ); } interface BlockEntryProps { name: string; index: number; value: any; /** * Name of the field to use as the discriminator for type * Defaults to `type` */ typeField: string; /** * Name of the field to use as the value * Defaults to `value` */ valueField: string; autoFocus: boolean; /** * Record of properties, where the key is the `type` and the value * is the corresponding property */ properties: Record; /** * Additional values related to the state of the form or the entity */ context: FormContext; } function BlockEntry({ name, index, value, typeField, valueField, properties, autoFocus, context }: BlockEntryProps) { const type = value && value[typeField]; const [typeInternal, setTypeInternal] = useState(type ?? undefined); const formex = useFormex(); useEffect(() => { if (!type) { updateType(Object.keys(properties)[0]); } }, []); useEffect(() => { if (type !== typeInternal) { setTypeInternal(type); } }, [type]); const property = typeInternal ? properties[typeInternal] : undefined; const enumValuesConfigs = Object.entries(properties) .map(([key, property]) => ({ id: key, label: property.name ?? key })); const typeFieldName = `${name}.${typeField}`; const valueFieldName = `${name}.${valueField}`; const fieldProps: PropertyFieldBindingProps | undefined = property ? { propertyKey: valueFieldName, property, context, autoFocus, partOfArray: false, partOfBlock: true, tableMode: false } : undefined; const updateType = (newType: any) => { const newSelectedProperty = newType ? properties[newType] : undefined; setTypeInternal(newType); formex.setFieldTouched(typeFieldName, true); formex.setFieldValue(typeFieldName, newType); formex.setFieldValue(valueFieldName, newSelectedProperty ? getDefaultValueFor(newSelectedProperty) : null); }; return (
{(fieldProps) => { const value1 = fieldProps.field.value !== undefined && fieldProps.field.value !== null ? fieldProps.field.value as string : ""; return ( <> ); } } {fieldProps && ( // It is important to use this key to force a re-render of the field on type change )}
); }