11import React from 'react' ;
2- import { Form , Button } from '@uiw/react-native' ;
2+ import { Form , Button , Toast } from '@uiw/react-native' ;
33import Layout , { Container } from '../../Layout' ;
44const { Body, Footer} = Layout ;
55
66const FormDemo = ( ) => {
7+ const form = Form . useForm ( ) ;
78 const items = [
89 {
910 type : 'input' ,
@@ -13,12 +14,10 @@ const FormDemo = () => {
1314 {
1415 type : 'input' ,
1516 field : 'age' ,
17+ validate : ( val : any ) => ( Number ( val ) > 30 || Number ( val ) < 10 ? `起输入10-30` : '' ) ,
1618 } ,
1719 ] ;
18-
19- const initialValues = { name : '王滴滴' , age : '' } ;
20-
21- const form = Form . useForm ( ) ;
20+ const initialValues = { name : '王滴滴' , age : '31' } ;
2221
2322 return (
2423 < Container >
@@ -28,18 +27,27 @@ const FormDemo = () => {
2827 < Button
2928 type = "primary"
3029 onPress = { ( ) => {
31- const values = form . getStore ( ) ;
32- const age = form . getFieldValue ( 'age' ) ;
33- console . log ( 'values' , values ) ;
30+ form
31+ . validateFields ( )
32+ . then ( ( values : any ) => Toast . success ( JSON . stringify ( values ) ) )
33+ . catch ( ( errors : any ) => Toast . warning ( JSON . stringify ( errors ) ) ) ;
3434 } } >
35- 默认按钮
35+ 确定
3636 </ Button >
3737 < Button type = "primary" onPress = { ( ) => form . setFieldValue ( 'age' , '456' ) } >
3838 设置
3939 </ Button >
4040 < Button type = "primary" onPress = { ( ) => form . resetFieldValue ( ) } >
4141 重置
4242 </ Button >
43+ < Button
44+ type = "primary"
45+ onPress = { ( ) => {
46+ const errors = form . validate ( ) ;
47+ Toast . warning ( JSON . stringify ( errors ) ) ;
48+ } } >
49+ 触发验证
50+ </ Button >
4351 </ Body >
4452 < Footer />
4553 </ Layout >
0 commit comments