import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import JsonEditor from "./JsonEditor"; export type JsonValue = | string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; export type JsonSchemaType = { type: "string" | "number" | "integer" | "boolean" | "array" | "object"; description?: string; properties?: Record; items?: JsonSchemaType; }; type JsonObject = { [key: string]: JsonValue }; interface DynamicJsonFormProps { schema: JsonSchemaType; value: JsonValue; onChange: (value: JsonValue) => void; maxDepth?: number; } const formatFieldLabel = (key: string): string => { return key .replace(/([A-Z])/g, " $1") // Insert space before capital letters .replace(/_/g, " ") // Replace underscores with spaces .replace(/^\w/, (c) => c.toUpperCase()); // Capitalize first letter }; const DynamicJsonForm = ({ schema, value, onChange, maxDepth = 3, }: DynamicJsonFormProps) => { const generateDefaultValue = (propSchema: JsonSchemaType): JsonValue => { switch (propSchema.type) { case "string": return ""; case "number": case "integer": return 0; case "boolean": return false; case "array": return []; case "object": { const obj: JsonObject = {}; if (propSchema.properties) { Object.entries(propSchema.properties).forEach(([key, prop]) => { obj[key] = generateDefaultValue(prop); }); } return obj; } default: return null; } }; const [isJsonMode, setIsJsonMode] = useState(false); const [jsonError, setJsonError] = useState(); // Add state for storing raw JSON value const [rawJsonValue, setRawJsonValue] = useState( JSON.stringify(value ?? generateDefaultValue(schema), null, 2) ); // Update rawJsonValue when value prop changes useEffect(() => { if (!isJsonMode) { setRawJsonValue(JSON.stringify(value ?? generateDefaultValue(schema), null, 2)); } }, [value, schema, isJsonMode]); const handleSwitchToFormMode = () => { if (isJsonMode) { // When switching to Form mode, ensure we have valid JSON try { const parsed = JSON.parse(rawJsonValue); // Update the parent component's state with the parsed value onChange(parsed); // Switch to form mode setIsJsonMode(false); } catch (err) { setJsonError(err instanceof Error ? err.message : "Invalid JSON"); } } else { // Update raw JSON value when switching to JSON mode setRawJsonValue(JSON.stringify(value ?? generateDefaultValue(schema), null, 2)); setIsJsonMode(true); } }; const renderFormFields = ( propSchema: JsonSchemaType, currentValue: JsonValue, path: string[] = [], depth: number = 0, ) => { if ( depth >= maxDepth && (propSchema.type === "object" || propSchema.type === "array") ) { // Render as JSON editor when max depth is reached return ( { try { const parsed = JSON.parse(newValue); handleFieldChange(path, parsed); setJsonError(undefined); } catch (err) { setJsonError(err instanceof Error ? err.message : "Invalid JSON"); } }} error={jsonError} /> ); } switch (propSchema.type) { case "string": case "number": case "integer": return ( handleFieldChange( path, propSchema.type === "string" ? e.target.value : Number(e.target.value), ) } placeholder={propSchema.description} /> ); case "boolean": return ( handleFieldChange(path, e.target.checked)} className="w-4 h-4" /> ); case "object": { // Handle case where we have a value but no schema properties const objectValue = currentValue as JsonObject || {}; // If we have schema properties, use them to render fields if (propSchema.properties) { return (
{Object.entries(propSchema.properties).map(([key, prop]) => (
{renderFormFields( prop, objectValue[key], [...path, key], depth + 1, )}
))}
); } // If we have a value but no schema properties, render fields based on the value else if (Object.keys(objectValue).length > 0) { return (
{Object.entries(objectValue).map(([key, value]) => (
handleFieldChange([...path, key], e.target.value) } />
))}
); } // If we have neither schema properties nor value, return null return null; } case "array": { const arrayValue = Array.isArray(currentValue) ? currentValue : []; if (!propSchema.items) return null; return (
{propSchema.description && (

{propSchema.description}

)} {propSchema.items?.description && (

Items: {propSchema.items.description}

)}
{arrayValue.map((item, index) => (
{renderFormFields( propSchema.items as JsonSchemaType, item, [...path, index.toString()], depth + 1, )}
))}
); } default: return null; } }; const handleFieldChange = (path: string[], fieldValue: JsonValue) => { if (path.length === 0) { onChange(fieldValue); return; } const updateArray = ( array: JsonValue[], path: string[], value: JsonValue, ): JsonValue[] => { const [index, ...restPath] = path; const arrayIndex = Number(index); // Validate array index if (isNaN(arrayIndex)) { console.error(`Invalid array index: ${index}`); return array; } // Check array bounds if (arrayIndex < 0) { console.error(`Array index out of bounds: ${arrayIndex} < 0`); return array; } const newArray = [...array]; if (restPath.length === 0) { newArray[arrayIndex] = value; } else { // Ensure index position exists if (arrayIndex >= array.length) { console.warn(`Extending array to index ${arrayIndex}`); newArray.length = arrayIndex + 1; newArray.fill(null, array.length, arrayIndex); } newArray[arrayIndex] = updateValue( newArray[arrayIndex], restPath, value, ); } return newArray; }; const updateObject = ( obj: JsonObject, path: string[], value: JsonValue, ): JsonObject => { const [key, ...restPath] = path; // Validate object key if (typeof key !== "string") { console.error(`Invalid object key: ${key}`); return obj; } const newObj = { ...obj }; if (restPath.length === 0) { newObj[key] = value; } else { // Ensure key exists if (!(key in newObj)) { console.warn(`Creating new key in object: ${key}`); newObj[key] = {}; } newObj[key] = updateValue(newObj[key], restPath, value); } return newObj; }; const updateValue = ( current: JsonValue, path: string[], value: JsonValue, ): JsonValue => { if (path.length === 0) return value; try { if (!current) { current = !isNaN(Number(path[0])) ? [] : {}; } // Type checking if (Array.isArray(current)) { return updateArray(current, path, value); } else if (typeof current === "object" && current !== null) { return updateObject(current, path, value); } else { console.error( `Cannot update path ${path.join(".")} in non-object/array value:`, current, ); return current; } } catch (error) { console.error(`Error updating value at path ${path.join(".")}:`, error); return current; } }; try { const newValue = updateValue(value, path, fieldValue); onChange(newValue); } catch (error) { console.error("Failed to update form value:", error); // Keep the original value unchanged onChange(value); } }; return (
{isJsonMode ? ( { setRawJsonValue(newValue); try { if (/^\s*[{[].*[}\]]\s*$/.test(newValue)) { const parsed = JSON.parse(newValue); onChange(parsed); setJsonError(undefined); } } catch { // Don't set an error during typing - that will happen when the user // tries to save or submit the form } }} error={jsonError} /> ) : ( // If schema type is object but value is not an object or is empty, and we have actual JSON data, // render a simple representation of the JSON data schema.type === "object" && (typeof value !== "object" || value === null || Object.keys(value).length === 0) && rawJsonValue && rawJsonValue !== "{}" ? (

Form view not available for this JSON structure. Using simplified view:

              {rawJsonValue}
            

Use JSON mode for full editing capabilities.

) : ( renderFormFields(schema, value) ) )}
); }; export default DynamicJsonForm;