Skip to content

Commit cbe56fb

Browse files
authored
Merge pull request #606 from hy916/dev
fix(Form) 优化Form组件和组件文档
2 parents 3bc6a6d + a0d0a98 commit cbe56fb

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',
@@ -275,7 +363,57 @@ const FormDemo = () => {
275363
<Form
276364
form={form}
277365
schema={schema}
278-
initialValues={{name: '王滴滴', rate: 4, datePicker: new Date(), picker: ['2']}}
366+
initialValues={{name: '王滴滴'}}
367+
watch={{
368+
name: (value: unknown) => console.log('value', value),
369+
}}
370+
customComponentList={{
371+
render: <Slider />,
372+
}}
373+
changeValidate={true}
374+
/>
375+
<Button
376+
type="primary"
377+
onPress={() => {
378+
form
379+
.validateFields()
380+
.then((values: any) => setStore(values))
381+
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
382+
}}>
383+
确定
384+
</Button>
385+
</Card>
386+
<Card title="卡片模式">
387+
<Form
388+
form={form}
389+
schema={schemaCard}
390+
mode="card"
391+
initialValues={{name: '王滴滴', rate: 4}}
392+
watch={{
393+
name: (value: unknown) => console.log('value', value),
394+
}}
395+
customComponentList={{
396+
render: <Slider />,
397+
}}
398+
changeValidate={true}
399+
/>
400+
<Spacing />
401+
<Button
402+
type="primary"
403+
onPress={() => {
404+
form
405+
.validateFields()
406+
.then((values: any) => setStore(values))
407+
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
408+
}}>
409+
确定
410+
</Button>
411+
</Card>
412+
<Card title="表单项类型">
413+
<Form
414+
form={form}
415+
schema={schemaType}
416+
initialValues={{name: '王滴滴', rate: 4}}
279417
watch={{
280418
name: (value: unknown) => console.log('value', value),
281419
}}
@@ -284,9 +422,11 @@ const FormDemo = () => {
284422
}}
285423
changeValidate={true}
286424
/>
425+
<Spacing />
287426
<View>
288427
<Text color="primary_text">{JSON.stringify(state)}</Text>
289428
</View>
429+
<Spacing />
290430
<Button
291431
type="primary"
292432
onPress={() => {
@@ -297,12 +437,15 @@ const FormDemo = () => {
297437
}}>
298438
确定
299439
</Button>
440+
<Spacing />
300441
<Button type="primary" onPress={() => form.setFieldValue('age', '456')}>
301442
设置
302443
</Button>
444+
<Spacing />
303445
<Button type="primary" onPress={() => form.resetFieldValue()}>
304446
重置
305447
</Button>
448+
<Spacing />
306449
<Button
307450
type="primary"
308451
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';
@@ -31,7 +33,7 @@ const Form = (baseProps: FormProps) => {
3133
changeValidate = false,
3234
children,
3335
} = baseProps;
34-
36+
const theme = useTheme<Theme>();
3537
const isMount = useRef<boolean>();
3638

3739
const innerMethods = form.getInnerMethods(true);
@@ -65,7 +67,7 @@ const Form = (baseProps: FormProps) => {
6567
picker: <Picker />,
6668
datePicker: <DatePicker />,
6769
datePeriodInput: <DatePeriodInput />,
68-
verificationCode: <VerificationCode outerStyle={{ backgroundColor: '#FFF' }} />,
70+
verificationCode: <VerificationCode outerStyle={{ backgroundColor: theme.colors.mask }} />,
6971
tree: <Tree />,
7072
},
7173
changeValidate: changeValidate,

0 commit comments

Comments
 (0)