4242npm install react-native-use-form
4343```
4444
45+
46+
47+ ## Import some localized strings
48+ Ideally you do this somewhere in your ` index.js ` before ` react-native-paper-dates ` is used.
49+ Currently we have en/nl/de/pl/pt/ar/ko/fr translations but it's really easy to add one extra since it are only some labels and error messages.
50+
51+ ``` tsx
52+ // e.g in your index.js
53+ import {
54+ en ,
55+ registerTranslation ,
56+ registerDefaultLocale
57+ } from ' react-native-use-form'
58+ registerTranslation (' en' , en )
59+ // you can override the locale per form
60+ registerDefaultLocale (' en' ) // optional (default = en)
61+ // registerTranslation('nl', nl)
62+ ```
63+
64+ ### or register your own
65+ Please send a PR with your language to make sure all locales are there next time
66+ ``` tsx
67+ import {
68+ registerTranslation ,
69+ } from ' react-native-use-form'
70+ registerTranslation (" en" , {
71+ required : (params ) => ` ${params .fieldKey || params .fieldKey } is required ` ,
72+ lengtShouldBeLongerThan : (params ) =>
73+ ` ${params .fieldKey || params .fieldKey } length should be longer than ${
74+ params .requiredLength
75+ } ` ,
76+ lengthShouldBeShorterThan : (params ) =>
77+ ` ${params .fieldKey || params .fieldKey } length should be shorter than ${
78+ params .requiredLength
79+ } ` ,
80+ shouldFollowRegex : (params ) =>
81+ params .errorMessage ||
82+ ` ${params .fieldKey || params .fieldKey } is not in the right format ` ,
83+ })
84+ ```
85+
4586## Usage
4687``` tsx
4788
@@ -51,6 +92,9 @@ import { View } from 'react-native';
5192import { useFormState , Form } from ' react-native-use-form' ;
5293import { Button , HelperText , TextInput } from ' react-native-paper' ;
5394
95+
96+
97+
5498export default function App() {
5599 const [
56100 { errors, submit, formProps, hasError },
@@ -62,8 +106,9 @@ export default function App() {
62106 password: ' ' ,
63107 },
64108 {
109+ locale: ' en' , // optional override
65110 onChange : (latestValues ) => {
66- // do something with latestValues
111+ // optional: do something with latestValues
67112 },
68113 onSubmit : (submittedValues ) => {
69114 // do something with submittedValues
@@ -79,47 +124,34 @@ export default function App() {
79124 }}
80125 >
81126 <Form { ... formProps } >
82- <TextInput
127+ <TextInputWithError
83128 mode = " outlined"
84- error = { hasError (' email' )}
85129 { ... email (' email' , {
86130 validate : (v ) => {
87131 return looksLikeMail (v ) ? true : ' Email-address is invalid' ;
88132 },
133+ label: " E-mail"
89134 })}
90- label = " E-mail"
91135 />
92- <HelperText type = " error" visible = { hasError (' email' )} >
93- { errors .email }
94- </HelperText >
95- <TextInput
136+ <TextInputWithError
96137 mode = " outlined"
97138 { ... telephone (' telephone' , {
98139 validate : (v ) => {
99140 console .log ({ v });
100141 return looksLikeTelephone (v ) ? true : ' Telephone is invalid' ;
101142 },
143+ label: " Telefoon"
102144 })}
103- label = " Telefoon"
104- error = { hasError (' telephone' )}
105145 />
106- <HelperText type = " error" visible = { hasError (' telephone' )} >
107- { errors .telephone }
108- </HelperText >
109-
110- <TextInput
146+ <TextInputWithError
111147 mode = " outlined"
112148 { ... password (' password' , {
113149 required: true ,
114150 minLength: 3 ,
115151 maxLength: 10 ,
152+ label: " Wachtwoord"
116153 })}
117- label = " Wachtwoord"
118- error = { hasError (' password' )}
119154 />
120- <HelperText type = " error" visible = { hasError (' password' )} >
121- { errors .password }
122- </HelperText >
123155 <Button mode = " contained" onPress = { submit } >
124156 Save
125157 </Button >
@@ -128,6 +160,20 @@ export default function App() {
128160 );
129161}
130162
163+
164+
165+ function TextInputWithError({ errorMessage , ... rest }: React .ComponentProps <typeof TextInput > & { errorMessage? : string }) {
166+ return (
167+ <>
168+ <TextInput { ... rest } />
169+ <HelperText type = " error" visible = { rest .error } >
170+ { errorMessage || ' ' }
171+ </HelperText >
172+ </>
173+ );
174+ }
175+
176+
131177// you can add your own validate functions
132178function looksLikeTelephone(str : string ): boolean {
133179 if (str .length !== 10 ) {
0 commit comments