Skip to content

Commit 0acf180

Browse files
committed
Bug fix
1 parent 5e790f9 commit 0acf180

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ht-rnd/json-schema-editor",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"license": "Apache-2.0",
55
"description": "Headless JSON Schema Editor - provides hooks and utilities for building JSON Schema editors",
66
"keywords": [

src/lib/core/transforms.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ export const schemaToForm = (schema: JSONSchema): FormSchema => {
278278
});
279279
});
280280
}
281+
if (propertiesArray.length === 0) {
282+
propertiesArray.push({ id: nanoid(6), key: "", isRequired: false, schema: { type: "string" } });
283+
}
281284

282285
const definitionsArray: any[] = [];
283286
if (defsToUse) {

src/lib/core/useJsonSchemaEditor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export function useJsonSchemaEditor(
5757
): UseJsonSchemaEditorReturn {
5858
const { rootType = "object", defaultValue, onChange } = options;
5959

60+
const hasDefaultValue = defaultValue != null && Object.keys(defaultValue).length > 0;
61+
6062
const onChangeRef = useRef(onChange);
6163
onChangeRef.current = onChange;
6264

@@ -88,7 +90,7 @@ export function useJsonSchemaEditor(
8890

8991
const form = useForm<FormSchema>({
9092
resolver: zodResolver(formSchema) as any,
91-
defaultValues: defaultValue ? schemaToForm(defaultValue) : getDefaultValues(initialId),
93+
defaultValues: hasDefaultValue ? schemaToForm(defaultValue!) : getDefaultValues(initialId),
9294
});
9395

9496
const { setError, clearErrors, setValue, reset: formReset, getValues } = form;
@@ -177,11 +179,11 @@ export function useJsonSchemaEditor(
177179
}, [ajvErrors, clearErrors, setError, getValues]);
178180

179181
useEffect(() => {
180-
if (!defaultValue) {
182+
if (!hasDefaultValue) {
181183
const id = nanoid(6);
182184
formReset(getDefaultValues(id));
183185
}
184-
}, [rootType, formReset, defaultValue, getDefaultValues]);
186+
}, [rootType, formReset, hasDefaultValue, getDefaultValues]);
185187

186188
const addField = useCallback(() => {
187189
const id = nanoid(6);

0 commit comments

Comments
 (0)