fixes
This commit is contained in:
@@ -15,13 +15,21 @@ export function DiscriminatorField({ field, value, onChange, error }: Props) {
|
||||
const discProp = field.discriminatorProperty ?? "type";
|
||||
const currentType = value?.[discProp] ?? "";
|
||||
|
||||
function defaultFieldValue(field: FieldConfig): any {
|
||||
if (field.enumValues) return field.enumValues[0];
|
||||
if (field.isArray) return [];
|
||||
if (field.type === "object") return {};
|
||||
if (field.type === "number" || field.type === "integer") return 0;
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleTypeChange = useCallback((e: any) => {
|
||||
const newType = e.target.value;
|
||||
const option = options.find((o) => o.value === newType);
|
||||
const newValue: Record<string, any> = { [discProp]: newType };
|
||||
if (option) {
|
||||
for (const f of option.fields) {
|
||||
newValue[f.name] = f.enumValues?.[0] ?? f.type === "number" ? 0 : f.type === "integer" ? 0 : "";
|
||||
newValue[f.name] = defaultFieldValue(f);
|
||||
}
|
||||
}
|
||||
onChange(newValue);
|
||||
|
||||
@@ -243,6 +243,10 @@ function buildDefaultShape(fields: FieldConfig[], schemas: Record<string, any>):
|
||||
const refSchemaObj = schemas[f.refSchema!];
|
||||
const nestedFields = refSchemaObj ? extractFields(f.refSchema!, refSchemaObj, schemas) : [];
|
||||
shape[f.name] = f.isArray ? [] : buildDefaultShape(nestedFields, schemas);
|
||||
} else if (f.isArray) {
|
||||
shape[f.name] = [];
|
||||
} else if (f.type === "object") {
|
||||
shape[f.name] = {};
|
||||
} else {
|
||||
shape[f.name] = null;
|
||||
}
|
||||
@@ -254,7 +258,7 @@ function initEditValue(value: any, field: FieldConfig, schemas: Record<string, a
|
||||
if (field.isArray) {
|
||||
return value ? value.map((item: any) => ({ ...item })) : [];
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
if (value != null && value !== "" && typeof value === "object") {
|
||||
return { ...value };
|
||||
}
|
||||
return buildDefaultShape(
|
||||
|
||||
Reference in New Issue
Block a user