Skip to content

Commit 545b59d

Browse files
committed
update form
1 parent e323008 commit 545b59d

10 files changed

Lines changed: 146 additions & 107 deletions

File tree

frontend/packages/flow-pc/flow-pc-approval/src/components/flow-approval/components/form-view-component.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import React from "react";
22
import {useApprovalContext} from "@/components/flow-approval/hooks/use-approval-context";
33
import {ViewBindPlugin} from "@flow-engine/flow-types";
4-
import { Form } from "antd";
4+
import {Form as AntdForm} from "antd";
55
import {FlowFormView} from "@flow-engine/flow-pc-form";
66

7-
interface FormViewComponentProps{
8-
onValuesChange?:(values:any)=>void;
7+
interface FormViewComponentProps {
8+
onValuesChange?: (values: any) => void;
99
}
1010

1111
export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
1212
const {state, context} = useApprovalContext();
13-
const ViewComponent = ViewBindPlugin.getInstance().get(state.flow?.view || 'default') || FlowFormView ;
13+
const ViewComponent = ViewBindPlugin.getInstance().get(state.flow?.view || 'default') || FlowFormView;
1414

1515
const flowForm = state.flow?.form;
1616

1717
// 是否可合并审批
1818
const mergeable = state.flow?.mergeable || false;
1919
const todos = state.flow?.todos || [];
20-
const viewForms = todos.length>0?todos.map(item => {
20+
const viewForms= todos.length > 0 ? todos.map(item => {
2121
return {
22-
instance: Form.useForm()[0],
23-
data: item.data,
22+
instance: AntdForm.useForm()[0],
23+
data: item.data as any,
2424
}
25-
}):[
25+
}) : [
2626
{
27-
instance: Form.useForm()[0],
27+
instance: AntdForm.useForm()[0],
2828
data: undefined,
2929
}
3030
]
@@ -34,11 +34,18 @@ export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
3434
const formInstance = item.instance;
3535
const data = item.data;
3636
context.getPresenter().getFormActionContext().addAction({
37-
save(): any {
37+
save: () => {
3838
return formInstance.getFieldsValue();
3939
},
40-
key(): string {
40+
key: () => {
4141
return 'view-form'
42+
},
43+
validate: () => {
44+
return new Promise((resolve,reject) => {
45+
formInstance.validateFields()
46+
.then(resolve)
47+
.catch(reject)
48+
})
4249
}
4350
});
4451
formInstance.setFieldsValue(data);
@@ -59,7 +66,7 @@ export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
5966
<ViewComponent
6067
key={index}
6168
meta={flowForm}
62-
form={item.instance as any}
69+
form={item.instance}
6370
onValuesChange={props.onValuesChange}
6471
/>
6572
))}

frontend/packages/flow-pc/flow-pc-approval/src/components/flow-approval/presenters/action.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {FlowApprovalApi, State} from "@/components/flow-approval/typings";
2-
import {FormActionContext} from "@/components/flow-approval/presenters/form";
2+
import {FormActionContext} from "@flow-engine/flow-types";
33

44
export class FlowActionPresenter {
55

@@ -29,10 +29,27 @@ export class FlowActionPresenter {
2929
}
3030

3131

32-
public async action(actionId:string) {
32+
/**
33+
* 是否通过操作
34+
* @param actionId
35+
* @private
36+
*/
37+
private isPassAction(actionId:string){
38+
const actions = this.state.flow?.actions || [];
39+
for(const action of actions){
40+
if(action.id === actionId){
41+
if(action.type ==='PASS'){
42+
return true;
43+
}
44+
}
45+
}
46+
return false;
47+
}
48+
49+
50+
private async submitAction(actionId:string,formData:any){
3351
const recordId = this.state.flow?.recordId;
3452
const workId = this.state.flow?.workId;
35-
const formData = this.formActionContext.save();
3653
if(recordId){
3754
const request = {
3855
formData,
@@ -60,4 +77,15 @@ export class FlowActionPresenter {
6077
}
6178
}
6279

80+
81+
public async action(actionId:string) {
82+
let formData;
83+
if(this.isPassAction(actionId)){
84+
formData = await this.formActionContext.validate();
85+
}else {
86+
formData = this.formActionContext.save();
87+
}
88+
return await this.submitAction(actionId, formData);
89+
}
90+
6391
}

frontend/packages/flow-pc/flow-pc-approval/src/components/flow-approval/presenters/form.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

frontend/packages/flow-pc/flow-pc-approval/src/components/flow-approval/presenters/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {FlowApprovalApi, State} from "@/components/flow-approval/typings";
22
import {Dispatch} from "@flow-engine/flow-core";
3-
import {FormActionContext} from "@/components/flow-approval/presenters/form";
3+
import {FormActionContext} from "@flow-engine/flow-types";
44
import {FlowActionPresenter} from "@/components/flow-approval/presenters/action";
55

66
export class Presenter {

frontend/packages/flow-pc/flow-pc-design/src/components/design-panel/presenters/form.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

frontend/packages/flow-pc/flow-pc-design/src/components/design-panel/presenters/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {DesignPanelApi, FlowNode, initStateData, State, TabPanelType} from "../types";
22
import {Dispatch} from "@flow-engine/flow-core";
3-
import {FormActionContext} from "@/components/design-panel/presenters/form";
3+
import {FormActionContext} from "@flow-engine/flow-types";
44
import {WorkflowFormManager} from "@/components/design-panel/manager/form";
55
import {NodeConvertorManager} from "@/components/design-panel/manager/node";
66
import {WorkflowConvertor} from "@/components/design-panel/presenters/convertor";

frontend/packages/flow-pc/flow-pc-design/src/components/design-panel/tabs/base.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,34 @@ export const TabBase = () => {
6767
// 注册form行为
6868
React.useEffect(() => {
6969
formActionContext.addAction({
70-
save() {
70+
save:()=> {
7171
return baseForm.getFieldsValue();
7272
},
73-
key(): string {
73+
key:()=> {
7474
return 'base';
75+
},
76+
validate:()=>{
77+
return new Promise((resolve, reject) => {
78+
baseForm.validateFields().then(values => {
79+
resolve(values);
80+
}).catch(reject);
81+
})
7582
}
7683
});
7784

7885
formActionContext.addAction({
79-
save() {
86+
save:()=> {
8087
return operatorForm.getFieldsValue();
8188
},
82-
key(): string {
89+
key:()=> {
8390
return 'operator';
91+
},
92+
validate:()=>{
93+
return new Promise((resolve, reject) => {
94+
operatorForm.validateFields().then(values=>{
95+
resolve(values);
96+
}).catch(reject)
97+
})
8498
}
8599
});
86100

frontend/packages/flow-pc/flow-pc-design/src/components/design-panel/tabs/flow.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@ export const TabFlow = () => {
1515
// 注册form行为
1616
React.useEffect(() => {
1717
formActionContext.addAction({
18-
save() {
18+
save:()=> {
1919
const data = actionRef.current?.getData();
2020
return {
2121
nodes: nodeManager.toData(data?.nodes || []),
2222
}
2323
},
24-
key(): string {
24+
key:()=> {
2525
return 'flow';
26+
},
27+
validate:()=>{
28+
return new Promise((resolve, reject) => {
29+
const data = actionRef.current?.getData();
30+
const values = {
31+
nodes: nodeManager.toData(data?.nodes || []),
32+
}
33+
resolve(values);
34+
})
2635
}
2736
});
2837

frontend/packages/flow-pc/flow-pc-design/src/components/design-panel/tabs/setting.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from "react";
2-
import {Panel} from "@flow-engine/flow-pc-ui";
2+
import {CardForm, Panel} from "@flow-engine/flow-pc-ui";
33
import {InterferePanel} from "@/components/design-panel/panels/workflow/interfere";
44
import {UrgePanel} from "@/components/design-panel/panels/workflow/urge";
5-
import {CardForm} from "@flow-engine/flow-pc-ui";
65
import {useDesignContext} from "@/components/design-panel/hooks/use-design-context";
76
import {WorkflowStrategyManager} from "@/components/design-panel/manager/strategy";
87

@@ -16,7 +15,7 @@ export const TabSetting = () => {
1615

1716
const workflowStrategyManager = new WorkflowStrategyManager();
1817

19-
const resetFieldsValue = ()=>{
18+
const resetFieldsValue = () => {
2019
const formData = workflowStrategyManager.toRender(state.workflow.strategies as any[]);
2120
form.setFieldsValue(formData);
2221
}
@@ -31,11 +30,19 @@ export const TabSetting = () => {
3130
resetFieldsValue();
3231

3332
formActionContext.addAction({
34-
save() {
33+
save: () => {
3534
return workflowStrategyManager.toData(form.getFieldsValue());
3635
},
37-
key(): string {
36+
key: () => {
3837
return 'setting';
38+
},
39+
validate: () => {
40+
return new Promise((resolve, reject) => {
41+
form.validateFields()
42+
.then(values => {
43+
resolve(workflowStrategyManager.toData(values));
44+
}).catch(reject)
45+
})
3946
}
4047
});
4148

frontend/packages/flow-types/src/types/form-instance.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@ export interface FormInstance{
66
getFieldError: (name: NamePath) => string[];
77
resetFields: (fields?: NamePath[]) => void;
88
setFieldsValue: (values: any) => void;
9-
setFiledValue: (name: NamePath, value: any) => void;
9+
setFieldValue: (name: NamePath, value: any) => void;
1010
submit: () => void;
1111
}
12+
13+
14+
export interface IFormAction {
15+
save(): any;
16+
17+
key(): string;
18+
19+
validate():Promise<any>;
20+
}
21+
22+
23+
export class FormActionContext {
24+
25+
private readonly formActions: IFormAction[];
26+
27+
constructor() {
28+
this.formActions = [];
29+
}
30+
31+
public addAction(submit: IFormAction) {
32+
const keys = this.formActions.map(item => item.key());
33+
if (keys.includes(submit.key())) {
34+
return;
35+
}
36+
this.formActions.push(submit);
37+
}
38+
39+
public removeAction(key: string) {
40+
const index = this.formActions.findIndex(item => item.key() === key);
41+
if (index !== -1) {
42+
this.formActions.splice(index, 1);
43+
}
44+
}
45+
46+
public save() {
47+
let value = {};
48+
for (const form of this.formActions) {
49+
const data = form.save();
50+
value = Object.assign(value, data);
51+
}
52+
return value;
53+
}
54+
55+
public async validate() {
56+
let value = {};
57+
for (const form of this.formActions) {
58+
const data = await form.validate();
59+
value = Object.assign(value, data);
60+
}
61+
return value;
62+
}
63+
}

0 commit comments

Comments
 (0)