模版解析正确

This commit is contained in:
dengqichen 2024-12-06 10:45:39 +08:00
parent 3c3342a94d
commit ab4db6b7c6

View File

@ -1,5 +1,6 @@
import React, { useMemo, useEffect } from 'react';
import { Form, Input, Select, InputNumber, Switch, Divider } from 'antd';
import type { Rule } from 'antd/es/form';
import { NodeType } from '../../../../types';
interface NodeConfigProps {
@ -103,33 +104,75 @@ const NodeConfig: React.FC<NodeConfigProps> = ({ nodeType, form, onValuesChange
format,
} = property;
const rules = [];
const rules: Rule[] = [];
// 添加必填规则
if (required) {
rules.push({ required: true, message: `请输入${title}` });
rules.push({ required: true, message: `请输入${title}` } as Rule);
}
// 添加长度限制
if (minLength !== undefined) {
rules.push({ min: minLength, message: `最少输入${minLength}个字符` });
rules.push({ type: 'string', min: minLength, message: `最少输入${minLength}个字符` } as Rule);
}
if (maxLength !== undefined) {
rules.push({ max: maxLength, message: `最多输入${maxLength}个字符` });
rules.push({ type: 'string', max: maxLength, message: `最多输入${maxLength}个字符` } as Rule);
}
// 添加数值范围限制
if (minimum !== undefined) {
rules.push({ min: minimum, message: `不能小于${minimum}` });
if (type === 'number') {
// 添加数字类型验证
rules.push({
type: 'number',
message: '请输入有效的数字',
transform(value) {
if (value === '' || value === undefined || value === null) return undefined;
const num = Number(value);
return isNaN(num) ? undefined : num;
},
} as Rule);
// 添加整数验证
rules.push({
validator: async (_: any, value: any) => {
if (value === undefined || value === null || value === '') return;
if (!Number.isInteger(Number(value))) {
throw new Error('请输入整数');
}
}
} as Rule);
// 最小值验证
if (minimum !== undefined) {
rules.push({
validator: async (_: any, value: any) => {
if (value === undefined || value === null || value === '') return;
const num = Number(value);
if (num < minimum) {
throw new Error(`不能小于${minimum}`);
}
}
} as Rule);
}
// 最大值验证
if (maximum !== undefined) {
rules.push({ max: maximum, message: `不能大于${maximum}` });
rules.push({
validator: async (_: any, value: any) => {
if (value === undefined || value === null || value === '') return;
const num = Number(value);
if (num > maximum) {
throw new Error(`不能大于${maximum}`);
}
}
} as Rule);
}
}
// 添加正则校验
if (pattern) {
try {
rules.push({ pattern: new RegExp(pattern), message: `格式不正确` });
rules.push({ pattern: new RegExp(pattern), message: `格式不正确` } as Rule);
} catch (error) {
console.error('正则表达式解析错误:', error);
}
@ -162,6 +205,8 @@ const NodeConfig: React.FC<NodeConfigProps> = ({ nodeType, form, onValuesChange
placeholder={`请输入${title}`}
min={minimum}
max={maximum}
precision={0} // 只允许整数
step={1} // 步长为1
/>
);
break;
@ -185,6 +230,18 @@ const NodeConfig: React.FC<NodeConfigProps> = ({ nodeType, form, onValuesChange
);
};
// 获取初始值
const getInitialValues = () => {
const values = { ...nodeDefaultConfig };
// 如果有执行器配置,添加执行器的默认值
if (selectedExecutor && executorDefaultConfig) {
Object.assign(values, executorDefaultConfig);
}
return values;
};
// 渲染节点基本配置
const renderNodeConfig = () => {
if (!nodeSchema?.properties) return null;
@ -207,10 +264,7 @@ const NodeConfig: React.FC<NodeConfigProps> = ({ nodeType, form, onValuesChange
form={form}
layout="vertical"
onValuesChange={onValuesChange}
initialValues={{
...nodeDefaultConfig,
...executorDefaultConfig
}}
initialValues={getInitialValues()}
>
{/* 节点基本配置 */}
{renderNodeConfig()}