Skip to content

Commit c6a2387

Browse files
committed
add redux
1 parent d11c9df commit c6a2387

21 files changed

Lines changed: 322 additions & 83 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {useFormRegister} from "@coding-form/form-view";
1+
import {registerFormItems} from "@coding-form/form-view";
22
import {FormString} from "@/components/string";
33
import {Form} from "antd-mobile";
44

55
export const registerForms = () => {
66

7-
useFormRegister(Form,[
7+
registerFormItems(Form,[
88
{
99
type: 'string',
1010
componentType:FormString

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const HomePage = () => {
3131
form={form}
3232
validators={[
3333
{
34-
code:'name',
34+
fieldCode:'name',
3535
validator:(value,instance)=>{
3636
if(value){
3737
return true;
@@ -66,15 +66,27 @@ const HomePage = () => {
6666

6767
<Button
6868
onClick={() => {
69-
form.hiddenFields(['name']);
69+
form.hiddenFields(true,['name']);
7070
}}
71-
>hidden</Button>
71+
>enable hidden</Button>
7272

7373
<Button
7474
onClick={() => {
75-
form.showFields(['name']);
75+
form.hiddenFields(false,['name']);
7676
}}
77-
>show</Button>
77+
>disable hidden</Button>
78+
79+
<Button
80+
onClick={() => {
81+
form.requiredFields(true,['name']);
82+
}}
83+
>enable required</Button>
84+
85+
<Button
86+
onClick={() => {
87+
form.requiredFields(false,['name']);
88+
}}
89+
>disable required</Button>
7890
</Space>
7991
</div>
8092
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {useFormRegister} from "@coding-form/form-view";
1+
import {registerFormItems} from "@coding-form/form-view";
22
import {FormString} from "@/components/string";
33
import {Form} from "antd";
44

55
export const registerForms = () => {
66

7-
useFormRegister(Form,[
7+
registerFormItems(Form,[
88
{
99
type: 'string',
1010
componentType:FormString

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const HomePage = () => {
3030
form={form}
3131
validators={[
3232
{
33-
code:'name',
33+
fieldCode:'name',
3434
validator:(value,instance)=>{
3535
if(value){
3636
return true;
@@ -65,15 +65,27 @@ const HomePage = () => {
6565

6666
<Button
6767
onClick={() => {
68-
form.hiddenFields(['name']);
68+
form.hiddenFields(true,['name']);
6969
}}
70-
>hidden</Button>
70+
>enable hidden</Button>
7171

7272
<Button
7373
onClick={() => {
74-
form.showFields(['name']);
74+
form.hiddenFields(false,['name']);
7575
}}
76-
>show</Button>
76+
>disable hidden</Button>
77+
78+
<Button
79+
onClick={() => {
80+
form.requiredFields(true,['name']);
81+
}}
82+
>enable required</Button>
83+
84+
<Button
85+
onClick={() => {
86+
form.requiredFields(false,['name']);
87+
}}
88+
>disable required</Button>
7789
</Space>
7890
</div>
7991
);
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
export type NamePath = string | number | boolean | (string | number | boolean)[];
22

3-
3+
/** 表单实例对象 **/
44
export interface FormInstance {
5+
/** 获取表单字段 **/
56
getFieldValue: (name: NamePath) => any;
7+
/** 获取表单值 **/
68
getFieldsValue: () => any;
9+
/** 重新赋值 **/
710
resetFields: (nameList?: NamePath[]) => void;
11+
/** 设置表单值 **/
812
setFieldsValue: (values: any) => void;
13+
/** 设置表单值 **/
914
setFieldValue: (name: NamePath, value: any) => void;
15+
/** 提交表单 **/
1016
submit: () => void;
17+
/** 校验字段 **/
1118
validateFields(nameList?: NamePath[]): Promise<any>;
12-
hiddenFields: (nameList?: NamePath[]) => void;
13-
showFields: (nameList?: NamePath[]) => void;
19+
/** 隐藏字段 **/
20+
hiddenFields: (hidden:boolean,nameList?: NamePath[]) => void;
21+
/** 必填字段 **/
22+
requiredFields: (required:boolean,nameList?: NamePath[]) => void;
23+
1424
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import {FormInstance} from "@/form/instance";
22

33

4+
/**
5+
* 字段校验器
6+
*/
47
export interface FormFieldValidator {
58

6-
code: string;
9+
/** 表单名称 **/
10+
formCode?: string;
711

8-
validator: ( value: any, instance: FormInstance) => string | true;
12+
/** 字段名 **/
13+
fieldCode: string;
14+
15+
/** 校验函数 **/
16+
validator: (value: any, instance: FormInstance) => string | true;
917

1018
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ export interface FormMeta {
5656
// 表单字段
5757
fields: FormField[];
5858
// 子表单
59-
subForms: FormMeta[];
59+
subForms?: FormMeta[];
6060
}
6161

6262

6363
/**
6464
* 表单视图属性
6565
*/
6666
export interface FormViewProps {
67+
/** 表单定义 **/
6768
meta: FormMeta;
6869
/** 表单操控对象 */
6970
form?: FormInstance;

packages/form-view/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"dev": "rslib build --watch"
1818
},
1919
"dependencies": {
20-
"@coding-form/form-types": "workspace:*"
20+
"@coding-form/form-types": "workspace:*",
21+
"@reduxjs/toolkit": "^2.11.2",
22+
"react-redux": "^9.2.0"
2123
},
2224
"peerDependencies": {
2325
"react": ">=18",
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
import React from "react";
22
import {FormInstance} from "@/instance";
33
import {FormValidate} from "@/validate";
4+
import {FormPresenter} from "@/presenters";
5+
import {FormState} from "@/types";
46

57

6-
export interface FormContextScope{
8+
export class FormContextScope {
79

8-
instance:FormInstance;
10+
private readonly instance: FormInstance;
11+
private readonly validate: FormValidate;
12+
private readonly presenter: FormPresenter;
913

10-
validate:FormValidate;
14+
constructor(instance: FormInstance,validate: FormValidate, presenter: FormPresenter) {
15+
this.instance = instance;
16+
this.presenter = presenter;
17+
this.validate = validate;
18+
}
1119

20+
public initialState(){
21+
this.presenter.initialState();
22+
}
23+
24+
public syncState(state: FormState){
25+
this.presenter.syncState(state);
26+
}
27+
28+
public getInstance(){
29+
return this.instance;
30+
}
31+
32+
public getPresenter(){
33+
return this.presenter;
34+
}
35+
36+
public getValidate(){
37+
return this.validate;
38+
}
1239
}
1340

1441

15-
export const FormContext = React.createContext<FormContextScope|undefined>(undefined);
42+
export const FormContext = React.createContext<FormContextScope | undefined>(undefined);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from "react";
22
import {FormItemProps} from "@/types/item";
33

4-
export class FormFactory {
4+
export class FormItemFactory {
55

66
private readonly cache: Map<string, React.ComponentType<FormItemProps>>;
77

88
private constructor() {
99
this.cache = new Map();
1010
}
1111

12-
private static readonly instance = new FormFactory();
12+
private static readonly instance = new FormItemFactory();
1313

1414
public static getInstance() {
1515
return this.instance;

0 commit comments

Comments
 (0)