This commit is contained in:
dengqichen 2024-12-19 16:45:38 +08:00
parent dd57d2d8c8
commit 301f42c67a

View File

@ -79,11 +79,15 @@ const NodeDesignForm: React.FC = () => {
// 递归获取默认值 // 递归获取默认值
const getDefaultValues = (schema: any) => { const getDefaultValues = (schema: any) => {
console.log('Current Schema:', schema);
if (!schema || !schema.properties) return {}; if (!schema || !schema.properties) return {};
return Object.entries(schema.properties).reduce((acc, [key, value]: [string, any]) => { const result = Object.entries(schema.properties).reduce((acc, [key, value]: [string, any]) => {
console.log(`Processing key: ${key}, value:`, value);
if (value.type === 'object') { if (value.type === 'object') {
acc[key] = getDefaultValues(value); const nestedDefaults = getDefaultValues(value);
console.log(`Nested defaults for ${key}:`, nestedDefaults);
acc[key] = nestedDefaults;
} else if (value.type === 'array' && value.items) { } else if (value.type === 'array' && value.items) {
acc[key] = value.default || []; acc[key] = value.default || [];
} else { } else {
@ -91,6 +95,9 @@ const NodeDesignForm: React.FC = () => {
} }
return acc; return acc;
}, {}); }, {});
console.log('Result for schema:', result);
return result;
}; };
// 将 JSON Schema 转换为 ProForm Schema // 将 JSON Schema 转换为 ProForm Schema
@ -99,65 +106,22 @@ const NodeDesignForm: React.FC = () => {
return Object.entries(schema.properties).map(([key, value]: [string, any]) => { return Object.entries(schema.properties).map(([key, value]: [string, any]) => {
const fullKey = parentKey ? `${parentKey}.${key}` : key; const fullKey = parentKey ? `${parentKey}.${key}` : key;
console.log('Converting schema key:', fullKey, 'value:', value);
// 基础字段配置
const baseField = { const baseField = {
title: value.title || key, title: value.title,
dataIndex: fullKey, dataIndex: fullKey,
tooltip: value.description, tooltip: value.description,
fieldProps: {
style: { width: '100%' }
}
}; };
// 处理枚举类型
if (value.enum) {
return {
...baseField,
valueType: 'select',
valueEnum: value.enum.reduce((acc: any, item: string, index: number) => {
acc[item] = { text: value.enumNames?.[index] || item };
return acc;
}, {})
};
}
// 处理不同类型的字段 // 处理不同类型的字段
if (value.type === 'string') { if (value.type === 'object') {
return {
...baseField,
valueType: 'text'
};
} else if (value.type === 'number' || value.type === 'integer') {
return {
...baseField,
valueType: 'digit'
};
} else if (value.type === 'boolean') {
return {
...baseField,
valueType: 'switch'
};
} else if (value.type === 'array') {
const itemColumns = value.items ? convertToProFormSchema(
{ properties: { item: value.items } },
`${fullKey}.item`,
level + 1
) : [];
return {
...baseField,
valueType: 'formList',
columns: itemColumns
};
} else if (value.type === 'object') {
const childColumns = value.properties ? convertToProFormSchema(value, fullKey, level + 1) : [];
return { return {
...baseField, ...baseField,
dataIndex: fullKey,
valueType: 'group', valueType: 'group',
columns: childColumns, columns: convertToProFormSchema(value, fullKey, level + 1),
colProps: {
span: 24
},
fieldProps: { fieldProps: {
title: value.title, title: value.title,
style: { style: {
@ -171,7 +135,48 @@ const NodeDesignForm: React.FC = () => {
}; };
} }
return baseField; // 处理枚举类型
if (value.enum) {
return {
...baseField,
valueType: 'select',
valueEnum: value.enum.reduce((acc: any, item: string, index: number) => {
acc[item] = { text: value.enumNames?.[index] || item };
return acc;
}, {}),
initialValue: value.default
};
}
// 处理数组类型
if (value.type === 'array') {
const itemColumns = value.items ? convertToProFormSchema(
{ properties: { item: value.items } },
`${fullKey}.item`,
level + 1
) : [];
return {
...baseField,
valueType: 'formList',
columns: itemColumns,
initialValue: value.default
};
}
// 处理基础类型
const typeMap: Record<string, string> = {
'string': 'text',
'integer': 'digit',
'number': 'digit',
'boolean': 'switch'
};
return {
...baseField,
valueType: typeMap[value.type] || 'text',
initialValue: value.default
};
}); });
}; };
@ -187,6 +192,9 @@ const NodeDesignForm: React.FC = () => {
// 判断是否是 schema 格式 // 判断是否是 schema 格式
const isSchema = typeof content === 'object' && 'type' in content && 'properties' in content; const isSchema = typeof content === 'object' && 'type' in content && 'properties' in content;
console.log('Is Schema:', isSchema);
console.log('Content:', content);
const schema = isSchema ? content : { const schema = isSchema ? content : {
type: 'object', type: 'object',
properties: Object.entries(content).reduce((acc, [key, value]) => ({ properties: Object.entries(content).reduce((acc, [key, value]) => ({
@ -199,8 +207,10 @@ const NodeDesignForm: React.FC = () => {
}), {}) }), {})
}; };
console.log('Final Schema:', schema);
// 获取默认值 // 获取默认值
const defaultValues = getDefaultValues(schema); const defaultValues = getDefaultValues(schema);
console.log('Default Values:', defaultValues);
return ( return (
<Card bordered={false}> <Card bordered={false}>