Skip to content

Commit 47f30da

Browse files
committed
add hiddenFields requiredFields
1 parent 9a1817a commit 47f30da

11 files changed

Lines changed: 48 additions & 16 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createFormInstance, FormView} from "@coding-form/form-view";
22
import type {FormMeta} from "@coding-form/form-view";
33
import {Button, Space} from "antd-mobile";
4+
import {FormInstance} from "@coding-form/form-view";
45

56
const HomePage = () => {
67

@@ -32,7 +33,7 @@ const HomePage = () => {
3233
validators={[
3334
{
3435
target:'name',
35-
validator:(value,instance)=>{
36+
validator:(instance:FormInstance,value:any)=>{
3637
if(value){
3738
return true;
3839
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createFormInstance, FormView} from "@coding-form/form-view";
22
import type {FormMeta} from "@coding-form/form-view";
33
import {Button, Space} from "antd";
4+
import {FormInstance} from "@coding-form/form-view";
45

56
const HomePage = () => {
67

@@ -9,7 +10,16 @@ const HomePage = () => {
910
code: "leave",
1011
fields: [
1112
{
12-
id: "1",
13+
id: "id",
14+
name: 'id',
15+
code: 'id',
16+
type: "string",
17+
dataType: 'STRING',
18+
hidden: true,
19+
required: false,
20+
},
21+
{
22+
id: "name",
1323
name: '姓名',
1424
code: 'name',
1525
type: "string",
@@ -31,7 +41,7 @@ const HomePage = () => {
3141
validators={[
3242
{
3343
target:'name',
34-
validator:(value,instance)=>{
44+
validator:(instance:FormInstance,value:any)=>{
3545
if(value){
3646
return true;
3747
}
@@ -43,8 +53,9 @@ const HomePage = () => {
4353
{
4454
type: 'change',
4555
target:'name',
46-
event:(value)=>{
47-
console.log('value',value)
56+
event:(instance:FormInstance,value:any)=>{
57+
console.log('value',value);
58+
instance.setFieldValue('id',value);
4859
}
4960
}
5061
]}

packages/form-view/src/context/form-context.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export class FormContextScope {
3939
return this.instance.getFormControl(formCode);
4040
}
4141

42+
public getFormInstance(){
43+
return this.instance;
44+
}
45+
4246
public getPresenter() {
4347
return this.presenter;
4448
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {FieldKey, FormEvent} from "@/types";
2+
import {FormInstance} from "@/instance";
23

34
export class EventContext {
45

@@ -8,21 +9,21 @@ export class EventContext {
89
this.events = events;
910
}
1011

11-
public handlerOnChange(target: FieldKey, value: any) {
12+
public handlerOnChange(formInstance:FormInstance,target: FieldKey, value: any) {
1213
const events = this.getEvents('change', target);
1314
if (events && events.length > 0) {
1415
for (const event of events) {
15-
event.event(value);
16+
event.event(formInstance,value);
1617
}
1718
}
1819
}
1920

2021

21-
public handlerOnBlur(target: FieldKey,value:any) {
22+
public handlerOnBlur(formInstance:FormInstance,target: FieldKey,value:any) {
2223
const events = this.getEvents('blur', target);
2324
if (events && events.length > 0) {
2425
for (const event of events) {
25-
event.event(value);
26+
event.event(formInstance,value);
2627
}
2728
}
2829
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,21 @@ export class FormItemFactory {
4040

4141
const eventContext = context.getEventContext();
4242

43+
const instance = context.getFormInstance();
44+
4345
const fieldKey = {
4446
formCode,
4547
fieldCode:formField.code,
4648
}
4749

4850
const handlerOnChange = (value:any)=>{
4951
formItemProps.onChange?.(value);
50-
eventContext.handlerOnChange(fieldKey, value);
52+
eventContext.handlerOnChange(instance,fieldKey, value);
5153
}
5254

5355
const handlerOnBlur = (value:any) => {
5456
formItemProps.onBlur?.(value);
55-
eventContext.handlerOnBlur(fieldKey,value);
57+
eventContext.handlerOnBlur(instance,fieldKey,value);
5658
}
5759

5860
if (FormItem) {

packages/form-view/src/form/sub-view.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export const FormSubView: React.FC<FormSubViewProps> = (props) => {
3131

3232
const formControl = context.getFormControl(formCode);
3333

34+
const formInstance = context.getFormInstance();
35+
3436
const formTarget = formControl?.getProxyTarget();
3537

3638
React.useEffect(() => {
3739
const events = context.getEventContext().getLoadEvents();
3840
if (events && events.length > 0) {
3941
for (const event of events) {
40-
event.event();
42+
event.event(formInstance);
4143
}
4244
}
4345
}, []);

packages/form-view/src/instance/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class FormInstance {
8484
this.getFormControl(formCode)?.setFieldsValue(values);
8585
}
8686

87-
public setFieldValue(name: string, value: any, formCode: string) {
87+
public setFieldValue(name: string, value: any, formCode?: string) {
8888
this.getFormControl(formCode)?.setFieldValue(name, value);
8989
}
9090

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {FieldKey} from "@/types";
2+
import {FormInstance} from "@/instance";
23

34
/**
45
* 表单事件
@@ -12,7 +13,7 @@ export interface FormEvent {
1213
target?:FieldKey;
1314

1415
// 事件触发
15-
event:(value?:any)=>void;
16+
event:(instance:FormInstance,value?:any)=>void;
1617

1718
}
1819

packages/form-view/src/types/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export interface FormFieldValidator {
1010
target: FieldKey;
1111

1212
/** 校验函数 **/
13-
validator: (value: any, instance: FormInstance) => string | true;
13+
validator: (instance: FormInstance,value: any) => string | true;
1414

1515
}

packages/form-view/src/types/view.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ export interface FieldCode{
7070
fieldCode:string;
7171
}
7272

73+
/**
74+
* 布局控制
75+
*/
76+
export interface FormLayout{
77+
78+
}
79+
7380

7481
/**
7582
* 字段唯一标识
7683
*/
7784
export type FieldKey = string|FieldCode;
7885

7986

87+
8088
/**
8189
* 表单视图属性
8290
*/
@@ -93,4 +101,6 @@ export interface FormViewProps {
93101
validators?:FormFieldValidator[];
94102
/** 事件定义 **/
95103
events?: FormEvent[];
104+
/** 布局控制 **/
105+
layout?:FormLayout
96106
}

0 commit comments

Comments
 (0)