From 678f6a180974caa6e9148d676a28ca34dfc7ca31 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Mon, 30 Dec 2024 16:17:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=A3=B0=E9=81=93=E6=92=92=E6=97=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Design/components/NodeConfigModal.tsx | 170 ++++++++---------- 1 file changed, 77 insertions(+), 93 deletions(-) diff --git a/frontend/src/pages/Workflow/Definition/Design/components/NodeConfigModal.tsx b/frontend/src/pages/Workflow/Definition/Design/components/NodeConfigModal.tsx index 9b156500..4af583b9 100644 --- a/frontend/src/pages/Workflow/Definition/Design/components/NodeConfigModal.tsx +++ b/frontend/src/pages/Workflow/Definition/Design/components/NodeConfigModal.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import {Tabs, TabsProps} from 'antd'; import {Cell} from '@antv/x6'; import {NodeDefinitionResponse} from "@/pages/Workflow/NodeDesign/types"; @@ -10,18 +10,8 @@ import { SheetFooter, } from "@/components/ui/sheet"; import {Button} from "@/components/ui/button"; -import { - Form, - FormControl, - FormField, - FormItem, - FormLabel, - FormMessage, -} from "@/components/ui/form"; import {Input} from "@/components/ui/input"; -import {useForm} from "react-hook-form"; -import {zodResolver} from "@hookform/resolvers/zod"; -import * as z from "zod"; +import {Label} from "@/components/ui/label"; interface NodeConfigDrawerProps { visible: boolean; @@ -42,100 +32,90 @@ const NodeConfigDrawer: React.FC = ({ onOk, onCancel, }) => { - // 创建动态的 schema - const createFormSchema = (schema: any) => { - if (!schema?.properties) return z.object({}); + const [panelValues, setPanelValues] = React.useState({}); + const [localValues, setLocalValues] = React.useState({}); + + // 初始化表单值,包括默认值 + const initializeFormValues = (schema: any) => { + if (!schema?.properties) return {}; - const schemaFields: Record = {}; + const initialValues: Variables = {}; Object.entries(schema.properties).forEach(([key, property]: [string, any]) => { - switch (property.type) { - case 'string': - schemaFields[key] = property.required ? z.string() : z.string().optional(); - break; - case 'number': - schemaFields[key] = property.required ? z.number() : z.number().optional(); - break; - case 'boolean': - schemaFields[key] = property.required ? z.boolean() : z.boolean().optional(); - break; - default: - schemaFields[key] = z.any(); + if (property.default !== undefined) { + initialValues[key] = property.default; } }); - return z.object(schemaFields); + return initialValues; }; - const panelFormSchema = createFormSchema(nodeDefinition?.panelVariablesSchema); - const localFormSchema = createFormSchema(nodeDefinition?.localVariablesSchema); - - const panelForm = useForm>({ - resolver: zodResolver(panelFormSchema), - defaultValues: nodeDefinition?.panelVariables || {}, - }); - - const localForm = useForm>({ - resolver: zodResolver(localFormSchema), - defaultValues: nodeDefinition?.localVariables || {}, - }); - - const handleSubmit = async () => { - try { - const panelValues = await panelForm.handleSubmit((data) => data)(); - const localValues = await localForm.handleSubmit((data) => data)(); - - const updatedNodeDefinition = { - ...nodeDefinition, - panelVariables: panelValues, - localVariables: localValues - }; - onOk(updatedNodeDefinition); - } catch (error) { - console.error('Validation failed:', error); + useEffect(() => { + if (nodeDefinition) { + // 合并默认值和已有值 + const panelDefaults = initializeFormValues(nodeDefinition.panelVariablesSchema); + const localDefaults = initializeFormValues(nodeDefinition.localVariablesSchema); + + setPanelValues({ + ...panelDefaults, + ...(nodeDefinition.panelVariables || {}) + }); + setLocalValues({ + ...localDefaults, + ...(nodeDefinition.localVariables || {}) + }); } + }, [nodeDefinition]); + + const handleSubmit = () => { + const updatedNodeDefinition = { + ...nodeDefinition, + panelVariables: panelValues, + localVariables: localValues + }; + onOk(updatedNodeDefinition); }; - const renderFormFields = (schema: any, form: any) => { + const renderFormFields = (schema: any, values: Variables, onChange: (key: string, value: any) => void) => { if (!schema?.properties) return null; return Object.entries(schema.properties).map(([key, property]: [string, any]) => ( - ( - - {property.title || key} - - - - - - )} - /> +
+ + onChange(key, e.target.value)} + readOnly={property.readOnly} + className={property.readOnly ? "bg-muted" : ""} + /> +
)); }; + const handlePanelChange = (key: string, value: any) => { + setPanelValues(prev => ({...prev, [key]: value})); + }; + + const handleLocalChange = (key: string, value: any) => { + setLocalValues(prev => ({...prev, [key]: value})); + }; + const items: TabsProps['items'] = [ nodeDefinition?.panelVariablesSchema && { key: 'panel', label: '面板变量', children: ( -
- - {renderFormFields(nodeDefinition.panelVariablesSchema, panelForm)} -
- +
+ {renderFormFields(nodeDefinition.panelVariablesSchema, panelValues, handlePanelChange)} +
) }, nodeDefinition?.localVariablesSchema && { key: 'local', label: '环境变量', children: ( -
- - {renderFormFields(nodeDefinition.localVariablesSchema, localForm)} -
- +
+ {renderFormFields(nodeDefinition.localVariablesSchema, localValues, handleLocalChange)} +
) } ].filter(Boolean) as TabsProps['items']; @@ -143,20 +123,24 @@ const NodeConfigDrawer: React.FC = ({ return ( !open && onCancel()}> - - 编辑节点 - {nodeDefinition?.nodeName || ''} - -
- +
+ + 编辑节点 - {nodeDefinition?.nodeName || ''} + +
+ +
+
+
+ + +
+
- - - - );