Skip to content

Commit e1b015e

Browse files
committed
Add comments explaining extra parsing logic
1 parent 7c4ed6a commit e1b015e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

client/src/components/DynamicJsonForm.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ const DynamicJsonForm = ({
4646
}: DynamicJsonFormProps) => {
4747
const [isJsonMode, setIsJsonMode] = useState(false);
4848
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
5051
const [rawJsonValue, setRawJsonValue] = useState<string>(
5152
JSON.stringify(value ?? generateDefaultValue(schema), null, 2),
5253
);
5354

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
5557
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
5658

57-
// Create a debounced function to update parent state
59+
// Debounce JSON parsing and parent updates to handle typing gracefully
5860
const debouncedUpdateParent = useCallback(
5961
(jsonString: string) => {
6062
// Clear any existing timeout
@@ -146,6 +148,8 @@ const DynamicJsonForm = ({
146148
value={(currentValue as string) ?? ""}
147149
onChange={(e) => {
148150
const val = e.target.value;
151+
// Allow clearing non-required fields by setting undefined
152+
// This preserves the distinction between empty string and unset
149153
if (!val && !propSchema.required) {
150154
handleFieldChange(path, undefined);
151155
} else {
@@ -163,6 +167,8 @@ const DynamicJsonForm = ({
163167
value={(currentValue as number)?.toString() ?? ""}
164168
onChange={(e) => {
165169
const val = e.target.value;
170+
// Allow clearing non-required number fields
171+
// This preserves the distinction between 0 and unset
166172
if (!val && !propSchema.required) {
167173
handleFieldChange(path, undefined);
168174
} else {
@@ -184,10 +190,13 @@ const DynamicJsonForm = ({
184190
value={(currentValue as number)?.toString() ?? ""}
185191
onChange={(e) => {
186192
const val = e.target.value;
193+
// Allow clearing non-required integer fields
194+
// This preserves the distinction between 0 and unset
187195
if (!val && !propSchema.required) {
188196
handleFieldChange(path, undefined);
189197
} else {
190198
const num = Number(val);
199+
// Only update if it's a valid integer
191200
if (!isNaN(num) && Number.isInteger(num)) {
192201
handleFieldChange(path, num);
193202
}

0 commit comments

Comments
 (0)