Skip to content

Commit 7bb0605

Browse files
author
hy
committed
fix(Form) 优化Form组件和组件文档
1 parent b193f63 commit 7bb0605

File tree

3 files changed

+151
-6
lines changed

3 files changed

+151
-6
lines changed

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

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {useState, useRef} from 'react';
2-
import {Form, Button, Toast, Slider, Flex, Text} from '@uiw/react-native';
2+
import {Form, Button, Toast, Slider, Flex, Text, Spacing} from '@uiw/react-native';
33
import {View} from 'react-native';
44
import Layout, {Container} from '../../Layout';
55
const {Body, Footer, Card} = Layout;
@@ -18,6 +18,42 @@ const FormDemo = () => {
1818
const [state, setStore] = useState({});
1919

2020
const schema = [
21+
{
22+
type: 'input',
23+
field: 'name',
24+
name: '姓名',
25+
attr: {},
26+
required: true,
27+
validate: (val: any) => (!val ? `请输入name` : ''),
28+
},
29+
{
30+
type: 'input',
31+
field: 'phone',
32+
name: '电话',
33+
attr: {},
34+
required: true,
35+
validate: (val: any) => (!val ? `请输入电话` : ''),
36+
},
37+
{
38+
type: 'radio',
39+
field: 'sex',
40+
name: '单选框',
41+
options: [
42+
{label: '四川菜', value: 1},
43+
{label: '湖北菜', value: 2},
44+
{label: '西北菜', value: 3},
45+
{label: '新疆菜', value: 4},
46+
{label: '东北菜', value: 5},
47+
{label: '四川菜', value: 6},
48+
{label: '湖北菜', value: 7},
49+
{label: '西北菜', value: 8},
50+
{label: '新疆菜', value: 9},
51+
{label: '东北菜', value: 10},
52+
],
53+
},
54+
];
55+
56+
const schemaCard = [
2157
{
2258
type: 'input',
2359
field: 'name',
@@ -60,6 +96,58 @@ const FormDemo = () => {
6096
{label: '东北菜', value: 10},
6197
],
6298
},
99+
];
100+
const schemaType = [
101+
{
102+
type: 'input',
103+
field: 'name',
104+
name: '姓名',
105+
attr: {},
106+
required: true,
107+
validate: (val: any) => (!val ? `请输入name` : ''),
108+
},
109+
{
110+
type: 'input',
111+
field: 'phone',
112+
name: '电话',
113+
attr: {},
114+
required: true,
115+
validate: (val: any) => (!val ? `请输入电话` : ''),
116+
},
117+
{
118+
type: 'radio',
119+
field: 'sex',
120+
name: '单选框',
121+
options: [
122+
{label: '四川菜', value: 1},
123+
{label: '湖北菜', value: 2},
124+
{label: '西北菜', value: 3},
125+
{label: '新疆菜', value: 4},
126+
{label: '东北菜', value: 5},
127+
{label: '四川菜', value: 6},
128+
{label: '湖北菜', value: 7},
129+
{label: '西北菜', value: 8},
130+
{label: '新疆菜', value: 9},
131+
{label: '东北菜', value: 10},
132+
],
133+
},
134+
{
135+
type: 'checkBox',
136+
field: 'fruit',
137+
name: '多选框',
138+
options: [
139+
{label: '香蕉', value: 1},
140+
{label: '西瓜', value: 2},
141+
{label: '猕猴桃', value: 3},
142+
{label: '新疆菜', value: 4},
143+
{label: '东北菜', value: 5},
144+
{label: '四川菜', value: 6},
145+
{label: '湖北菜', value: 7},
146+
{label: '西北菜', value: 8},
147+
{label: '新疆菜', value: 9},
148+
{label: '东北菜', value: 10},
149+
],
150+
},
63151
{
64152
type: 'rate',
65153
field: 'rate',
@@ -232,7 +320,57 @@ const FormDemo = () => {
232320
<Form
233321
form={form}
234322
schema={schema}
235-
initialValues={{name: '王滴滴', rate: 4, datePicker: new Date(), picker: ['2']}}
323+
initialValues={{name: '王滴滴'}}
324+
watch={{
325+
name: (value: unknown) => console.log('value', value),
326+
}}
327+
customComponentList={{
328+
render: <Slider />,
329+
}}
330+
changeValidate={true}
331+
/>
332+
<Button
333+
type="primary"
334+
onPress={() => {
335+
form
336+
.validateFields()
337+
.then((values: any) => setStore(values))
338+
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
339+
}}>
340+
确定
341+
</Button>
342+
</Card>
343+
<Card title="卡片模式">
344+
<Form
345+
form={form}
346+
schema={schemaCard}
347+
mode="card"
348+
initialValues={{name: '王滴滴', rate: 4}}
349+
watch={{
350+
name: (value: unknown) => console.log('value', value),
351+
}}
352+
customComponentList={{
353+
render: <Slider />,
354+
}}
355+
changeValidate={true}
356+
/>
357+
<Spacing />
358+
<Button
359+
type="primary"
360+
onPress={() => {
361+
form
362+
.validateFields()
363+
.then((values: any) => setStore(values))
364+
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
365+
}}>
366+
确定
367+
</Button>
368+
</Card>
369+
<Card title="表单项类型">
370+
<Form
371+
form={form}
372+
schema={schemaType}
373+
initialValues={{name: '王滴滴', rate: 4}}
236374
watch={{
237375
name: (value: unknown) => console.log('value', value),
238376
}}
@@ -241,9 +379,11 @@ const FormDemo = () => {
241379
}}
242380
changeValidate={true}
243381
/>
382+
<Spacing />
244383
<View>
245384
<Text color="primary_text">{JSON.stringify(state)}</Text>
246385
</View>
386+
<Spacing />
247387
<Button
248388
type="primary"
249389
onPress={() => {
@@ -254,12 +394,15 @@ const FormDemo = () => {
254394
}}>
255395
确定
256396
</Button>
397+
<Spacing />
257398
<Button type="primary" onPress={() => form.setFieldValue('age', '456')}>
258399
设置
259400
</Button>
401+
<Spacing />
260402
<Button type="primary" onPress={() => form.resetFieldValue()}>
261403
重置
262404
</Button>
405+
<Spacing />
263406
<Button
264407
type="primary"
265408
onPress={() => {

packages/core/src/Card/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ function createStyles({ backgroundColor = '#fff', shadowColor = '#999', selectBo
108108
return StyleSheet.create({
109109
container: {
110110
backgroundColor: backgroundColor,
111-
padding: 15,
112-
margin: 15,
111+
padding: 10,
112+
// margin: 10,
113113
marginBottom: 0,
114114
...Platform.select({
115115
android: {

packages/core/src/Form/form.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import FormItems from './formItems';
33
import { Provider } from './hooks/context';
44
import { FormProps } from './types';
55
import { cloneDeep } from './utils';
6+
import { useTheme } from '@shopify/restyle';
7+
import { Theme } from '../theme';
68
import TextArea from '../TextArea';
79
import Slider from '../Slider';
810
import SearchBar from '../SearchBar';
@@ -30,7 +32,7 @@ const Form = (baseProps: FormProps) => {
3032
changeValidate = false,
3133
children,
3234
} = baseProps;
33-
35+
const theme = useTheme<Theme>();
3436
const isMount = useRef<boolean>();
3537

3638
const innerMethods = form.getInnerMethods(true);
@@ -64,7 +66,7 @@ const Form = (baseProps: FormProps) => {
6466
picker: <Picker />,
6567
datePicker: <DatePicker />,
6668
datePeriodInput: <DatePeriodInput />,
67-
verificationCode: <VerificationCode outerStyle={{ backgroundColor: '#FFF' }} />,
69+
verificationCode: <VerificationCode outerStyle={{ backgroundColor: theme.colors.mask }} />,
6870
},
6971
changeValidate: changeValidate,
7072
};

0 commit comments

Comments
 (0)