Skip to content

Commit 8cf44de

Browse files
committed
docs(Form): Form组件文档
1 parent b3da54b commit 8cf44de

File tree

2 files changed

+189
-2
lines changed

2 files changed

+189
-2
lines changed

packages/core/src/Form/README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
Form 表单
2+
---
3+
4+
集成react-native-uiw表单控件,并支持自定义组件
5+
6+
### 基础示例
7+
8+
<!--DemoStart-->
9+
```jsx
10+
import { SafeAreaView } from 'react-native';
11+
import { Form } from '@uiw/react-native';
12+
13+
const FormDemo = () => {
14+
const form = Form.useForm();
15+
const initialValues = { name: '王滴滴' };
16+
const items = [
17+
{
18+
type: 'input',
19+
field: 'name',
20+
name: '输入框',
21+
}
22+
];
23+
return (
24+
<SafeAreaView>
25+
<Form form={form} formDatas={items} initialValues={initialValues} />
26+
</SafeAreaView>
27+
);
28+
}
29+
```
30+
<!--End-->
31+
32+
### 自定义验证提交
33+
34+
<!--DemoStart-->
35+
```jsx
36+
import { SafeAreaView,Toast } from 'react-native';
37+
import { Form } from '@uiw/react-native';
38+
39+
const FormDemo = () => {
40+
const form = Form.useForm({
41+
changeValidate: true,
42+
});
43+
const initialValues = {name: ''};
44+
const items = [
45+
{
46+
type: 'input',
47+
field: 'name',
48+
name: '输入框',
49+
attr: {},
50+
required: true,
51+
validate: (val) => (!val ? `请输入name` : ''),
52+
}
53+
];
54+
return (
55+
<SafeAreaView>
56+
<Form form={form} formDatas={items} initialValues={initialValues} />
57+
<Button
58+
type="primary"
59+
onPress={() => {
60+
form
61+
.validateFields()
62+
.then((values) => Toast.success(JSON.stringify(values)))
63+
.catch((errors) => Toast.warning(JSON.stringify(errors)));
64+
}}>
65+
确定
66+
</Button>
67+
</SafeAreaView>
68+
);
69+
};
70+
```
71+
<!--End-->
72+
73+
### 自定义表单组件
74+
75+
<!--DemoStart-->
76+
```jsx
77+
import { SafeAreaView,Toast,Slider } from 'react-native';
78+
import { Form } from '@uiw/react-native';
79+
80+
const FormDemo = () => {
81+
const form = Form.useForm({
82+
changeValidate: true,
83+
customComponentList: {
84+
render: <Slider />,
85+
},
86+
});
87+
const initialValues = {name: ''};
88+
const items = [
89+
{
90+
type: 'input',
91+
field: 'name',
92+
name: '输入框',
93+
required: true,
94+
},
95+
{
96+
type: 'render',
97+
field: 'render',
98+
name: '自定义',
99+
},
100+
];
101+
return (
102+
<SafeAreaView>
103+
<Form form={form} formDatas={items} initialValues={initialValues} />
104+
</SafeAreaView>
105+
);
106+
};
107+
```
108+
<!--End-->
109+
110+
### 监听表单变化
111+
112+
<!--DemoStart-->
113+
```jsx
114+
import { SafeAreaView,Toast } from 'react-native';
115+
import { Form } from '@uiw/react-native';
116+
117+
const FormDemo = () => {
118+
const form = Form.useForm({
119+
changeValidate: true,
120+
watch: {
121+
name: (value: unknown) => {
122+
console.log('value', value);
123+
},
124+
}
125+
});
126+
const initialValues = {name: ''};
127+
const items = [
128+
{
129+
type: 'input',
130+
field: 'name',
131+
name: '输入框',
132+
required: true,
133+
}
134+
];
135+
return (
136+
<SafeAreaView>
137+
<Form form={form} formDatas={items} initialValues={initialValues} />
138+
</SafeAreaView>
139+
);
140+
};
141+
```
142+
<!--End-->
143+
144+
### FormProps
145+
```ts
146+
interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> {
147+
/**
148+
* 表单集合
149+
*/
150+
formDatas?: FormItemsProps[];
151+
/**
152+
* useForm表单实例
153+
*/
154+
form: FormInstance<FormData, FieldValue, FieldKey>;
155+
/**
156+
* 表单默认值
157+
*/
158+
initialValues?: Partial<FormData>;
159+
}
160+
```
161+
162+
### FormItemsProps
163+
```ts
164+
interface FormItemsProps {
165+
field: string;
166+
type: string;
167+
name: string;
168+
validate?: RulesOption['validate'];
169+
options?: Array<{ label: string; value: KeyType | any }>;
170+
attr?: any;
171+
required?: boolean;
172+
render?: JSX.Element;
173+
}
174+
```
175+
176+
### FormInstance
177+
```ts
178+
type FormInstance<FormData = any, FieldValue = FormData[keyof FormData], FieldKey extends KeyType = keyof FormData> = {
179+
getFieldValue: (field: FieldKey) => FieldValue;
180+
setFieldValue: (field: FieldKey, value: FieldValue) => void;
181+
resetFieldValue: () => void;
182+
validate: () => Partial<Record<string, string>>;
183+
validateFields: () => Promise<FormData> | any;
184+
getInnerMethods: (inner?: boolean) => InnerMethodsReturnType<FormData>;
185+
};
186+
```
187+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Validator from 'validator.tool';
33

44
type KeyType = string | number | symbol;
55

6-
export type InnerMethodsReturnType<FormData = any> = {
6+
type InnerMethodsReturnType<FormData = any> = {
77
store: Partial<FormData>;
88
initialValues: Partial<FormData>;
99
updateStore: (value: any) => void;
@@ -40,4 +40,4 @@ interface FormItemsProps {
4040
render?: JSX.Element;
4141
}
4242

43-
export type { FormProps, FormItemsProps, KeyType, FormInstance };
43+
export type { FormProps, FormItemsProps, KeyType, FormInstance, InnerMethodsReturnType };

0 commit comments

Comments
 (0)