diff --git a/client/src/components/DynamicJsonForm.tsx b/client/src/components/DynamicJsonForm.tsx index 709b0d4..d76b151 100644 --- a/client/src/components/DynamicJsonForm.tsx +++ b/client/src/components/DynamicJsonForm.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; @@ -73,25 +73,24 @@ const DynamicJsonForm = ({ JSON.stringify(value ?? generateDefaultValue(schema), null, 2) ); - const validateJsonBeforeSubmit = () => { - if (isJsonMode && rawJsonValue) { - try { - const parsed = JSON.parse(rawJsonValue); - onChange(parsed); - setJsonError(undefined); - return true; - } catch (err) { - setJsonError(err instanceof Error ? err.message : "Invalid JSON"); - return false; - } + // Update rawJsonValue when value prop changes + useEffect(() => { + if (!isJsonMode) { + setRawJsonValue(JSON.stringify(value ?? generateDefaultValue(schema), null, 2)); } - return true; - }; + }, [value, schema, isJsonMode]); const handleSwitchToFormMode = () => { if (isJsonMode) { - if (validateJsonBeforeSubmit()) { + // 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 @@ -160,23 +159,50 @@ const DynamicJsonForm = ({ className="w-4 h-4" /> ); - case "object": - if (!propSchema.properties) return null; - return ( -
Form view not available for this JSON structure. Using simplified view:
+
+ {rawJsonValue}
+
+ Use JSON mode for full editing capabilities.
+