Skip to content

Commit f74d8d1

Browse files
committed
fix(Form): useForm优化
1 parent bbc8325 commit f74d8d1

File tree

6 files changed

+79
-105
lines changed

6 files changed

+79
-105
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const FormDemo = () => {
1818

1919
const initialValues = {name: '王滴滴'};
2020

21-
const [form] = Form.useForm();
21+
const form = Form.useForm();
2222

2323
return (
2424
<Container>

packages/core/src/Form/form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ const Form = <
1414
const { formDatas, form, initialValues = {} } = baseProps;
1515

1616
const isMount = useRef<boolean>();
17-
const [formInstance] = useForm<FormData, FieldValue, FieldKey>(form);
18-
const innerMethods = formInstance.getInnerMethods(true);
17+
18+
const innerMethods = form.getInnerMethods(true);
1919

2020
if (!isMount.current) {
21-
innerMethods.innerSetInitialValues(initialValues);
21+
innerMethods.updateStore({ initialValues: initialValues, store: initialValues });
2222
}
2323

2424
useEffect(() => {

packages/core/src/Form/formItems.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
1-
import React, { FC, useContext } from 'react';
1+
import React, { FC, useContext, useEffect } from 'react';
22
import { FormProps, KeyType, FormItemsProps } from './types';
33
import { Context } from './hooks/context';
44
import Input from '../Input';
55
import { View, Text } from 'react-native';
6-
import { useValidator } from '@validator.tool/hook';
76

8-
const FormItems: FC<FormProps> = ({ formDatas = [] }) => {
9-
const { innerMethods } = useContext(Context);
7+
const FormItems: FC<any> = ({ formDatas = [] }) => {
8+
const {
9+
innerMethods: { store = {}, updateStore },
10+
} = useContext(Context);
1011

11-
const formValues = innerMethods.innerGetStore();
12+
const change = (field: KeyType, value: any) => updateStore?.({ store: { ...store, [field]: value } });
1213

13-
const { validator, forceUpdate } = useValidator({
14-
initValues: { ...formValues },
15-
});
16-
17-
const change = (field: KeyType, value: any) => innerMethods?.innerUpdateStore(field, value);
14+
const validate = async () => {
15+
// showMessages();
16+
// forceUpdate();
17+
};
1818

1919
const _render = () => {
2020
return formDatas.map((v: FormItemsProps, i: number) => {
2121
let _render;
2222
if (v.type === 'input') {
2323
_render = (
24-
<Input
25-
value={formValues[v.field]}
26-
onChangeText={(value) => change(v.field, value)}
27-
onBlur={() => {
28-
validator.showMessages();
29-
forceUpdate();
30-
console.log('validator', validator);
31-
}}
32-
/>
24+
<Input value={store[v.field]} onChangeText={(value) => change(v.field, value)} onBlur={() => validate()} />
3325
);
3426
}
3527
return (
3628
<View key={i}>
3729
{_render}
38-
<Text style={{ color: 'red' }}>
39-
{validator.message(v.field, formValues[v.field], {
30+
{/* <Text style={{ color: 'red' }}>
31+
{message(v.field, formValues[v.field], {
4032
validate: v?.validate,
4133
})}
42-
</Text>
34+
</Text> */}
4335
</View>
4436
);
4537
});

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
1-
import { useRef } from 'react';
2-
import { KeyType, InnerMethodsReturnType, FormInstance } from '../types';
3-
import Store from './store';
1+
import { KeyType, InnerMethodsReturnType } from '../types';
2+
import { useState } from 'react';
43

5-
export function getFormInstance<
6-
FormData = any,
7-
FieldValue = FormData[keyof FormData],
8-
FieldKey extends KeyType = keyof FormData,
9-
>() {
10-
const store = new Store<FormData, FieldValue, FieldKey>();
11-
return {
12-
getStore: store.getStore,
13-
getFieldValue: store.getFieldValue,
14-
getInnerMethods: (inner?: boolean): InnerMethodsReturnType<FormData, FieldValue, FieldKey> => {
15-
let methods = {} as InnerMethodsReturnType<FormData, FieldValue, FieldKey>;
16-
if (inner) {
17-
methods = {
18-
innerSetInitialValues: store.innerSetInitialValues,
19-
innerGetStore: store.innerGetStore,
20-
innerUpdateStore: store.innerUpdateStore,
21-
};
22-
}
23-
return methods;
24-
},
25-
};
26-
}
4+
type State<FormData = any> = {
5+
store: Partial<FormData>;
6+
initialValues: Partial<FormData>;
7+
errorMessages: Partial<Record<string, string>>;
8+
};
279

2810
export default function useForm<
2911
FormData = any,
3012
FieldValue = FormData[keyof FormData],
3113
FieldKey extends KeyType = keyof FormData,
32-
>(form?: FormInstance<FormData, FieldValue, FieldKey>): [FormInstance<FormData, FieldValue, FieldKey>] {
33-
const formRef = useRef(form);
34-
35-
if (!formRef.current) {
36-
if (form) {
37-
formRef.current = form;
38-
} else {
39-
formRef.current = getFormInstance<FormData, FieldValue, FieldKey>();
40-
}
41-
}
42-
return [formRef.current];
14+
>(): any {
15+
const [state, setState] = useState<State>({
16+
initialValues: {},
17+
errorMessages: {},
18+
store: {},
19+
});
20+
21+
const updateStore = (datas: any) => {
22+
setState({
23+
...state,
24+
...datas,
25+
});
26+
};
27+
28+
const innerUseValidator = () => {};
29+
30+
const getFieldValue = (field: FieldKey): FieldValue => {
31+
const { store } = state;
32+
return store[field];
33+
};
34+
35+
const getStore = (): Partial<FormData> => {
36+
const { store } = state;
37+
return store;
38+
};
39+
40+
const getFormInstance = () => {
41+
return {
42+
getStore: getStore,
43+
getFieldValue: getFieldValue,
44+
getInnerMethods: (inner?: boolean): InnerMethodsReturnType<FormData, FieldValue, FieldKey> => {
45+
let methods = {} as any;
46+
if (inner) {
47+
methods = {
48+
store: state.store,
49+
initialValues: state.initialValues,
50+
updateStore: updateStore,
51+
};
52+
}
53+
return methods;
54+
},
55+
};
56+
};
57+
58+
return getFormInstance();
4359
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Store from '../hooks/store';
21
import { RulesOption } from '@validator.tool/hook';
32

43
type KeyType = string | number | symbol;
@@ -7,19 +6,21 @@ export type InnerMethodsReturnType<
76
FormData = any,
87
FieldValue = FormData[keyof FormData],
98
FieldKey extends KeyType = keyof FormData,
10-
> = Pick<Store<FormData, FieldValue, FieldKey>, 'innerSetInitialValues' | 'innerGetStore' | 'innerUpdateStore'>;
9+
> = {
10+
store: Partial<FormData>;
11+
initialValues: Partial<FormData>;
12+
updateStore: (value: any) => void;
13+
};
1114

12-
type FormInstance<
13-
FormData = any,
14-
FieldValue = FormData[keyof FormData],
15-
FieldKey extends KeyType = keyof FormData,
16-
> = Pick<Store<FormData, FieldValue, FieldKey>, 'getStore' | 'getFieldValue'> & {
15+
type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> = {
16+
getStore: () => FormData;
17+
getFieldValue: () => FieldValue;
1718
getInnerMethods: (inner?: boolean) => InnerMethodsReturnType<FormData, FieldValue, FieldKey>;
1819
};
1920

2021
interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> {
2122
formDatas?: FormItemsProps[];
22-
form?: FormInstance<FormData, FieldValue, FieldKey>;
23+
form: FormInstance<FormData, FieldValue, FieldKey>;
2324
initialValues?: Partial<FormData>;
2425
}
2526

0 commit comments

Comments
 (0)