Skip to content

Commit bc133e3

Browse files
committed
add FlowFormView
1 parent c50f034 commit bc133e3

14 files changed

Lines changed: 173 additions & 72 deletions

File tree

frontend/apps/app-pc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@flow-engine/flow-types": "workspace:*",
1414
"@flow-engine/flow-pc-design": "workspace:*",
1515
"@flow-engine/flow-pc-ui": "workspace:*",
16+
"@flow-engine/flow-pc-form": "workspace:*",
1617
"@flow-engine/flow-pc-approval":"workspace:*",
1718
"antd": "^6.2.1",
1819
"dayjs": "^1.11.19",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import {ViewBindPlugin} from "@flow-engine/flow-types";
2-
import {LeaveView} from "@/views/leave.tsx";
2+
import {FlowFormView} from "@flow-engine/flow-pc-form";
33

4-
ViewBindPlugin.getInstance().register('default',LeaveView);
4+
ViewBindPlugin.getInstance().register('default',FlowFormView);

frontend/apps/app-pc/src/views/leave.tsx

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

frontend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"build:app-pc": "pnpm -F @flow-engine/app-pc build",
1616
"dev:app-pc": "pnpm -F @flow-engine/app-pc dev",
1717
"dev:app-mobile": "pnpm -F @flow-engine/app-mobile dev",
18-
"watch:flow-design": "pnpm -F @flow-engine/flow-design dev",
19-
"watch:flow-form": "pnpm -F @flow-engine/flow-form dev"
18+
"watch:flow-pc-design": "pnpm -F @flow-engine/flow-pc-design dev",
19+
"watch:flow-pc-form": "pnpm -F @flow-engine/flow-pc-form dev"
2020
},
2121
"keywords": [],
2222
"author": "",

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ interface FormViewComponentProps{
1010
export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
1111
const {state, context} = useApprovalContext();
1212
const ViewComponent = ViewBindPlugin.getInstance().get(state.flow?.view || 'default');
13+
14+
const formMeta = state.flow?.form;
15+
1316
// 是否可合并审批
1417
const mergeable = state.flow?.mergeable || false;
1518
const todos = state.flow?.todos || [];
@@ -41,7 +44,7 @@ export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
4144
});
4245
}, []);
4346

44-
if (ViewComponent) {
47+
if (ViewComponent && formMeta) {
4548
if (mergeable) {
4649
return (
4750
<div>
@@ -54,6 +57,7 @@ export const FormViewComponent: React.FC<FormViewComponentProps> = (props) => {
5457
{viewForms.map((item, index) => (
5558
<ViewComponent
5659
key={index}
60+
meta={formMeta}
5761
form={item.instance as any}
5862
onValuesChange={props.onValuesChange}
5963
/>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {FlowFormFieldMeta} from "@flow-engine/flow-types";
2+
import React from "react";
3+
4+
import {FormItemString} from "@/components/item/string";
5+
import {FormItemNumber} from "@/components/item/number";
6+
import {FormItemFloat} from "@/components/item/float";
7+
import {FormItemBoolean} from "@/components/item/boolean";
8+
9+
10+
export class FormItemFactory {
11+
12+
private readonly cache:Map<string,React.ComponentType<FlowFormFieldMeta>>;
13+
14+
private static instance:FormItemFactory = new FormItemFactory();
15+
16+
private constructor() {
17+
this.cache = new Map();
18+
this.cache.set('number',FormItemNumber);
19+
this.cache.set('string',FormItemString);
20+
this.cache.set('float',FormItemFloat);
21+
this.cache.set('boolean',FormItemBoolean);
22+
}
23+
24+
public static getInstance():FormItemFactory {
25+
return FormItemFactory.instance;
26+
}
27+
28+
public createFrom(formType:string){
29+
return this.cache.get(formType);
30+
}
31+
32+
}
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
import React from "react";
22
import {type ViewComponentProps} from "@flow-engine/flow-types";
3-
import {Form, Input} from "antd";
3+
import {Form} from "antd";
44
import {ObjectUtils} from "@flow-engine/flow-core";
5+
import {FormItemFactory} from "@/components/factory/form-item-factory";
56

67
export const FlowFormView: React.FC<ViewComponentProps> = (props) => {
78

89
const [values, setValues] = React.useState<any>({});
910

1011
const form = props.form;
1112

13+
const meta = props.meta;
14+
15+
const fields = meta.fields || [];
16+
1217
return (
1318
<Form
1419
form={form as any}
1520
layout={"vertical"}
16-
onBlur={()=>{
21+
onBlur={() => {
1722
const latestValues = form.getFieldsValue();
18-
if(ObjectUtils.isEqual(values,latestValues)){
23+
if (ObjectUtils.isEqual(values, latestValues)) {
1924
return;
2025
}
2126
setValues(latestValues);
2227
props.onValuesChange?.(latestValues);
2328
}}
2429
>
25-
<Form.Item
26-
name={"days"}
27-
label={"请假天数"}
28-
>
29-
<Input/>
30-
</Form.Item>
31-
32-
<Form.Item
33-
name={"desc"}
34-
label={"请假理由"}
35-
>
36-
<Input/>
37-
</Form.Item>
30+
{fields.map((field, i) => {
31+
const FormItem = FormItemFactory.getInstance().createFrom(field.type);
32+
if (FormItem) {
33+
return (
34+
<FormItem
35+
key={field.id}
36+
{...field}
37+
/>
38+
);
39+
}
40+
})}
3841
</Form>
3942
)
4043
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
import {Form, Switch} from "antd";
3+
import {FlowFormFieldMeta} from "@flow-engine/flow-types";
4+
5+
export const FormItemBoolean:React.FC<FlowFormFieldMeta> = (props)=>{
6+
7+
const rules = props.required?[
8+
{
9+
required: props.required,
10+
message: `${props.name}不能为空`
11+
}
12+
]:[];
13+
14+
const defaultValue = props.defaultValue === "true" || false;
15+
16+
return (
17+
<Form.Item
18+
name={props.code}
19+
label={props.name}
20+
required={props.required}
21+
rules={rules}
22+
>
23+
<Switch defaultValue={defaultValue}/>
24+
</Form.Item>
25+
)
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from "react";
2+
import {Form, Input} from "antd";
3+
import {FlowFormFieldMeta} from "@flow-engine/flow-types";
4+
5+
export const FormItemFloat:React.FC<FlowFormFieldMeta> = (props)=>{
6+
7+
const rules = props.required?[
8+
{
9+
required: props.required,
10+
message: `${props.name}不能为空`
11+
}
12+
]:[];
13+
14+
return (
15+
<Form.Item
16+
name={props.code}
17+
label={props.name}
18+
required={props.required}
19+
rules={rules}
20+
>
21+
<Input type={"number"} defaultValue={props.defaultValue}/>
22+
</Form.Item>
23+
)
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from "react";
2+
import {Form, Input} from "antd";
3+
import {FlowFormFieldMeta} from "@flow-engine/flow-types";
4+
5+
export const FormItemNumber:React.FC<FlowFormFieldMeta> = (props)=>{
6+
7+
const rules = props.required?[
8+
{
9+
required: props.required,
10+
message: `${props.name}不能为空`
11+
}
12+
]:[];
13+
14+
return (
15+
<Form.Item
16+
name={props.code}
17+
label={props.name}
18+
required={props.required}
19+
rules={rules}
20+
>
21+
<Input type={"number"} defaultValue={props.defaultValue}/>
22+
</Form.Item>
23+
)
24+
}

0 commit comments

Comments
 (0)