fix(utils): 完善生成数据源默认值

This commit is contained in:
roymondchen 2023-10-24 16:06:07 +08:00
parent 257c8c9fa1
commit 808c82f193

View File

@ -356,15 +356,42 @@ export const getDefaultValueFromFields = (fields: DataSchema[]) => {
fields.forEach((field) => {
if (typeof field.defaultValue !== 'undefined') {
if (field.type === 'array' && !Array.isArray(field.defaultValue)) {
data[field.name] = defaultValue.array;
return;
}
if (field.type === 'object' && !isObject(field.defaultValue)) {
if (typeof field.defaultValue === 'string') {
try {
data[field.name] = JSON.parse(field.defaultValue);
} catch (e) {
data[field.name] = defaultValue.object;
}
return;
}
data[field.name] = defaultValue.object;
return;
}
data[field.name] = field.defaultValue;
} else if (field.type === 'object') {
data[field.name] = field.fields ? getDefaultValueFromFields(field.fields) : {};
} else if (field.type) {
data[field.name] = defaultValue[field.type];
} else {
data[field.name] = undefined;
return;
}
if (field.type === 'object') {
data[field.name] = field.fields ? getDefaultValueFromFields(field.fields) : defaultValue.object;
return;
}
if (field.type) {
data[field.name] = defaultValue[field.type];
return;
}
data[field.name] = undefined;
});
return data;
};