This commit is contained in:
2026-07-17 14:31:39 +05:30
parent 83ecdcb500
commit 6c720c390c
6 changed files with 134 additions and 8 deletions

View File

@@ -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);