Skip to content

Commit 10462bc

Browse files
committed
add condition Group
1 parent 918d14d commit 10462bc

9 files changed

Lines changed: 228 additions & 29 deletions

File tree

frontend/packages/flow-pc/flow-pc-design/src/components/design-editor/node-components/condition/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React from "react";
44
import {GroovyScriptPreview} from "@/components/script/components/groovy-script-preview";
55
import {EditOutlined} from "@ant-design/icons";
66
import {ConditionConfigModal} from "@/components/script/modal/condition-config-modal";
7+
import {useScriptVariables} from "@/components/design-editor/hooks/use-script-variables";
78

89
/**
910
* 条件配置
@@ -13,6 +14,7 @@ export const ConditionScript = ()=>{
1314

1415
const [form] = Form.useForm();
1516
const [visible,setVisible] = React.useState(false);
17+
const scriptVariables = useScriptVariables();
1618

1719
return (
1820
<Form
@@ -49,6 +51,7 @@ export const ConditionScript = ()=>{
4951
onCancel={()=>{setVisible(false);}}
5052
onConfirm={(value)=>{onChange(value)}}
5153
script={value}
54+
variables={scriptVariables}
5255
/>
5356
</Space.Compact>
5457
)}

frontend/packages/flow-pc/flow-pc-design/src/components/script/components/condition/components/condition-group/condition.tsx

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,126 @@
11
import {Condition} from "@/components/script/components/condition/typings";
22
import {Input, Select, Space} from "antd";
33
import React from "react";
4+
import {useConditionContext} from "@/components/script/components/condition/hooks/use-condition-context";
5+
import {GroovyVariableMapping} from "@/components/script/typings";
46

57
interface ConditionItemViewProps {
68
data?: Condition;
9+
id: string;
10+
location: 'left' | 'right';
11+
}
12+
13+
interface ConditionInputProps {
14+
value: string;
15+
onChange: (value: Condition) => void;
16+
}
17+
18+
const VariableSelect: React.FC<ConditionInputProps> = (props) => {
19+
20+
const {state} = useConditionContext();
21+
22+
const variables = React.useMemo(() => {
23+
const groups = new Map<string, GroovyVariableMapping[]>();
24+
25+
for (const mapping of state.variables) {
26+
const existing = groups.get(mapping.tag) || [];
27+
existing.push(mapping);
28+
groups.set(mapping.tag, existing);
29+
}
30+
31+
const options: any[] = [];
32+
for (const group of Array.from(groups.keys())) {
33+
const children = groups.get(group) || [];
34+
35+
options.push({
36+
label: group,
37+
options: children,
38+
})
39+
}
40+
41+
return options;
42+
}, [state.variables]);
43+
44+
const handleChange = (value: string, option: any) => {
45+
props.onChange({
46+
label: option.label,
47+
value: option.value,
48+
dataType: option.type,
49+
type: 'variable',
50+
});
51+
}
52+
53+
return (
54+
<Select
55+
showSearch={{
56+
filterOption: (value: string, option) => {
57+
if (option.value) {
58+
return option.value.includes(value) || option.label.includes(value);
59+
}
60+
return false;
61+
}
62+
}}
63+
value={props.value}
64+
placeholder={"请选择参数"}
65+
style={{width: "200px"}}
66+
options={variables}
67+
onChange={handleChange}
68+
/>
69+
)
70+
}
71+
72+
const TypeInput: React.FC<ConditionInputProps> = (props) => {
73+
74+
const handleChange = (value: string) => {
75+
props.onChange({
76+
label: value,
77+
value: value,
78+
dataType: 'STRING',
79+
type: 'input',
80+
});
81+
}
82+
83+
return (
84+
<Input
85+
style={{width: "200px"}}
86+
value={props.value}
87+
placeholder={"请输入参数"}
88+
onChange={(event)=>{
89+
handleChange(event.target.value);
90+
}}
91+
/>
92+
)
793
}
894

995
export const ConditionItemView: React.FC<ConditionItemViewProps> = (props) => {
10-
const label = props.data?.label || '未设置';
11-
const [type,setType] = React.useState('variable');
96+
const [type, setType] = React.useState(props.data?.type || 'variable');
97+
98+
const {context} = useConditionContext();
99+
100+
const presenter = context.getPresenter().getConditionGroupPresenter();
101+
102+
const handleUpdate = (value: any) => {
103+
const latest = {
104+
...props.data,
105+
...value
106+
}
107+
presenter.updateCondition(props.id,props.location, latest);
108+
}
109+
110+
React.useEffect(()=>{
111+
if(props.data) {
112+
setType(props.data.type);
113+
}
114+
},[props.data])
12115

13116
return (
14117
<Space.Compact>
15118
<Select
16-
style={{ width: "100px" }}
17-
defaultValue={type}
119+
style={{width: "100px"}}
120+
value={type}
18121
options={[
19122
{
20-
label: '插入参数',
123+
label: '选择变量',
21124
value: 'variable'
22125
},
23126
{
@@ -30,14 +133,20 @@ export const ConditionItemView: React.FC<ConditionItemViewProps> = (props) => {
30133
}}
31134
/>
32135
{type === "variable" && (
33-
<Select
34-
style={{ width: "100px" }}
136+
<VariableSelect
137+
value={props.data?.value || ''}
138+
onChange={(value) => {
139+
handleUpdate(value)
140+
}}
35141
/>
36142
)}
37143

38144
{type === "input" && (
39-
<Input
40-
style={{ width: "100px" }}
145+
<TypeInput
146+
value={props.data?.value || ''}
147+
onChange={(value) => {
148+
handleUpdate(value)
149+
}}
41150
/>
42151
)}
43152

frontend/packages/flow-pc/flow-pc-design/src/components/script/components/condition/components/condition-group/group.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,34 @@ export const Group = () => {
4343
title: '左侧参数',
4444
dataIndex: 'left',
4545
render: (_, record) => {
46-
return (<ConditionItemView data={record.left}/>)
46+
return (<ConditionItemView
47+
id={record.id}
48+
location={'left'}
49+
data={record.left}
50+
/>)
4751
}
4852
},
4953
{
5054
key: 'type',
5155
title: '条件关系',
5256
dataIndex: 'type',
5357
render: (_, record) => {
54-
return (<ConditionTypeView type={record.type}/>)
58+
return (<ConditionTypeView
59+
type={record.type}
60+
id={record.id}
61+
/>)
5562
}
5663
},
5764
{
5865
key: 'right',
5966
title: '右侧参数',
6067
dataIndex: 'right',
6168
render: (_, record) => {
62-
return (<ConditionItemView data={record.right}/>)
69+
return (<ConditionItemView
70+
id={record.id}
71+
location={'right'}
72+
data={record.right}
73+
/>)
6374
}
6475
},
6576
{
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import {RelationType, relationTypeOptions} from "@/components/script/components/condition/typings";
2+
import {Select} from "antd";
23
import React from "react";
4+
import {useConditionContext} from "@/components/script/components/condition/hooks/use-condition-context";
35

46
interface ConditionTypeViewProps {
57
type?: RelationType;
8+
id: string;
69
}
710

811
export const ConditionTypeView: React.FC<ConditionTypeViewProps> = (props) => {
912
const type = props.type;
10-
const typeLabel = relationTypeOptions.find(item => item.value === type);
11-
if (typeLabel) {
12-
return (
13-
<span>{typeLabel.label}</span>
14-
)
13+
14+
const {context} = useConditionContext();
15+
const presenter = context.getPresenter().getConditionGroupPresenter();
16+
17+
const handleChangeType = (value: string) => {
18+
presenter.updateType(props.id, value as RelationType);
1519
}
20+
1621
return (
17-
<span>未知</span>
22+
<Select
23+
placeholder={"请选择关系"}
24+
options={relationTypeOptions}
25+
value={type}
26+
style={{width: "100px"}}
27+
onChange={handleChangeType}
28+
/>
1829
)
1930
}

frontend/packages/flow-pc/flow-pc-design/src/components/script/components/condition/hooks/use-condition-context.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import {Presenter} from "../presenters";
33
import {useDispatch, useSelector} from "react-redux";
44
import {ConditionContext, ConditionContextScope} from "../context";
5-
import {ConditionReduxState,updateState} from "../store";
5+
import {ConditionReduxState, updateState} from "../store";
66
import {ConditionApiImpl} from "../models";
77
import {ConditionViewProps} from "../typings";
88
import {useScriptMetaData} from "@/components/script/hooks/use-script-meta-data";
@@ -38,21 +38,24 @@ export const createConditionContext = (props: ConditionViewProps) => {
3838
new ConditionApiImpl()
3939
);
4040
ref.current = new ConditionContextScope(presenter, props);
41-
ref.current.initState(initData);
41+
ref.current.initState({
42+
...initData,
43+
variables: props.variables
44+
});
4245
}
4346

4447
React.useEffect(() => {
4548
ref.current?.syncState(state);
4649
}, [state]);
4750

4851

49-
React.useEffect(()=>{
52+
React.useEffect(() => {
5053
// 关闭时清空redux的数据
51-
return ()=>{
54+
return () => {
5255
ref.current?.clearState();
5356
ref.current = undefined;
5457
}
55-
},[]);
58+
}, []);
5659

5760
return {
5861
state,

frontend/packages/flow-pc/flow-pc-design/src/components/script/components/condition/presenters/group-presenter.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {IdUtils} from "@/utils";
22
import {Presenter} from "./index";
3-
import {ConditionGroup} from "@/components/script/components/condition/typings";
3+
import {Condition, ConditionGroup, RelationType} from "@/components/script/components/condition/typings";
44

55
/**
66
* 条件分组的Presenter对象
@@ -12,6 +12,50 @@ export class ConditionGroupPresenter {
1212
this.presenter = presenter;
1313
}
1414

15+
/**
16+
* 更新条件参数
17+
* @param id
18+
* @param location
19+
* @param condition
20+
*/
21+
public updateCondition(id: string, location: 'left' | 'right', condition: Condition) {
22+
this.presenter.updateState(prevState => {
23+
const groups = prevState.groups.map(group => {
24+
if (group.id === id) {
25+
return {
26+
...group,
27+
[location]: condition
28+
};
29+
}
30+
return group;
31+
});
32+
console.log('updateCondition:', groups);
33+
return {
34+
...prevState,
35+
groups: groups
36+
}
37+
})
38+
}
39+
40+
41+
public updateType(id: string, type: RelationType) {
42+
this.presenter.updateState(prevState => {
43+
const groups = prevState.groups.map(group => {
44+
if (group.id === id) {
45+
return {
46+
...group,
47+
type: type
48+
};
49+
}
50+
return group;
51+
});
52+
return {
53+
...prevState,
54+
groups: groups
55+
}
56+
})
57+
}
58+
1559
/**
1660
* 添加条件
1761
*/

0 commit comments

Comments
 (0)