Skip to content

Commit 4302756

Browse files
committed
fix(Form): 新增getFieldValue & setFieldValue & resetFieldValue等方法
1 parent f74d8d1 commit 4302756

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const FormDemo = () => {
1616
},
1717
];
1818

19-
const initialValues = {name: '王滴滴'};
19+
const initialValues = {name: '王滴滴', age: ''};
2020

2121
const form = Form.useForm();
2222

@@ -29,10 +29,17 @@ const FormDemo = () => {
2929
type="primary"
3030
onPress={() => {
3131
const values = form.getStore();
32+
const age = form.getFieldValue('age');
3233
console.log('values', values);
3334
}}>
3435
默认按钮
3536
</Button>
37+
<Button type="primary" onPress={() => form.setFieldValue('age', '456')}>
38+
设置
39+
</Button>
40+
<Button type="primary" onPress={() => form.resetFieldValue()}>
41+
重置
42+
</Button>
3643
</Body>
3744
<Footer />
3845
</Layout>

packages/core/src/Form/form.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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 useForm from './hooks/useForm';
65

76
const Form = <
87
FormData extends unknown = any,
@@ -17,9 +16,9 @@ const Form = <
1716

1817
const innerMethods = form.getInnerMethods(true);
1918

20-
if (!isMount.current) {
21-
innerMethods.updateStore({ initialValues: initialValues, store: initialValues });
22-
}
19+
useEffect(() => {
20+
if (!isMount.current) innerMethods.updateStore({ initialValues: initialValues, store: initialValues });
21+
}, []);
2322

2423
useEffect(() => {
2524
isMount.current = true;

packages/core/src/Form/formItems.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React, { FC, useContext, useEffect } from 'react';
2-
import { FormProps, KeyType, FormItemsProps } from './types';
2+
import { KeyType, FormItemsProps } from './types';
33
import { Context } from './hooks/context';
44
import Input from '../Input';
5-
import { View, Text } from 'react-native';
5+
import { View, Text, TextInput } from 'react-native';
66

77
const FormItems: FC<any> = ({ formDatas = [] }) => {
88
const {
9-
innerMethods: { store = {}, updateStore },
9+
innerMethods: { store = {}, updateStore, forceUpdate, validator },
1010
} = useContext(Context);
1111

1212
const change = (field: KeyType, value: any) => updateStore?.({ store: { ...store, [field]: value } });
1313

14-
const validate = async () => {
15-
// showMessages();
16-
// forceUpdate();
14+
const validate = () => {
15+
validator.showMessages();
16+
forceUpdate();
1717
};
1818

1919
const _render = () => {
@@ -27,11 +27,11 @@ const FormItems: FC<any> = ({ formDatas = [] }) => {
2727
return (
2828
<View key={i}>
2929
{_render}
30-
{/* <Text style={{ color: 'red' }}>
31-
{message(v.field, formValues[v.field], {
30+
<Text style={{ color: 'red' }}>
31+
{validator.message(v.field, store[v.field], {
3232
validate: v?.validate,
3333
})}
34-
</Text> */}
34+
</Text>
3535
</View>
3636
);
3737
});

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { KeyType, InnerMethodsReturnType } from '../types';
22
import { useState } from 'react';
3+
import { useValidator } from '@validator.tool/hook';
34

45
type State<FormData = any> = {
56
store: Partial<FormData>;
67
initialValues: Partial<FormData>;
7-
errorMessages: Partial<Record<string, string>>;
88
};
99

1010
export default function useForm<
@@ -14,40 +14,65 @@ export default function useForm<
1414
>(): any {
1515
const [state, setState] = useState<State>({
1616
initialValues: {},
17-
errorMessages: {},
1817
store: {},
1918
});
2019

21-
const updateStore = (datas: any) => {
20+
const { validator, forceUpdate } = useValidator({
21+
initValues: { initialValues: state.initialValues },
22+
});
23+
24+
const updateStore = (datas: Partial<any>) => {
2225
setState({
2326
...state,
2427
...datas,
2528
});
2629
};
2730

28-
const innerUseValidator = () => {};
29-
31+
// 获取表单字段的值
3032
const getFieldValue = (field: FieldKey): FieldValue => {
3133
const { store } = state;
3234
return store[field];
3335
};
3436

37+
// 设置表单字段的值
38+
const setFieldValue = (field: FieldKey, value: FieldValue) => {
39+
updateStore({
40+
store: {
41+
...state.store,
42+
[field]: value,
43+
},
44+
});
45+
};
46+
47+
// 重置表单
48+
const resetFieldValue = () => {
49+
updateStore({
50+
store: { ...state.initialValues },
51+
});
52+
};
53+
3554
const getStore = (): Partial<FormData> => {
3655
const { store } = state;
3756
return store;
3857
};
3958

4059
const getFormInstance = () => {
4160
return {
61+
forceUpdate,
62+
validator,
4263
getStore: getStore,
4364
getFieldValue: getFieldValue,
65+
setFieldValue: setFieldValue,
66+
resetFieldValue,
4467
getInnerMethods: (inner?: boolean): InnerMethodsReturnType<FormData, FieldValue, FieldKey> => {
4568
let methods = {} as any;
4669
if (inner) {
4770
methods = {
4871
store: state.store,
4972
initialValues: state.initialValues,
5073
updateStore: updateStore,
74+
validator,
75+
forceUpdate,
5176
};
5277
}
5378
return methods;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RulesOption } from '@validator.tool/hook';
2+
import Validator from 'validator.tool';
23

34
type KeyType = string | number | symbol;
45

@@ -10,11 +11,15 @@ export type InnerMethodsReturnType<
1011
store: Partial<FormData>;
1112
initialValues: Partial<FormData>;
1213
updateStore: (value: any) => void;
14+
validator: Validator;
15+
forceUpdate: () => void;
1316
};
1417

1518
type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> = {
1619
getStore: () => FormData;
17-
getFieldValue: () => FieldValue;
20+
getFieldValue: (field: FieldKey) => FieldValue;
21+
setFieldValue: (field: FieldKey, value: FieldValue) => void;
22+
resetFieldValue: () => void;
1823
getInnerMethods: (inner?: boolean) => InnerMethodsReturnType<FormData, FieldValue, FieldKey>;
1924
};
2025

0 commit comments

Comments
 (0)