Skip to content

Commit 8c9d96d

Browse files
committed
add option
1 parent 856ef36 commit 8c9d96d

12 files changed

Lines changed: 125 additions & 25 deletions

File tree

apps/app-mobile/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@form-example/app-mobile",
3-
"version": "0.0.16",
3+
"version": "0.0.18",
44
"private": true,
55
"type": "module",
66
"scripts": {

apps/app-pc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@form-example/app-pc",
3-
"version": "0.0.16",
3+
"version": "0.0.18",
44
"private": true,
55
"type": "module",
66
"scripts": {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {Form, Select} from "antd";
2+
import React from "react";
3+
import {type FormItemProps, useFormContext} from "@coding-form/form-engine";
4+
5+
export const FormSelect: React.FC<FormItemProps> = (props) => {
6+
7+
const {context} = useFormContext();
8+
9+
const instance = context.getFormInstance();
10+
11+
const AttributeUtils = React.useMemo(() => {
12+
let data = {} as any;
13+
if (props.attributes && props.attributes.length > 0) {
14+
props.attributes.forEach(attribute => {
15+
if(attribute.label && attribute.value) {
16+
const item = {
17+
[attribute.label]: attribute.value,
18+
}
19+
// 合并数据到data
20+
Object.assign(data, item);
21+
}
22+
});
23+
}
24+
return {
25+
getAttribute: (name: string) => {
26+
return data[name];
27+
}
28+
}
29+
}, [props.attributes]);
30+
31+
return (
32+
<Form.Item
33+
name={props.name}
34+
key={props.name}
35+
label={props.label}
36+
required={props.required}
37+
hidden={props.hidden}
38+
rules={props.rules}
39+
layout={props.layout}
40+
>
41+
<Select
42+
options={AttributeUtils.getAttribute('DataSource')}
43+
placeholder={props.placeholder}
44+
onChange={(value, option) => {
45+
props.onChange?.(value, option);
46+
}}
47+
onBlur={(_) => {
48+
const value = instance.getFieldValue(props.name);
49+
props.onBlur?.(value);
50+
}}
51+
/>
52+
</Form.Item>
53+
)
54+
55+
}

apps/app-pc/src/config/form-register.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {registerFormItems} from "@coding-form/form-engine";
22
import {FormString} from "@/components/string";
3+
import {FormSelect} from "@/components/select";
34
import {Form} from "antd";
45
import {LayoutRegister} from "@coding-form/form-engine";
56
import {CardFormLayout} from "@/layout/card-form-layout.tsx";
67

8+
79
export const registerForms = () => {
810

911
registerFormItems(Form,[
@@ -13,5 +15,12 @@ export const registerForms = () => {
1315
}
1416
]);
1517

18+
registerFormItems(Form,[
19+
{
20+
type: 'select',
21+
componentType:FormSelect
22+
}
23+
]);
24+
1625
LayoutRegister.getInstance().register('card',CardFormLayout);
1726
}

apps/app-pc/src/pages/home.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,32 @@ const HomePage = () => {
3535
dataType: 'STRING',
3636
hidden: false,
3737
required: true,
38-
}
38+
},
39+
{
40+
id: "option",
41+
name: 'option',
42+
code: 'option',
43+
type: "select",
44+
dataType: 'STRING',
45+
hidden: false,
46+
required: true,
47+
attributes: [
48+
{
49+
key: '123',
50+
label: 'DataSource',
51+
value: [
52+
{
53+
label: '1',
54+
value: '1'
55+
},
56+
{
57+
label: '2',
58+
value: '2'
59+
}
60+
]
61+
}
62+
]
63+
},
3964
],
4065
subForms: []
4166
}
@@ -86,6 +111,13 @@ const HomePage = () => {
86111
instance.setFieldValue('id', value);
87112
instance.refreshFields('name');
88113
}
114+
},
115+
{
116+
type: 'change',
117+
target: 'option',
118+
event: (_: FormInstance, value: any, option) => {
119+
console.log('value', value, option);
120+
}
89121
}
90122
]}
91123
layouts={[
@@ -104,6 +136,10 @@ const HomePage = () => {
104136
{
105137
code: 'name',
106138
span: 24
139+
},
140+
{
141+
code: 'option',
142+
span: 24
107143
}
108144
]
109145
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coding-form/root",
3-
"version": "0.0.16",
3+
"version": "0.0.18",
44
"description": "form-engine",
55
"main": "index.js",
66
"scripts": {

packages/form-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coding-form/form-engine",
3-
"version": "0.0.16",
3+
"version": "0.0.18",
44
"description": "form-engine components",
55
"keywords": [
66
"coding-form",

packages/form-engine/src/event/event-context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ export class EventContext {
99
this.events = events;
1010
}
1111

12-
public handlerOnChange(formInstance:FormInstance,target: FieldKey, value: any) {
12+
public handlerOnChange(formInstance: FormInstance, target: FieldKey, value: any, option: any) {
1313
const events = this.getEvents('change', target);
1414
if (events && events.length > 0) {
1515
for (const event of events) {
16-
event.event(formInstance,value);
16+
event.event(formInstance, value, option);
1717
}
1818
}
1919
}
2020

2121

22-
public handlerOnBlur(formInstance:FormInstance,target: FieldKey,value:any) {
22+
public handlerOnBlur(formInstance: FormInstance, target: FieldKey, value: any) {
2323
const events = this.getEvents('blur', target);
2424
if (events && events.length > 0) {
2525
for (const event of events) {
26-
event.event(formInstance,value);
26+
event.event(formInstance, value);
2727
}
2828
}
2929
}

packages/form-engine/src/factory/index.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class FormItemFactory {
1717
return this.instance;
1818
}
1919

20-
public register(type: string,componentType:React.ComponentType<FormItemProps>) {
20+
public register(type: string, componentType: React.ComponentType<FormItemProps>) {
2121
this.cache.set(type, componentType);
2222
}
2323

@@ -27,17 +27,17 @@ export class FormItemFactory {
2727
}
2828

2929

30-
public render(formCode:string,
31-
formField:FormField,
32-
layout:'horizontal' | 'vertical',
33-
readOnly:boolean,
34-
context:FormContextScope){
30+
public render(formCode: string,
31+
formField: FormField,
32+
layout: 'horizontal' | 'vertical',
33+
readOnly: boolean,
34+
context: FormContextScope) {
3535

3636
const formItemProps: FormItemProps = {
3737
...formField,
3838
readOnly: readOnly,
39-
name:formField.code,
40-
label:formField.name
39+
name: formField.code,
40+
label: formField.name
4141
};
4242

4343
const FormItem = FormItemFactory.getInstance().getItem(formField.type);
@@ -48,17 +48,17 @@ export class FormItemFactory {
4848

4949
const fieldKey = {
5050
formCode,
51-
fieldCode:formField.code,
51+
fieldCode: formField.code,
5252
}
5353

54-
const handlerOnChange = (value:any)=>{
54+
const handlerOnChange = (value: any, option: any) => {
5555
formItemProps.onChange?.(value);
56-
eventContext.handlerOnChange(instance,fieldKey, value);
56+
eventContext.handlerOnChange(instance, fieldKey, value, option);
5757
}
5858

59-
const handlerOnBlur = (value:any) => {
59+
const handlerOnBlur = (value: any) => {
6060
formItemProps.onBlur?.(value);
61-
eventContext.handlerOnBlur(instance,fieldKey,value);
61+
eventContext.handlerOnBlur(instance, fieldKey, value);
6262
}
6363

6464
if (FormItem) {

packages/form-engine/src/types/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface FormEvent {
1313
target?:FieldKey;
1414

1515
// 事件触发
16-
event:(instance:FormInstance,value?:any)=>void;
16+
event:(instance:FormInstance,value?:any,option?:any)=>void;
1717

1818
}
1919

0 commit comments

Comments
 (0)