大声道撒旦
This commit is contained in:
parent
4ac633d295
commit
678f6a1809
@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import {Tabs, TabsProps} from 'antd';
|
import {Tabs, TabsProps} from 'antd';
|
||||||
import {Cell} from '@antv/x6';
|
import {Cell} from '@antv/x6';
|
||||||
import {NodeDefinitionResponse} from "@/pages/Workflow/NodeDesign/types";
|
import {NodeDefinitionResponse} from "@/pages/Workflow/NodeDesign/types";
|
||||||
@ -10,18 +10,8 @@ import {
|
|||||||
SheetFooter,
|
SheetFooter,
|
||||||
} from "@/components/ui/sheet";
|
} from "@/components/ui/sheet";
|
||||||
import {Button} from "@/components/ui/button";
|
import {Button} from "@/components/ui/button";
|
||||||
import {
|
|
||||||
Form,
|
|
||||||
FormControl,
|
|
||||||
FormField,
|
|
||||||
FormItem,
|
|
||||||
FormLabel,
|
|
||||||
FormMessage,
|
|
||||||
} from "@/components/ui/form";
|
|
||||||
import {Input} from "@/components/ui/input";
|
import {Input} from "@/components/ui/input";
|
||||||
import {useForm} from "react-hook-form";
|
import {Label} from "@/components/ui/label";
|
||||||
import {zodResolver} from "@hookform/resolvers/zod";
|
|
||||||
import * as z from "zod";
|
|
||||||
|
|
||||||
interface NodeConfigDrawerProps {
|
interface NodeConfigDrawerProps {
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
@ -42,100 +32,90 @@ const NodeConfigDrawer: React.FC<NodeConfigDrawerProps> = ({
|
|||||||
onOk,
|
onOk,
|
||||||
onCancel,
|
onCancel,
|
||||||
}) => {
|
}) => {
|
||||||
// 创建动态的 schema
|
const [panelValues, setPanelValues] = React.useState<Variables>({});
|
||||||
const createFormSchema = (schema: any) => {
|
const [localValues, setLocalValues] = React.useState<Variables>({});
|
||||||
if (!schema?.properties) return z.object({});
|
|
||||||
|
|
||||||
const schemaFields: Record<string, any> = {};
|
// 初始化表单值,包括默认值
|
||||||
|
const initializeFormValues = (schema: any) => {
|
||||||
|
if (!schema?.properties) return {};
|
||||||
|
|
||||||
|
const initialValues: Variables = {};
|
||||||
Object.entries(schema.properties).forEach(([key, property]: [string, any]) => {
|
Object.entries(schema.properties).forEach(([key, property]: [string, any]) => {
|
||||||
switch (property.type) {
|
if (property.default !== undefined) {
|
||||||
case 'string':
|
initialValues[key] = property.default;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return z.object(schemaFields);
|
return initialValues;
|
||||||
};
|
};
|
||||||
|
|
||||||
const panelFormSchema = createFormSchema(nodeDefinition?.panelVariablesSchema);
|
useEffect(() => {
|
||||||
const localFormSchema = createFormSchema(nodeDefinition?.localVariablesSchema);
|
if (nodeDefinition) {
|
||||||
|
// 合并默认值和已有值
|
||||||
|
const panelDefaults = initializeFormValues(nodeDefinition.panelVariablesSchema);
|
||||||
|
const localDefaults = initializeFormValues(nodeDefinition.localVariablesSchema);
|
||||||
|
|
||||||
const panelForm = useForm<z.infer<typeof panelFormSchema>>({
|
setPanelValues({
|
||||||
resolver: zodResolver(panelFormSchema),
|
...panelDefaults,
|
||||||
defaultValues: nodeDefinition?.panelVariables || {},
|
...(nodeDefinition.panelVariables || {})
|
||||||
});
|
});
|
||||||
|
setLocalValues({
|
||||||
const localForm = useForm<z.infer<typeof localFormSchema>>({
|
...localDefaults,
|
||||||
resolver: zodResolver(localFormSchema),
|
...(nodeDefinition.localVariables || {})
|
||||||
defaultValues: nodeDefinition?.localVariables || {},
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}, [nodeDefinition]);
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = () => {
|
||||||
try {
|
|
||||||
const panelValues = await panelForm.handleSubmit((data) => data)();
|
|
||||||
const localValues = await localForm.handleSubmit((data) => data)();
|
|
||||||
|
|
||||||
const updatedNodeDefinition = {
|
const updatedNodeDefinition = {
|
||||||
...nodeDefinition,
|
...nodeDefinition,
|
||||||
panelVariables: panelValues,
|
panelVariables: panelValues,
|
||||||
localVariables: localValues
|
localVariables: localValues
|
||||||
};
|
};
|
||||||
onOk(updatedNodeDefinition);
|
onOk(updatedNodeDefinition);
|
||||||
} catch (error) {
|
|
||||||
console.error('Validation failed:', error);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderFormFields = (schema: any, form: any) => {
|
const renderFormFields = (schema: any, values: Variables, onChange: (key: string, value: any) => void) => {
|
||||||
if (!schema?.properties) return null;
|
if (!schema?.properties) return null;
|
||||||
|
|
||||||
return Object.entries(schema.properties).map(([key, property]: [string, any]) => (
|
return Object.entries(schema.properties).map(([key, property]: [string, any]) => (
|
||||||
<FormField
|
<div key={key} className="space-y-2">
|
||||||
key={key}
|
<Label>{property.title || key}</Label>
|
||||||
control={form.control}
|
<Input
|
||||||
name={key}
|
value={values[key] || ''}
|
||||||
render={({field}) => (
|
placeholder={property.description}
|
||||||
<FormItem>
|
onChange={(e) => onChange(key, e.target.value)}
|
||||||
<FormLabel>{property.title || key}</FormLabel>
|
readOnly={property.readOnly}
|
||||||
<FormControl>
|
className={property.readOnly ? "bg-muted" : ""}
|
||||||
<Input {...field} placeholder={property.description} />
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
));
|
));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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'] = [
|
const items: TabsProps['items'] = [
|
||||||
nodeDefinition?.panelVariablesSchema && {
|
nodeDefinition?.panelVariablesSchema && {
|
||||||
key: 'panel',
|
key: 'panel',
|
||||||
label: '面板变量',
|
label: '面板变量',
|
||||||
children: (
|
children: (
|
||||||
<Form {...panelForm}>
|
<div className="space-y-4">
|
||||||
<form className="space-y-4">
|
{renderFormFields(nodeDefinition.panelVariablesSchema, panelValues, handlePanelChange)}
|
||||||
{renderFormFields(nodeDefinition.panelVariablesSchema, panelForm)}
|
</div>
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
nodeDefinition?.localVariablesSchema && {
|
nodeDefinition?.localVariablesSchema && {
|
||||||
key: 'local',
|
key: 'local',
|
||||||
label: '环境变量',
|
label: '环境变量',
|
||||||
children: (
|
children: (
|
||||||
<Form {...localForm}>
|
<div className="space-y-4">
|
||||||
<form className="space-y-4">
|
{renderFormFields(nodeDefinition.localVariablesSchema, localValues, handleLocalChange)}
|
||||||
{renderFormFields(nodeDefinition.localVariablesSchema, localForm)}
|
</div>
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
].filter(Boolean) as TabsProps['items'];
|
].filter(Boolean) as TabsProps['items'];
|
||||||
@ -143,20 +123,24 @@ const NodeConfigDrawer: React.FC<NodeConfigDrawerProps> = ({
|
|||||||
return (
|
return (
|
||||||
<Sheet open={visible} onOpenChange={(open) => !open && onCancel()}>
|
<Sheet open={visible} onOpenChange={(open) => !open && onCancel()}>
|
||||||
<SheetContent side="right" className="w-[480px] sm:w-[480px]">
|
<SheetContent side="right" className="w-[480px] sm:w-[480px]">
|
||||||
|
<div className="flex flex-col h-full">
|
||||||
<SheetHeader>
|
<SheetHeader>
|
||||||
<SheetTitle>编辑节点 - {nodeDefinition?.nodeName || ''}</SheetTitle>
|
<SheetTitle>编辑节点 - {nodeDefinition?.nodeName || ''}</SheetTitle>
|
||||||
</SheetHeader>
|
</SheetHeader>
|
||||||
<div className="flex-1 overflow-y-auto py-6">
|
<div className="flex-1 overflow-y-auto py-6">
|
||||||
<Tabs items={items}/>
|
<Tabs items={items} className="border-none"/>
|
||||||
</div>
|
</div>
|
||||||
<SheetFooter>
|
<div className="shrink-0 border-t bg-background p-4">
|
||||||
|
<div className="flex justify-end gap-2">
|
||||||
<Button variant="outline" onClick={onCancel}>
|
<Button variant="outline" onClick={onCancel}>
|
||||||
取消
|
取消
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={handleSubmit}>
|
<Button onClick={handleSubmit}>
|
||||||
确定
|
确定
|
||||||
</Button>
|
</Button>
|
||||||
</SheetFooter>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</SheetContent>
|
</SheetContent>
|
||||||
</Sheet>
|
</Sheet>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user