@@ -46,15 +46,17 @@ const DynamicJsonForm = ({
46
46
} : DynamicJsonFormProps ) => {
47
47
const [ isJsonMode , setIsJsonMode ] = useState ( false ) ;
48
48
const [ jsonError , setJsonError ] = useState < string > ( ) ;
49
- // Add state for storing raw JSON value
49
+ // Store the raw JSON string to allow immediate feedback during typing
50
+ // while deferring parsing until the user stops typing
50
51
const [ rawJsonValue , setRawJsonValue ] = useState < string > (
51
52
JSON . stringify ( value ?? generateDefaultValue ( schema ) , null , 2 ) ,
52
53
) ;
53
54
54
- // Create a ref to store the timeout ID
55
+ // Use a ref to manage debouncing timeouts to avoid parsing JSON
56
+ // on every keystroke which would be inefficient and error-prone
55
57
const timeoutRef = useRef < ReturnType < typeof setTimeout > > ( ) ;
56
58
57
- // Create a debounced function to update parent state
59
+ // Debounce JSON parsing and parent updates to handle typing gracefully
58
60
const debouncedUpdateParent = useCallback (
59
61
( jsonString : string ) => {
60
62
// Clear any existing timeout
@@ -146,6 +148,8 @@ const DynamicJsonForm = ({
146
148
value = { ( currentValue as string ) ?? "" }
147
149
onChange = { ( e ) => {
148
150
const val = e . target . value ;
151
+ // Allow clearing non-required fields by setting undefined
152
+ // This preserves the distinction between empty string and unset
149
153
if ( ! val && ! propSchema . required ) {
150
154
handleFieldChange ( path , undefined ) ;
151
155
} else {
@@ -163,6 +167,8 @@ const DynamicJsonForm = ({
163
167
value = { ( currentValue as number ) ?. toString ( ) ?? "" }
164
168
onChange = { ( e ) => {
165
169
const val = e . target . value ;
170
+ // Allow clearing non-required number fields
171
+ // This preserves the distinction between 0 and unset
166
172
if ( ! val && ! propSchema . required ) {
167
173
handleFieldChange ( path , undefined ) ;
168
174
} else {
@@ -184,10 +190,13 @@ const DynamicJsonForm = ({
184
190
value = { ( currentValue as number ) ?. toString ( ) ?? "" }
185
191
onChange = { ( e ) => {
186
192
const val = e . target . value ;
193
+ // Allow clearing non-required integer fields
194
+ // This preserves the distinction between 0 and unset
187
195
if ( ! val && ! propSchema . required ) {
188
196
handleFieldChange ( path , undefined ) ;
189
197
} else {
190
198
const num = Number ( val ) ;
199
+ // Only update if it's a valid integer
191
200
if ( ! isNaN ( num ) && Number . isInteger ( num ) ) {
192
201
handleFieldChange ( path , num ) ;
193
202
}
0 commit comments