Skip to content

Commit f90f8ec

Browse files
committed
fix(Form): 表单验证部分
1 parent 4302756 commit f90f8ec

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

example/examples/src/routes/Form/index.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
2-
import {Form, Button} from '@uiw/react-native';
2+
import {Form, Button, Toast} from '@uiw/react-native';
33
import Layout, {Container} from '../../Layout';
44
const {Body, Footer} = Layout;
55

66
const FormDemo = () => {
7+
const form = Form.useForm();
78
const items = [
89
{
910
type: 'input',
@@ -13,12 +14,10 @@ const FormDemo = () => {
1314
{
1415
type: 'input',
1516
field: 'age',
17+
validate: (val: any) => (Number(val) > 30 || Number(val) < 10 ? `起输入10-30` : ''),
1618
},
1719
];
18-
19-
const initialValues = {name: '王滴滴', age: ''};
20-
21-
const form = Form.useForm();
20+
const initialValues = {name: '王滴滴', age: '31'};
2221

2322
return (
2423
<Container>
@@ -28,18 +27,27 @@ const FormDemo = () => {
2827
<Button
2928
type="primary"
3029
onPress={() => {
31-
const values = form.getStore();
32-
const age = form.getFieldValue('age');
33-
console.log('values', values);
30+
form
31+
.validateFields()
32+
.then((values: any) => Toast.success(JSON.stringify(values)))
33+
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
3434
}}>
35-
默认按钮
35+
确定
3636
</Button>
3737
<Button type="primary" onPress={() => form.setFieldValue('age', '456')}>
3838
设置
3939
</Button>
4040
<Button type="primary" onPress={() => form.resetFieldValue()}>
4141
重置
4242
</Button>
43+
<Button
44+
type="primary"
45+
onPress={() => {
46+
const errors = form.validate();
47+
Toast.warning(JSON.stringify(errors));
48+
}}>
49+
触发验证
50+
</Button>
4351
</Body>
4452
<Footer />
4553
</Layout>

packages/core/src/Form/form.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect, useRef } from 'react';
22
import FormItems from './formItems';
33
import { Provider } from './hooks/context';
44
import { FormProps, KeyType } from './types';
5+
import { cloneDeep } from './utils';
56

67
const Form = <
78
FormData extends unknown = any,
@@ -17,7 +18,7 @@ const Form = <
1718
const innerMethods = form.getInnerMethods(true);
1819

1920
useEffect(() => {
20-
if (!isMount.current) innerMethods.updateStore({ initialValues: initialValues, store: initialValues });
21+
if (!isMount.current) innerMethods.updateStore({ initialValues: cloneDeep(initialValues), store: initialValues });
2122
}, []);
2223

2324
useEffect(() => {

packages/core/src/Form/hooks/useForm.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { KeyType, InnerMethodsReturnType } from '../types';
22
import { useState } from 'react';
33
import { useValidator } from '@validator.tool/hook';
4+
import { isObjectEmpty } from '../utils/is';
5+
import { cloneDeep } from '../utils';
46

57
type State<FormData = any> = {
68
store: Partial<FormData>;
@@ -51,19 +53,43 @@ export default function useForm<
5153
});
5254
};
5355

56+
// 触发验证
57+
const validate = () => {
58+
const { showMessages, errorMessages } = validator;
59+
showMessages?.();
60+
forceUpdate?.();
61+
if (Object.keys(errorMessages).length > 0) {
62+
return errorMessages;
63+
}
64+
return {};
65+
};
66+
67+
// 验证并获取表单值
68+
const validateFields = () => {
69+
return new Promise(async function (resolve, reject) {
70+
const errors = validate();
71+
if (isObjectEmpty(errors)) {
72+
const value = cloneDeep(state.store);
73+
resolve(value);
74+
} else {
75+
reject(errors);
76+
}
77+
});
78+
};
79+
5480
const getStore = (): Partial<FormData> => {
5581
const { store } = state;
5682
return store;
5783
};
5884

5985
const getFormInstance = () => {
6086
return {
61-
forceUpdate,
62-
validator,
63-
getStore: getStore,
64-
getFieldValue: getFieldValue,
65-
setFieldValue: setFieldValue,
87+
getStore,
88+
getFieldValue,
89+
setFieldValue,
6690
resetFieldValue,
91+
validate,
92+
validateFields,
6793
getInnerMethods: (inner?: boolean): InnerMethodsReturnType<FormData, FieldValue, FieldKey> => {
6894
let methods = {} as any;
6995
if (inner) {

packages/core/src/Form/types/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKe
2020
getFieldValue: (field: FieldKey) => FieldValue;
2121
setFieldValue: (field: FieldKey, value: FieldValue) => void;
2222
resetFieldValue: () => void;
23+
validate: () => Partial<Record<string, string>>;
24+
validateFields: () => Promise<FormData> | any;
2325
getInnerMethods: (inner?: boolean) => InnerMethodsReturnType<FormData, FieldValue, FieldKey>;
2426
};
2527

packages/core/src/Form/utils/index.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,3 @@ export function cloneDeep(value: any) {
1111
}
1212
});
1313
}
14-
15-
export function set<T extends { [key: string]: any }>(target: T, field: PropertyPath, value: any) {
16-
lodashSet(target, field, cloneDeep(value));
17-
return target;
18-
}

packages/core/src/Form/utils/is.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ export function isNumber(obj: any): obj is number {
1919
export function isRegExp(obj: any) {
2020
return opt.call(obj) === '[object RegExp]';
2121
}
22+
23+
export function isObjectEmpty(obj: any) {
24+
for (let _key in obj) {
25+
return false;
26+
}
27+
return true;
28+
}

0 commit comments

Comments
 (0)