import React, { useCallback } from "react"; import { Box, FormControl, InputLabel, Select, MenuItem, Typography } from "@mui/material"; import type { FieldConfig } from "../../../types"; import { FormFieldRenderer } from "../FormFieldRenderer"; interface Props { field: FieldConfig; value: any; onChange: (value: any) => void; error?: string; } export function DiscriminatorField({ field, value, onChange, error }: Props) { const options = field.oneOfOptions ?? []; const discProp = field.discriminatorProperty ?? "type"; const currentType = value?.[discProp] ?? ""; function defaultFieldValue(field: FieldConfig): any { if (field.enumValues) return field.enumValues[0]; if (field.isArray) return []; if (field.type === "object") return {}; if (field.type === "number" || field.type === "integer") return 0; return null; } const handleTypeChange = useCallback((e: any) => { const newType = e.target.value; const option = options.find((o) => o.value === newType); const newValue: Record = { [discProp]: newType }; if (option) { for (const f of option.fields) { newValue[f.name] = defaultFieldValue(f); } } onChange(newValue); }, [discProp, onChange, options]); const handleFieldChange = useCallback((fieldName: string, fieldValue: any) => { onChange({ ...(value ?? {}), [fieldName]: fieldValue }); }, [onChange, value]); const activeFields = options.find((o) => o.value === currentType)?.fields ?? []; return ( {field.label} {currentType && activeFields.length > 0 && ( {activeFields.map((f) => ( handleFieldChange(f.name, v)} /> ))} )} ); }