@@ -6,26 +6,27 @@ This repository is a set of high order components designed to help you take cont
66
77** Features**
88
9- - Easily composable set of helpers
10- - Connects your React Native input to Formik with no boilerplate (See ` makeReactNativeField ` )
11- - Add a ` type ` prop on your TextInput to take care of the input options based on the type (See ` withInputTypeProps ` )
12- - Automatically focus the next input (See ` withNextInputAutoFocus ` )
13- - Awesome Test coverage [ ![ Coverage Status] ( https://coveralls.io/repos/github/bamlab/react-native-formik/badge.svg?branch=master )] ( https://coveralls.io/github/bamlab/react-native-formik?branch=master )
9+ * Easily composable set of helpers
10+ * Connects your React Native input to Formik with no boilerplate (See ` makeReactNativeField ` )
11+ * Add a ` type ` prop on your TextInput to take care of the input options based on the type (See ` withInputTypeProps ` )
12+ * Automatically focus the next input (See ` withNextInputAutoFocus ` )
13+ * Awesome Test coverage [ ![ Coverage Status] ( https://coveralls.io/repos/github/bamlab/react-native-formik/badge.svg?branch=master )] ( https://coveralls.io/github/bamlab/react-native-formik?branch=master )
1414
1515** Table of contents**
1616
17- - [ Installation] ( #installation )
18- - [ Advanced Example] ( #advanced-example )
19- - [ Formatting inputs] ( #formatting-inputs )
20- - [ API] ( #api )
21- - [ makeReactNativeField] ( #makereactnativefield )
22- - [ setFormikInitialValue] ( #setFormikInitialValue )
23- - [ withError] ( #witherror )
24- - [ withFocus] ( #withfocus )
25- - [ withFormik] ( #withformik )
26- - [ withInputTypeProps] ( #withinputtypeprops )
27- - [ withNextInputAutoFocus] ( #withnextinputautofocus )
28- - [ withTouched] ( #withtouched )
17+ * [ Installation] ( #installation )
18+ * [ Advanced Example] ( #advanced-example )
19+ * [ Formatting inputs] ( #formatting-inputs )
20+ * [ API] ( #api )
21+ * [ makeReactNativeField] ( #makereactnativefield )
22+ * [ setFormikInitialValue] ( #setFormikInitialValue )
23+ * [ withError] ( #witherror )
24+ * [ withFocus] ( #withfocus )
25+ * [ withFormik] ( #withformik )
26+ * [ withInputTypeProps] ( #withinputtypeprops )
27+ * [ withNextInputAutoFocus] ( #withnextinputautofocus )
28+ * [ withTouched] ( #withtouched )
29+ * [ withPickerValues] ( #withpickervalues )
2930
3031## Installation
3132
@@ -38,6 +39,7 @@ yarn add react-native-formik
3839Say we want to create a form with Material design inputs.
3940
4041### Create a custom input
42+
4143Let's create our custom text input design, called ` MaterialTextInput ` :
4244
4345We can use [ react-native-material-textfield] ( https://github.com/n4kz/react-native-material-textfield ) for the material design.
@@ -47,9 +49,9 @@ Notice our component also implement a `focus` function, for `withNextInputAutoFo
4749
4850``` javascript
4951// MaterialTextInput.js
50- import React from " react" ;
51- import { Text , View } from " react-native" ;
52- import { TextField } from " react-native-material-textfield" ;
52+ import React from ' react' ;
53+ import { Text , View } from ' react-native' ;
54+ import { TextField } from ' react-native-material-textfield' ;
5355
5456export default class MaterialTextInput extends React .PureComponent {
5557 // Your custom input needs a focus function for `withNextInputAutoFocus` to work
@@ -61,19 +63,25 @@ export default class MaterialTextInput extends React.PureComponent {
6163 const { error , touched , ... props } = this .props ;
6264
6365 const displayError = !! error && touched;
64- const errorColor = " rgb(239, 51, 64)" ;
66+ const errorColor = ' rgb(239, 51, 64)' ;
6567
6668 return (
6769 < View>
6870 < TextField
69- ref= {input => this .input = input}
71+ ref= {input => ( this .input = input) }
7072 labelHeight= {12 }
71- baseColor= {displayError ? errorColor : " #1976D2" }
73+ baseColor= {displayError ? errorColor : ' #1976D2' }
7274 tintColor= " #2196F3"
7375 textColor= " #212121"
7476 {... props}
7577 / >
76- < Text style= {{ textAlign: " right" , color: displayError ? errorColor : " transparent" , height: 20 }}>
78+ < Text
79+ style= {{
80+ textAlign: ' right' ,
81+ color: displayError ? errorColor : ' transparent' ,
82+ height: 20 ,
83+ }}
84+ >
7785 {error}
7886 < / Text >
7987 < / View>
@@ -90,34 +98,34 @@ Compose our input with high order components to make it awesome.
9098Let's add in ` withNextInputAutoFocusInput ` :
9199
92100``` javascript
93- import { compose } from " recompose" ;
94- import makeInputGreatAgain , { withNextInputAutoFocusInput } from " react-native-formik" ;
95- import MaterialTextInput from " ./MaterialTextInput" ;
101+ import { compose } from ' recompose' ;
102+ import makeInputGreatAgain , { withNextInputAutoFocusInput } from ' react-native-formik' ;
103+ import MaterialTextInput from ' ./MaterialTextInput' ;
96104
97105const MyInput = compose (makeInputGreatAgain, withNextInputAutoFocusInput)(MaterialTextInput);
98106```
99107
100108To complement ` withNextInputAutoFocusInput ` , we need to create a ` Form ` component, for instance:
101109
102110``` javascript
103- import { View } from " react-native" ;
104- import { withNextInputAutoFocusForm } from " react-native-formik" ;
111+ import { View } from ' react-native' ;
112+ import { withNextInputAutoFocusForm } from ' react-native-formik' ;
105113
106114const Form = withNextInputAutoFocusForm (View);
107115```
108116
109117We can also create a validation schema, with ` yup ` . It's of course possible to use other validation possibilities provided by Formik, but ` yup ` makes validation and error messaging painless.
110118
111119``` javascript
112- import Yup from " yup" ;
120+ import Yup from ' yup' ;
113121
114122const validationSchema = Yup .object ().shape ({
115123 email: Yup .string ()
116124 .required ()
117125 .email (" well that's not an email" ),
118126 password: Yup .string ()
119127 .required ()
120- .min (2 , " pretty sure this will be hacked" )
128+ .min (2 , ' pretty sure this will be hacked' ),
121129});
122130```
123131
@@ -126,7 +134,7 @@ Then the form in itself becomes simple:
126134``` javascript
127135export default props => (
128136 < Formik
129- onSubmit= {( values ) => console .log (values)}
137+ onSubmit= {values => console .log (values)}
130138 validationSchema= {validationSchema}
131139 render= {props => {
132140 return (
@@ -146,30 +154,32 @@ export default props => (
146154Full code:
147155
148156``` javascript
149- import React from " react" ;
150- import { Button , TextInput , View } from " react-native" ;
151- import { compose } from " recompose"
152- import { Formik } from " formik" ;
153- import Yup from " yup" ;
154- import makeInputGreatAgain , { withNextInputAutoFocusForm , withNextInputAutoFocusInput } from " react-native-formik" ;
155- import MaterialTextInput from " ./MaterialTextInput" ;
157+ import React from ' react' ;
158+ import { Button , TextInput , View } from ' react-native' ;
159+ import { compose } from ' recompose' ;
160+ import { Formik } from ' formik' ;
161+ import Yup from ' yup' ;
162+ import makeInputGreatAgain, {
163+ withNextInputAutoFocusForm ,
164+ withNextInputAutoFocusInput ,
165+ } from ' react-native-formik' ;
166+ import MaterialTextInput from ' ./MaterialTextInput' ;
156167
157168const MyInput = compose (makeInputGreatAgain, withNextInputAutoFocusInput)(MaterialTextInput);
158169const Form = withNextInputAutoFocusForm (View);
159170
160171const validationSchema = Yup .object ().shape ({
161172 email: Yup .string ()
162- .required (" please! email?" )
173+ .required (' please! email?' )
163174 .email (" well that's not an email" ),
164175 password: Yup .string ()
165176 .required ()
166- .min (2 , " pretty sure this will be hacked" )
177+ .min (2 , ' pretty sure this will be hacked' ),
167178});
168179
169-
170180export default props => (
171181 < Formik
172- onSubmit= {( values ) => console .log (values)}
182+ onSubmit= {values => console .log (values)}
173183 validationSchema= {validationSchema}
174184 render= {props => {
175185 return (
@@ -212,22 +222,24 @@ const formatPhoneNumber: string => string = (unformattedPhoneNumber) => ...;
212222### makeReactNativeField
213223
214224Connects your React Native component to the Formik context:
215- - its value will be set
216- - it will send ` onChangeText ` and ` onBlur ` events to Formik
225+
226+ * its value will be set
227+ * it will send ` onChangeText ` and ` onBlur ` events to Formik
217228
218229Now you only need this code:
230+
219231``` javascript
220- import React from " react" ;
221- import { TextInput , View } from " react-native" ;
222- import { Formik } from " formik" ;
223- import { makeReactNativeField } from " react-native-formik" ;
232+ import React from ' react' ;
233+ import { TextInput , View } from ' react-native' ;
234+ import { Formik } from ' formik' ;
235+ import { makeReactNativeField } from ' react-native-formik' ;
224236
225237const MyInput = makeReactNativeField (TextInput);
226238
227239export default props => {
228240 return (
229241 < Formik
230- onSubmit= {( values ) => console .log (values)}
242+ onSubmit= {values => console .log (values)}
231243 render= {props => {
232244 return (
233245 < View>
@@ -242,32 +254,33 @@ export default props => {
242254```
243255
244256instead of:
257+
245258``` javascript
246- import React from " react" ;
247- import { TextInput , View } from " react-native" ;
248- import { Formik } from " formik" ;
249- import { makeReactNativeField } from " react-native-formik" ;
259+ import React from ' react' ;
260+ import { TextInput , View } from ' react-native' ;
261+ import { Formik } from ' formik' ;
262+ import { makeReactNativeField } from ' react-native-formik' ;
250263
251264const MyInput = makeReactNativeField (TextInput);
252265
253266export default props => {
254267 return (
255268 < Formik
256- onSubmit= {( values ) => console .log (values)}
269+ onSubmit= {values => console .log (values)}
257270 render= {props => {
258271 return (
259272 < View>
260273 < MyInput
261274 name= " email"
262275 value= {props .value .email }
263- onChangeText= {( text ) => props .setFieldValue (" email" , text)}
264- onBlur= {() => setFieldTouched (" email" )}
276+ onChangeText= {text => props .setFieldValue (' email' , text)}
277+ onBlur= {() => setFieldTouched (' email' )}
265278 / >
266279 < MyInput
267280 name= " password"
268281 value= {props .value .email }
269- onChangeText= {( text ) => props .setFieldValue (" password" , text)}
270- onBlur= {() => setFieldTouched (" password" )}
282+ onChangeText= {text => props .setFieldValue (' password' , text)}
283+ onBlur= {() => setFieldTouched (' password' )}
271284 / >
272285 < / View>
273286 );
@@ -302,8 +315,8 @@ Let's face it, you'll always want to remove auto-capitalization for email inputs
302315Using ` withInputTypeProps ` and passing a ` type ` , you'll always get the correct props for you input.
303316
304317``` javascript
305- import { TextInput } from " react-native" ;
306- import { withInputTypeProps } from " react-native-formik" ;
318+ import { TextInput } from ' react-native' ;
319+ import { withInputTypeProps } from ' react-native-formik' ;
307320
308321const MyInput = withInputTypeProps (TextInput);
309322
@@ -316,21 +329,21 @@ Check [the props set by the type](./src/withInputTypeProps) in the source!
316329
317330### withNextInputAutoFocus
318331
319- - when an input is submitted, it will automatically focuses on the next or submit the form if it's the last one
320- - sets return key to "next" or "done" if input is the last one or not
321- - :warning : your input component needs to be a class and needs to implement a ` focus ` function
322- - :warning : Inputs need to be wrapped by ` withNextInputAutoFocusInput ` and the container of the inputs need to be wrapped in ` withNextInputAutoFocusForm ` .
332+ * when an input is submitted, it will automatically focuses on the next or submit the form if it's the last one
333+ * sets return key to "next" or "done" if input is the last one or not
334+ * :warning : your input component needs to be a class and needs to implement a ` focus ` function
335+ * :warning : Inputs need to be wrapped by ` withNextInputAutoFocusInput ` and the container of the inputs need to be wrapped in ` withNextInputAutoFocusForm ` .
323336
324337``` javascript
325- import { TextInput , View } from " react-native" ;
326- import { withNextInputAutoFocusForm , withNextInputAutoFocusInput } from " react-native-formik" ;
338+ import { TextInput , View } from ' react-native' ;
339+ import { withNextInputAutoFocusForm , withNextInputAutoFocusInput } from ' react-native-formik' ;
327340
328341const MyInput = withNextInputAutoFocusInput (TextInput);
329342const Form = withNextInputAutoFocusForm (View);
330343
331344export default props => (
332345 < Formik
333- onSubmit= {( values ) => console .log (values)}
346+ onSubmit= {values => console .log (values)}
334347 validationSchema= {validationSchema}
335348 render= {props => {
336349 return (
@@ -348,3 +361,32 @@ export default props => (
348361### withTouched
349362
350363Pass in the Formik touched value for the input as a prop.
364+
365+ ### withPickerValues
366+
367+ Wraps your component into a ` TouchableOpacity ` which, when pressed, opens a dialog to pick a value.
368+ You need to provide a ` values ` props with the pickable items.
369+
370+ ``` javascript
371+ import { TextInput , View } from ' react-native' ;
372+ import { withPickerValues } from ' react-native-formik' ;
373+
374+ const MyPicker = withPickerValues (TextInput);
375+
376+ export default props => (
377+ < Formik
378+ onSubmit= {values => console .log (values)}
379+ validationSchema= {validationSchema}
380+ render= {props => {
381+ return (
382+ < View>
383+ < MyPicker
384+ name= " gender"
385+ values= {[{ label: ' male' , value: ' Mr' }, { label: ' female' , value: ' Mrs' }]}
386+ / >
387+ < / View>
388+ );
389+ }}
390+ / >
391+ );
392+ ```
0 commit comments