1
1
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' ;
3
3
import { View } from 'react-native' ;
4
4
import Layout , { Container } from '../../Layout' ;
5
5
const { Body, Footer, Card} = Layout ;
@@ -18,6 +18,42 @@ const FormDemo = () => {
18
18
const [ state , setStore ] = useState ( { } ) ;
19
19
20
20
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 = [
21
57
{
22
58
type : 'input' ,
23
59
field : 'name' ,
@@ -60,6 +96,58 @@ const FormDemo = () => {
60
96
{ label : '东北菜' , value : 10 } ,
61
97
] ,
62
98
} ,
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
+ } ,
63
151
{
64
152
type : 'rate' ,
65
153
field : 'rate' ,
@@ -275,7 +363,57 @@ const FormDemo = () => {
275
363
< Form
276
364
form = { form }
277
365
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 } }
279
417
watch = { {
280
418
name : ( value : unknown ) => console . log ( 'value' , value ) ,
281
419
} }
@@ -284,9 +422,11 @@ const FormDemo = () => {
284
422
} }
285
423
changeValidate = { true }
286
424
/>
425
+ < Spacing />
287
426
< View >
288
427
< Text color = "primary_text" > { JSON . stringify ( state ) } </ Text >
289
428
</ View >
429
+ < Spacing />
290
430
< Button
291
431
type = "primary"
292
432
onPress = { ( ) => {
@@ -297,12 +437,15 @@ const FormDemo = () => {
297
437
} } >
298
438
确定
299
439
</ Button >
440
+ < Spacing />
300
441
< Button type = "primary" onPress = { ( ) => form . setFieldValue ( 'age' , '456' ) } >
301
442
设置
302
443
</ Button >
444
+ < Spacing />
303
445
< Button type = "primary" onPress = { ( ) => form . resetFieldValue ( ) } >
304
446
重置
305
447
</ Button >
448
+ < Spacing />
306
449
< Button
307
450
type = "primary"
308
451
onPress = { ( ) => {
0 commit comments