├── .gitignore
├── LICENSE
├── README.md
├── ejectTheme.js
├── package.json
└── src
├── components
└── panel
│ ├── index.js
│ └── styles.js
├── fields
├── date
│ ├── index.js
│ └── styles.js
├── form
│ └── index.js
├── picker
│ ├── index.js
│ └── styles.js
├── select
│ ├── index.js
│ └── styles.js
├── switch
│ ├── index.js
│ └── styles.js
└── textInput
│ ├── index.js
│ └── styles.js
├── formBuilder
└── index.js
├── index.js
├── styles.js
├── theme.js
└── utils
├── config.js
├── methods.js
└── validators.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 |
17 | # nyc test coverage
18 | .nyc_output
19 |
20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21 | .grunt
22 |
23 | # node-waf configuration
24 | .lock-wscript
25 |
26 | # Compiled binary addons (http://nodejs.org/api/addons.html)
27 | build/Release
28 |
29 | # Dependency directories
30 | node_modules
31 | jspm_packages
32 |
33 | # Optional npm cache directory
34 | .npm
35 |
36 | # Optional REPL history
37 | .node_repl_history
38 |
39 | #DS STORE
40 | .DS_Store
41 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Kuldeep Saxena
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://www.npmjs.com/package/react-native-form-builder)
2 | # react-native-form-builder
3 | 
4 | 
5 | # Note:
6 | If you're looking for a better form management library with more advanced features, Please check out [React Reactive Form](https://github.com/bietkul/react-reactive-form).
7 | ## Features
8 | - Generate Form Fields UI
9 | - Manage, track state & values of fields
10 | - Automatically manages focus to next field on submit (TextInput)
11 | - Handle all keyboard related problems smartly
12 | - Supports custom validations & nested forms
13 | - Uses Nativebase components
14 |
15 | ## Getting Started
16 |
17 | - [Installation](#installation)
18 | - [Basic Usage](#basic-usage)
19 | - [Properties](#properties)
20 | + [Basic](#basic)
21 | + [Methods](#methods)
22 | - [Form Fields](#form-fields)
23 | + [Field Structure](#field-structure)
24 | + [Common Properties to all Fields](#common-properties-to-all-fields)
25 | - [Field Types](#field-types)
26 | + [TextInput](#textinput)
27 | + [Picker](#picker)
28 | + [Switch](#switch)
29 | + [Date](#date)
30 | + [Select](#select)
31 | + [Nested Forms](#nested-forms)
32 | + [Prefilling Form's Values](#prefill-form-values)
33 | + [Add Custom Validations](#add-custom-validations)
34 | + [Customize Your Form](#customize-your-form)
35 | + [Add custom components](#add-custom-components)
36 | + [Add custome error component](#add-custom-error-component)
37 | - [Example](#example)
38 |
39 | ## Installation
40 |
41 | `react-native-form-builder` requires a peer of [`native-base`](https://github.com/GeekyAnts/NativeBase)
42 |
43 |
44 | To Install the peer dependecy
45 | ```
46 | $ npm i native-base --save
47 |
48 | ```
49 | link the peer dependecy using
50 |
51 | ```
52 | react-native link
53 | ```
54 | and then insteall `react-native-form-builder`
55 |
56 | ```bash
57 | $ npm i react-native-form-builder --save
58 | ```
59 |
60 | ## Basic Usage
61 |
62 | - Install `react-native` first
63 |
64 | ```bash
65 | $ npm i react-native -g
66 | ```
67 | - Initialization of a react-native project
68 |
69 | ```bash
70 | $ react-native init myproject
71 | ```
72 |
73 | - Then, edit `myproject/index.ios.js`, like this:
74 | Example: Login Form consisting of three fields (username, password, country)
75 |
76 | ```jsx
77 | import React, { Component } from 'react';
78 | import { AppRegistry } from 'react-native';
79 | import { View, Text, Button } from 'native-base';
80 | import GenerateForm from 'react-native-form-builder';
81 |
82 | const styles = {
83 | wrapper: {
84 | flex: 1,
85 | marginTop: 150,
86 | },
87 | submitButton: {
88 | paddingHorizontal: 10,
89 | paddingTop: 20,
90 | },
91 | };
92 | // These Fields will create a login form with three fields
93 | const fields = [
94 | {
95 | type: 'text',
96 | name: 'user_name',
97 | required: true,
98 | icon: 'ios-person',
99 | label: 'Username',
100 | },
101 | {
102 | type: 'password',
103 | name: 'password',
104 | icon: 'ios-lock',
105 | required: true,
106 | label: 'Password',
107 | },
108 | {
109 | type: 'picker',
110 | name: 'country',
111 | mode: 'dialog',
112 | label: 'Select Country',
113 | defaultValue: 'INDIA',
114 | options: ['US', 'INDIA', 'UK', 'CHINA', 'FRANCE'],
115 | },
116 | ];
117 | export default class FormGenerator extends Component {
118 | login() {
119 | const formValues = this.formGenerator.getValues();
120 | console.log('FORM VALUES', formValues);
121 | }
122 | render() {
123 | return (
124 |
125 |
126 | {
128 | this.formGenerator = c;
129 | }}
130 | fields={fields}
131 | />
132 |
133 |
134 |
137 |
138 |
139 | );
140 | }
141 | }
142 |
143 | AppRegistry.registerComponent('FormGenerator', () => FormGenerator);
144 | ```
145 | ## Properties
146 | ### Basic
147 | | Prop | Default | Type | Description |
148 | | :------------ |:---------------:| :---------------:| :-----|
149 | | autoValidation | true | `bool` | if `false` then auto validation script will not run i.e formBuilder will not validate form data automatically. |
150 | | onValueChange | N/A | `function` | Invoked every time after a field changes it's value |
151 | | customValidation | N/A | `function` | This function will be triggered everytime before a field changes it's value, here you can write your custom validation script & set error accordingly. |
152 | | customComponents | N/A | `object` | To define your custom type of components.|
153 | | formData | N/A | `object` | To prefill the form values.|
154 | | fields | `required` | `array` | Array of form fields. |
155 | | scrollViewProps | N/A | `object` | Scrollview custom props. |
156 | | errorComponent | N/A | `React Component` | Custom error display component. |
157 |
158 | ### Methods:
159 | Currently, these methods are available in FormBuilder, you can access them by using ref property.
160 |
161 | ### getValues
162 | To extract the values of form fields.
163 | Returns: An object consisting of field values (fieldName: value).
164 | for e.g
165 | ```
166 | {
167 | username: 'bietkul'
168 | password: 'bietkul@git'
169 | }
170 | ```
171 | ### setValues
172 | Forcefully set values for particular fields.
173 | Parameters: An object of key value pairs(`name: value`).
174 | name: Field name for which value has to be set.
175 | value: Value for that particular field
176 | For e.g
177 | ```
178 | { name1: value1, name2: value2, .....}
179 | ```
180 | ### resetForm
181 | Reset Form values as well as errors.
182 | ### setToDefault
183 | Forcefully set values to default for particular fields.
184 | Parameters: An array of strings, where every string is the name of a field for which value has to be set as default value.
185 | For e.g
186 | ```
187 | [fieldName1, fieldName2, fieldName3 .....]
188 | ```
189 |
190 | ### Form Fields
191 | ### Field Structure
192 | A field is an object which has the properties required to generate it.
193 | It looks something like that :
194 | ```jsx
195 | {
196 | type: 'text',
197 | name: 'user_name',
198 | label: 'Username'
199 | }
200 | ```
201 |
202 | ### Common Properties to all Fields
203 | These properties are applicable on all fields.
204 |
205 | | Property | Required | Type | Description |
206 | | :------------ |:---------------:| :---------------:| :-----|
207 | | type | `yes` | `enum` only possible values are : { text, password, group, email, number, url, select, switch, date } | To define type of field. |
208 | | name | `yes` | `string` | Every field should has a name which works as an unique identifier for that field. |
209 | | label | `yes` | `string` | Field label to display. |
210 | | editable | `No` | `bool` | To decide that whether a field is editable or not. |
211 | | required | `No` | `bool` | Helps to decide if a field can has empty value or not. Doesn't work in case of `autoValidation = false` . |
212 | | defaultValue | `No` | `Depends on field's type` | Sets the intial value for that field before the form initialization. |
213 | | hidden | `No` | `bool` | If `true` then a field will not be displayed. |
214 |
215 | ### Field Types
216 | ### TextInput
217 | Supports all these kind of types :-
218 | text,
219 | password,
220 | email,
221 | url,
222 | number
223 |
224 | #### Extra Properties
225 | | Props | Default | Type | Description |
226 | | :------------ |:--------------- |:---------------| :-----|
227 | | iconName | N/A | `string` | Sets the icon, you can use any icon name which is available in `react-native-vector-icons`|
228 | | iconOrientaion | `left (default)` or `right` | `string` | Adjust icon orientation |
229 | | props | N/A | `object` | Here you can define extra props which are applicable for react native TextInput Component. For e.g. { multiline: true, secureTextEntry : true .... }
230 |
231 | #### Value Type : `String` ( Except for type `number` )
232 |
233 | ### Picker
234 | `type: picker`
235 | Uses native picker
236 |
237 | #### Extra Properties
238 | | Props | Default | Type | Description |
239 | | :------------ |:--------------- |:---------------| :-----|
240 | | options (required) | N/A | `array` | An array of strings to define available options for e.g. ['CAR', 'BIKE', 'BICYCLE'] |
241 | | props | N/A | `object` | Here you can define extra props which are applicable of react native Picker Component for e.g. { mode: 'dropdown', .... }
242 |
243 | #### Value Type : `String`
244 |
245 | #### Default Value Type :
246 | You can set default value as a string which must be present in available options.
247 | For e.g If options are ['CAR', 'BIKE', 'BICYCLE'] then you can define `defaultValue = 'BIKE'`
248 |
249 | ### Switch
250 | `(type: switch)`
251 | It's an implement of React Native `switch` component.
252 |
253 | #### Value Type : `Boolean`
254 |
255 | ### Date
256 | `(type: string)`
257 |
258 | #### Extra Properties
259 | | Props | Default | Type | Description |
260 | | :------------ |:--------------- |:---------------| :-----|
261 | | mode | `datetime` | `string` | To define type of date picker, available values are `date, time, datetime` |
262 | | maxDate | N/A | `string` or `JS Date object` | To define the maximum date can be select in date picker |
263 | | minDate | N/A | `string` or `JS Date object` | To define the minimum date can be select in date picker |
264 |
265 |
266 | #### Value Type : `String`
267 | #### Default Value Type : `string` or `JS Date object`
268 |
269 | ### Select
270 |
271 | #### Extra Properties
272 | | Props | Required | Default | Type | Description |
273 | | :------------| :------------ |:--------------- |:---------------| :-----|
274 | | multiple | `No` | `false` | `bool` | To define that the field can accept multple values or not i.e user can select multiple values at a time or not. |
275 | | objectType | `No` | `false` | `string` | To define that the values can be of object or not.If `true`, then you need to specify `labelKey` & `primaryKey` |
276 | | labelKey | `Yes` if `objectType = true` | `N/A` | `string` | To define the key which value need to be used as label. |
277 | | primaryKey | `Yes` if `objectType = true` | `N/A` | `string` | To define the key which is unique in all objects. |
278 | | options | `Yes` | `N/A` | array of `objects` or `strings` | An array of `objects` or `strings` containing all available options.|
279 |
280 | #### Value Type : Array of `Strings` or `Objects`
281 | #### Array of Strings
282 | For e.g. `options = ['CAR', 'BIKE', 'BICYCLE']`
283 | #### Array of Objects
284 | If you're using array of objects then please don't forget to define these properties:
285 | ```jsx
286 | objectType: true,
287 | labelKey: 'name', // For Below example
288 | primaryKey: 'id', // For Below example
289 | ```
290 | For e.g.
291 | ```
292 | options: [
293 | {
294 | id: 1,
295 | name: 'CAR',
296 | },
297 | {
298 | id: 2,
299 | name: 'BIKE',
300 | },
301 | {
302 | id: 3,
303 | name: 'BICYCLE',
304 | },
305 | ]
306 | ```
307 | #### Default Value Type : `string` or `JS Date object`
308 | In case of object values:
309 | ```
310 | defaultValue: [{
311 | id: 3,
312 | name: 'kuldeep2',
313 | title: 'saxena2',
314 | }],
315 | ```
316 | In case of string values:
317 | ```
318 | defaultValue: ['CAR', 'BIKE'],
319 | ```
320 |
321 | ## Nested Forms
322 | `(type: group)`
323 | Form Builder also supports nested forms, some times you need to wrap all of your form values in an object or we can say that you have some nested fields, in this case you can define `group` fields.
324 | An example will better explain it:
325 |
326 | ```
327 | {
328 | type: 'group',
329 | name: 'address',
330 | label: 'Address',
331 | fields: [
332 | {
333 | type: 'text',
334 | name: 'city',
335 | label: 'City',
336 | },
337 | {
338 | type: 'picker',
339 | name: 'country',
340 | label: 'Country',
341 | defaultValue: 'INDIA',
342 | options: ['US', 'INDIA', 'UK', 'CHINA', 'FRANCE'],
343 | },
344 | ],
345 | },
346 | ```
347 | #### Value Type : `Object`
348 |
349 | For above example the return value object will be something like that:
350 | ```
351 | { city: 'Bangalore', country: 'INDIA' }
352 | ```
353 | #### Default Value Type : `Object`
354 |
355 | You can set default value for above example like that:
356 | ```
357 | { city: 'Newyork', country: 'US' }
358 | ```
359 | ## Prefill Form Values
360 | This feature of formBuilder is very helpful in case of edit mode i.e if you want to edit the values of a form then you can easily prefill the form by using `formData` prop.
361 | For e.g
362 | ```
363 | formData = {
364 | first_name : 'Jon',
365 | last_name: 'Snow',
366 | house: 'Winterfell',
367 | status: 'Sad'
368 | }
369 | ```
370 |
371 | ## Add Custom Validations
372 |
373 | It's very easy to add your custom validations & error messages with FormBuilder.All you need to do is define a function & pass it as `customValidation` prop.
374 |
375 | For e.g.
376 | ```
377 | function validate(field) {
378 | let error = false;
379 | let errorMsg = '';
380 | if (field.name === 'username' && !(field.value && field.value.trim())) {
381 | error = true;
382 | errorMsg = 'Username is required';
383 | }
384 | if (field.name === 'password' && !(field.value && field.value.trim())) {
385 | error = true;
386 | errorMsg = 'Password is required';
387 | }
388 | return { error, errorMsg };
389 | }
390 | ```
391 | Note: Always return an object which will have two entities `error` type of `boolean` & `errorMsg` type of `string`.
392 |
393 | ## Customize your form
394 | - Eject Theme by running this command
395 | ```bash
396 | node node_modules/react-native-form-builder/ejectTheme.js
397 | ```
398 | It will create a file named `form-theme.js` in your project's root folder.
399 |
400 | Customize your theme.
401 |
402 | Import theme from `form-them`.
403 |
404 | Use it by passing as `theme` prop.
405 | ```
406 | import theme from '../form-theme';
407 | ....
408 | { this.formGenerator = c; }}
410 | theme = {theme}
411 | ....
412 | />
413 | ```
414 | ## Add Custom Components
415 | Build your custom type's components & handle them easily with the help of form builder.
416 | Use the `customComponents` prop of form builder.
417 | ### Prototype
418 | It's an object of key value pair where key will be the `type` of the component & value will be your custom Component.