Skip to content

Commit 43ec537

Browse files
committed
fix(Form): 纠正ts类型
1 parent b0b99fb commit 43ec537

File tree

8 files changed

+96
-92
lines changed

8 files changed

+96
-92
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState} from 'react';
1+
import React, {useState, useRef} from 'react';
22
import {Form, Button, Toast, Slider, Flex} from '@uiw/react-native';
33
import {Text, View} from 'react-native';
44
import Layout, {Container} from '../../Layout';
@@ -13,15 +13,8 @@ interface actionProps {
1313
}
1414

1515
const FormDemo = () => {
16-
const form = Form.useForm({
17-
changeValidate: true,
18-
watch: {
19-
name: (value: unknown) => console.log('value', value),
20-
},
21-
customComponentList: {
22-
render: <Slider />,
23-
},
24-
});
16+
const form = Form.useForm();
17+
const ref = useRef();
2518
const [state, setStore] = useState({});
2619

2720
const schema = [
@@ -170,7 +163,18 @@ const FormDemo = () => {
170163
<Container>
171164
<Layout>
172165
<Body>
173-
<Form form={form} schema={schema} initialValues={{name: '王滴滴', rate: 4}} />
166+
<Form
167+
form={form}
168+
schema={schema}
169+
initialValues={{name: '王滴滴', rate: 4}}
170+
watch={{
171+
name: (value: unknown) => console.log('value', value),
172+
}}
173+
customComponentList={{
174+
render: <Slider />,
175+
}}
176+
changeValidate={true}
177+
/>
174178
<View>
175179
<Text>{JSON.stringify(state)}</Text>
176180
</View>

packages/core/src/Form/README.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ import { SafeAreaView,Toast } from 'react-native';
3737
import { Form } from '@uiw/react-native';
3838

3939
const FormDemo = () => {
40-
const form = Form.useForm({
41-
changeValidate: true,
42-
});
40+
const form = Form.useForm();
4341
const initialValues = {name: ''};
4442
const items = [
4543
{
@@ -78,12 +76,10 @@ import { SafeAreaView,Toast,Slider } from 'react-native';
7876
import { Form } from '@uiw/react-native';
7977

8078
const FormDemo = () => {
81-
const form = Form.useForm({
82-
changeValidate: true,
83-
customComponentList: {
79+
const form = Form.useForm();
80+
const customComponentList = {
8481
render: <Slider />,
85-
},
86-
});
82+
}
8783
const initialValues = {name: ''};
8884
const items = [
8985
{
@@ -100,7 +96,7 @@ const FormDemo = () => {
10096
];
10197
return (
10298
<SafeAreaView>
103-
<Form form={form} schema={items} initialValues={initialValues} />
99+
<Form form={form} schema={items} changeValidate={true} customComponentList={customComponentList} initialValues={initialValues} />
104100
</SafeAreaView>
105101
);
106102
};
@@ -115,14 +111,10 @@ import { SafeAreaView,Toast } from 'react-native';
115111
import { Form } from '@uiw/react-native';
116112

117113
const FormDemo = () => {
118-
const form = Form.useForm({
119-
changeValidate: true,
120-
watch: {
121-
name: (value: unknown) => {
122-
console.log('value', value);
123-
},
124-
}
125-
});
114+
const form = Form.useForm();
115+
const watch = {
116+
name: (value) => console.log('value', value)
117+
}
126118
const initialValues = {name: ''};
127119
const items = [
128120
{
@@ -134,7 +126,7 @@ const FormDemo = () => {
134126
];
135127
return (
136128
<SafeAreaView>
137-
<Form form={form} schema={items} initialValues={initialValues} />
129+
<Form form={form} schema={items} watch={watch} initialValues={initialValues} />
138130
</SafeAreaView>
139131
);
140132
};
@@ -154,9 +146,7 @@ import { SafeAreaView,View,Text } from 'react-native';
154146
import { Form,Button,Flex } from '@uiw/react-native';
155147

156148
const FormDemo = () => {
157-
const form = Form.useForm({
158-
changeValidate: true,
159-
});
149+
const form = Form.useForm();
160150
const initialValues = {name: ''};
161151
const items = [
162152
{
@@ -177,7 +167,7 @@ const FormDemo = () => {
177167
</Flex>
178168
</View>
179169
),
180-
renderAdd: ({add}: {add: () => void}) => (
170+
renderAdd: ({ add }) => (
181171
<View style={{marginTop: 12}}>
182172
<Button onPress={() => add()} type="primary" size="default" bordered={false}>
183173
新增数据
@@ -225,7 +215,13 @@ interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], Field
225215
/**
226216
* 支持默认和卡片两种模式
227217
*/
228-
mode?:'default' | 'card'
218+
mode?:'default' | 'card';
219+
// 表单是否在onChange时进行验证
220+
changeValidate?: boolean;
221+
// 监听表单字段变化
222+
watch?: Partial<Record<string, (value: unknown) => void>>;
223+
// 自定义组件
224+
customComponentList?: Partial<Record<string, JSX.Element>>;
229225
}
230226
```
231227

@@ -240,7 +236,7 @@ interface FormItemsProps {
240236
name: string;
241237
// 验证规则
242238
validate?: RulesOption['validate'];
243-
options?: Array<{ label: string; value: KeyType | any }>;
239+
options?: Array<{ label: string; value: KeyType }>;
244240
// 表单控件props
245241
attr?: any;
246242
// 展示是否必填
@@ -283,3 +279,5 @@ type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKe
283279
};
284280
```
285281

282+
283+

packages/core/src/Form/comps/container.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Card from '../../Card';
33
import { SafeAreaView } from 'react-native';
44
import styles from '../styles';
55

6-
const Container = ({ mode, children }: { mode: 'card' | 'default'; children?: JSX.Element }) => {
6+
const Container = ({ mode, children }: { mode: 'card' | 'default'; children?: React.ReactNode }) => {
77
return mode === 'card' ? <Card>{children}</Card> : <SafeAreaView style={styles.warpper}>{children}</SafeAreaView>;
88
};
99

packages/core/src/Form/form.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,35 @@ import FormItems from './formItems';
33
import { Provider } from './hooks/context';
44
import { FormProps, KeyType } from './types';
55
import { cloneDeep } from './utils';
6+
import TextArea from '../TextArea';
7+
import Slider from '../Slider';
8+
import SearchBar from '../SearchBar';
9+
import Stepper from '../Stepper';
10+
import Input from './comps/input';
11+
import Rating from './comps/rate';
12+
import Radio from './comps/radio';
13+
import Switch from './comps/switch';
14+
import CheckBox from './comps/checkBox';
615

7-
const Form = <
8-
FormData extends unknown = any,
9-
FieldValue = FormData[keyof FormData],
10-
FieldKey extends KeyType = keyof FormData,
11-
>(
12-
baseProps: FormProps,
13-
) => {
14-
const { schema, form, initialValues = {}, mode = 'default' } = baseProps;
16+
const Form = (baseProps: FormProps) => {
17+
const {
18+
schema,
19+
form,
20+
initialValues = {},
21+
mode = 'default',
22+
watch,
23+
customComponentList = {},
24+
changeValidate = false,
25+
} = baseProps;
1526

1627
const isMount = useRef<boolean>();
1728

1829
const innerMethods = form.getInnerMethods(true);
1930

2031
useEffect(() => {
21-
if (!isMount.current) innerMethods.updateStore({ initialValues: cloneDeep(initialValues), store: initialValues });
32+
if (!isMount.current) {
33+
innerMethods.updateStore({ initialValues: cloneDeep(initialValues), store: initialValues });
34+
}
2235
}, []);
2336

2437
useEffect(() => {
@@ -28,6 +41,20 @@ const Form = <
2841
const contextProps = {
2942
innerMethods: innerMethods,
3043
mode: mode,
44+
watch: watch,
45+
customComponentList: {
46+
...customComponentList,
47+
input: <Input />,
48+
textArea: <TextArea />,
49+
slider: <Slider />,
50+
rate: <Rating disabled={false} />,
51+
radio: <Radio />,
52+
search: <SearchBar />,
53+
switch: <Switch />,
54+
checkBox: <CheckBox />,
55+
stepper: <Stepper value={0} onChange={() => {}} />,
56+
},
57+
changeValidate: changeValidate,
3158
};
3259

3360
return (

packages/core/src/Form/formItems.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { FC, useContext } from 'react';
2-
import { KeyType, FormItemsProps } from './types';
1+
import React, { useContext } from 'react';
2+
import { KeyType, FormItemsProps, FormProps } from './types';
33
import { isObjectEmpty } from './utils/is';
44
import { Context } from './hooks/context';
55
import Label from './comps/label';
@@ -9,10 +9,13 @@ import Container from './comps/container';
99
import { View } from 'react-native';
1010
import styles from './styles';
1111

12-
const FormItems: FC<any> = ({ schema = [] }) => {
12+
const FormItems = ({ schema = [] }: Pick<FormProps, 'schema'>) => {
1313
const {
1414
mode,
15-
innerMethods: { store = {}, updateStore, innerValidate, watch, customComponentList, initialValues },
15+
innerMethods: { store = {}, updateStore, innerValidate },
16+
watch,
17+
customComponentList,
18+
changeValidate,
1619
} = useContext(Context);
1720

1821
const change = (field: KeyType, value: unknown) => {
@@ -33,15 +36,15 @@ const FormItems: FC<any> = ({ schema = [] }) => {
3336
value: store[v.field],
3437
onChange: (value: unknown) => {
3538
change(v.field, value);
36-
innerValidate();
39+
if (changeValidate) innerValidate();
3740
},
3841
})
3942
: null;
4043
}
4144
return null;
4245
};
4346

44-
const _render = (): JSX.Element => {
47+
const _render = () => {
4548
return schema.map((v: FormItemsProps, i: number) => {
4649
const last = schema.length - 1 === i;
4750
if (v.hide) {

packages/core/src/Form/formList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const FormList = ({
2020
},
2121
}: FormListProps) => {
2222
const {
23-
innerMethods: { store = {}, updateStore, customComponentList },
23+
innerMethods: { store = {}, updateStore },
24+
customComponentList,
2425
} = useContext(Context);
2526

2627
const { field, items = [], renderAdd, renderHeader } = formListValue;

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

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import React from 'react';
1+
import { useRef } from 'react';
22
import { KeyType, InnerMethodsReturnType, FormInstance } from '../types';
33
import { useState } from 'react';
44
import { useValidator } from '@validator.tool/hook';
55
import { isObjectEmpty } from '../utils/is';
66
import { cloneDeep } from '../utils';
7-
import TextArea from '../../TextArea';
8-
import Slider from '../../Slider';
9-
import SearchBar from '../../SearchBar';
10-
import Stepper from '../../Stepper';
11-
import Input from '../comps/input';
12-
import Rating from '../comps/rate';
13-
import Radio from '../comps/radio';
14-
import Switch from '../comps/switch';
15-
import CheckBox from '../comps/checkBox';
167

178
type State<FormData = any> = {
189
store: Partial<FormData>;
@@ -23,15 +14,7 @@ export default function useForm<
2314
FormData = any,
2415
FieldValue = FormData[keyof FormData],
2516
FieldKey extends KeyType = keyof FormData,
26-
>({
27-
changeValidate = false,
28-
watch = {},
29-
customComponentList,
30-
}: {
31-
changeValidate?: boolean;
32-
watch?: Partial<Record<string, (value: unknown) => void>>;
33-
customComponentList?: Partial<Record<string, unknown>>;
34-
}): FormInstance<FormData, FieldValue, FieldKey> {
17+
>(): FormInstance<FormData, FieldValue, FieldKey> {
3518
const [state, setState] = useState<State>({
3619
initialValues: {},
3720
store: {},
@@ -50,10 +33,8 @@ export default function useForm<
5033

5134
const innerValidate = () => {
5235
const { showMessages } = validator;
53-
if (changeValidate) {
54-
showMessages?.();
55-
forceUpdate?.();
56-
}
36+
showMessages?.();
37+
forceUpdate?.();
5738
};
5839

5940
// 获取表单字段的值
@@ -111,7 +92,7 @@ export default function useForm<
11192
validate,
11293
validateFields,
11394
getInnerMethods: (inner?: boolean): InnerMethodsReturnType<FormData> => {
114-
let methods = {} as any;
95+
let methods = {} as InnerMethodsReturnType<FormData>;
11596
if (inner) {
11697
methods = {
11798
store: state.store,
@@ -120,19 +101,6 @@ export default function useForm<
120101
validator,
121102
forceUpdate,
122103
innerValidate,
123-
watch,
124-
customComponentList: {
125-
...customComponentList,
126-
input: <Input />,
127-
textArea: <TextArea />,
128-
slider: <Slider />,
129-
rate: <Rating disabled={false} />,
130-
radio: <Radio />,
131-
search: <SearchBar />,
132-
switch: <Switch />,
133-
checkBox: <CheckBox />,
134-
stepper: <Stepper value={0} onChange={() => {}} />,
135-
},
136104
};
137105
}
138106
return methods;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type InnerMethodsReturnType<FormData = any> = {
1010
validator: Validator;
1111
forceUpdate: () => void;
1212
innerValidate: () => void;
13-
watch: Partial<Record<string, (value: unknown) => void>>;
14-
customComponentList: Partial<Record<string, JSX.Element>>;
13+
// watch: Partial<Record<string, (value: unknown) => void>>;
14+
// customComponentList: Partial<Record<string, JSX.Element>>;
1515
};
1616

1717
type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> = {
@@ -31,6 +31,9 @@ interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], Field
3131
* 支持默认和卡片两种模式
3232
*/
3333
mode?: 'default' | 'card';
34+
watch?: Partial<Record<string, (value: unknown) => void>>;
35+
customComponentList?: Partial<Record<string, JSX.Element>>;
36+
changeValidate?: boolean;
3437
}
3538

3639
interface actionProps {
@@ -46,7 +49,7 @@ interface FormItemsProps {
4649
type: string;
4750
name: string;
4851
validate?: RulesOption['validate'];
49-
options?: Array<{ label: string; value: KeyType | any }>;
52+
options?: Array<{ label: string; value: KeyType }>;
5053
attr?: any;
5154
required?: boolean;
5255
renderHeader?: (index: number, { remove, moveUp, moveDown }: actionProps) => React.ReactNode;

0 commit comments

Comments
 (0)