All Projects → bietkul → React Native Form Builder

bietkul / React Native Form Builder

Licence: mit
Handle your forms in a smart way

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Form Builder

Laraform
Reactive Form Builder for Vue.js with Laravel Support
Stars: ✭ 259 (+129.2%)
Mutual labels:  form, form-builder
Jhform
JhForm - 自定义表单工具,更加简单,快捷的创建表单、设置页面
Stars: ✭ 108 (-4.42%)
Mutual labels:  form, picker
Form Create
🔥🔥🔥 强大的动态表单生成器|form-create is a form generation component that can generate dynamic rendering, data collection, verification and submission functions through JSON.
Stars: ✭ 3,698 (+3172.57%)
Mutual labels:  form, form-builder
SettingsAppInSwiftUI
I have recreated a Settings App in iPhoneXR using SwiftUI concepts such as Form,Section,Toggle,Picker,Stack..etc
Stars: ✭ 37 (-67.26%)
Mutual labels:  form, picker
Redaxo yform
YForm für REDAXO 5 – Formulare im Frontend und Backend mit Verwaltung von Datenbank-Tabellen.
Stars: ✭ 60 (-46.9%)
Mutual labels:  form, form-builder
grav-plugin-form
Grav Form Plugin
Stars: ✭ 48 (-57.52%)
Mutual labels:  form, form-builder
Vue Form Builder
Build powerful vue form with JSON schema and composition api.
Stars: ✭ 325 (+187.61%)
Mutual labels:  form, form-builder
Jh flutter demo
a flutter demo
Stars: ✭ 229 (+102.65%)
Mutual labels:  form, picker
Usetheform
React library for composing declarative forms, manage their state, handling their validation and much more.
Stars: ✭ 40 (-64.6%)
Mutual labels:  form, form-builder
Form Builder
PHP表单生成器,快速生成现代化的form表单,支持前后端分离。内置复选框、单选框、输入框、下拉选择框,省市区三级联动,时间选择,日期选择,颜色选择,文件/图片上传等17种常用组件。
Stars: ✭ 607 (+437.17%)
Mutual labels:  form, form-builder
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+117.7%)
Mutual labels:  form, form-builder
Ngx Dynamic Form Builder
FormBuilder + class-transformer + class-validator = dynamic form group builder for Angular10+
Stars: ✭ 93 (-17.7%)
Mutual labels:  form, form-builder
antd-react-form-builder
Example
Stars: ✭ 74 (-34.51%)
Mutual labels:  form, form-builder
fform
Flexibile and extendable form builder with constructor
Stars: ✭ 26 (-76.99%)
Mutual labels:  form, form-builder
react-jsonschema-formbuilder
No description or website provided.
Stars: ✭ 45 (-60.18%)
Mutual labels:  form, form-builder
Vue Form Builder
Super Form Builder built on top of Vue with Drag & Drop functionality, savable-form-schema and easy to maintain/upgrade your form.
Stars: ✭ 292 (+158.41%)
Mutual labels:  form, form-builder
Elm Form
Dynamic forms handling in Elm
Stars: ✭ 189 (+67.26%)
Mutual labels:  form, form-builder
Angular Surveys
Angular survey / form builder inspired by Google Forms
Stars: ✭ 219 (+93.81%)
Mutual labels:  form, form-builder
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+21874.34%)
Mutual labels:  form, form-builder
Formium
The headless form builder for the modern web.
Stars: ✭ 78 (-30.97%)
Mutual labels:  form, form-builder

NPM Version

react-native-form-builder

alt text alt text

Note:

If you're looking for a better form management library with more advanced features, Please check out React Reactive Form.

Features

  • Generate Form Fields UI
  • Manage, track state & values of fields
  • Automatically manages focus to next field on submit (TextInput)
  • Handle all keyboard related problems smartly
  • Supports custom validations & nested forms
  • Uses Nativebase components

Getting Started

Installation

react-native-form-builder requires a peer of native-base

To Install the peer dependecy

$ npm i native-base --save

link the peer dependecy using

react-native link

and then insteall react-native-form-builder

$ npm i react-native-form-builder --save

Basic Usage

  • Install react-native first
$ npm i react-native -g
  • Initialization of a react-native project
$ react-native init myproject
  • Then, edit myproject/index.ios.js, like this:
    Example: Login Form consisting of three fields (username, password, country)
import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import { View, Text, Button } from 'native-base';
import GenerateForm from 'react-native-form-builder';

const styles = {
  wrapper: {
    flex: 1,
    marginTop: 150,
  },
  submitButton: {
    paddingHorizontal: 10,
    paddingTop: 20,
  },
};
// These Fields will create a login form with three fields
const fields = [
  {
    type: 'text',
    name: 'user_name',
    required: true,
    icon: 'ios-person',
    label: 'Username',
  },
  {
    type: 'password',
    name: 'password',
    icon: 'ios-lock',
    required: true,
    label: 'Password',
  },
  {
    type: 'picker',
    name: 'country',
    mode: 'dialog',
    label: 'Select Country',
    defaultValue: 'INDIA',
    options: ['US', 'INDIA', 'UK', 'CHINA', 'FRANCE'],
  },
];
export default class FormGenerator extends Component {
  login() {
    const formValues = this.formGenerator.getValues();
    console.log('FORM VALUES', formValues);
  }
  render() {
    return (
      <View style={styles.wrapper}>
        <View>
          <GenerateForm
            ref={(c) => {
              this.formGenerator = c;
            }}
            fields={fields}
          />
        </View>
        <View style={styles.submitButton}>
          <Button block onPress={() => this.login()}>
            <Text>Login</Text>
          </Button>
        </View>
      </View>
    );
  }
}

AppRegistry.registerComponent('FormGenerator', () => FormGenerator);

Properties

Basic

Prop Default Type Description
autoValidation true bool if false then auto validation script will not run i.e formBuilder will not validate form data automatically.
onValueChange N/A function Invoked every time after a field changes it's value
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.
customComponents N/A object To define your custom type of components.
formData N/A object To prefill the form values.
fields required array Array of form fields.
scrollViewProps N/A object Scrollview custom props.
errorComponent N/A React Component Custom error display component.

Methods:

Currently, these methods are available in FormBuilder, you can access them by using ref property.

getValues

To extract the values of form fields.
Returns: An object consisting of field values (fieldName: value).
for e.g

{
  username: 'bietkul'
  password: '[email protected]'
}

setValues

Forcefully set values for particular fields.
Parameters: An object of key value pairs(name: value).
name: Field name for which value has to be set.
value: Value for that particular field
For e.g

{ name1: value1, name2: value2, .....}

resetForm

Reset Form values as well as errors.

setToDefault

Forcefully set values to default for particular fields.
Parameters: An array of strings, where every string is the name of a field for which value has to be set as default value.
For e.g

[fieldName1, fieldName2, fieldName3 .....]

Form Fields

Field Structure

A field is an object which has the properties required to generate it. It looks something like that :

{
  type: 'text',
  name: 'user_name',
  label: 'Username'
}

Common Properties to all Fields

These properties are applicable on all fields.

Property Required Type Description
type yes enum only possible values are : { text, password, group, email, number, url, select, switch, date } To define type of field.
name yes string Every field should has a name which works as an unique identifier for that field.
label yes string Field label to display.
editable No bool To decide that whether a field is editable or not.
required No bool Helps to decide if a field can has empty value or not. Doesn't work in case of autoValidation = false .
defaultValue No Depends on field's type Sets the intial value for that field before the form initialization.
hidden No bool If true then a field will not be displayed.

Field Types

TextInput

Supports all these kind of types :- text, password, email, url, number

Extra Properties

Props Default Type Description
iconName N/A string Sets the icon, you can use any icon name which is available in react-native-vector-icons
iconOrientaion left (default) or right string Adjust icon orientation
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 .... }

Value Type : String ( Except for type number )

Picker

type: picker Uses native picker

Extra Properties

Props Default Type Description
options (required) N/A array An array of strings to define available options for e.g. ['CAR', 'BIKE', 'BICYCLE']
props N/A object Here you can define extra props which are applicable of react native Picker Component for e.g. { mode: 'dropdown', .... }

Value Type : String

Default Value Type :

You can set default value as a string which must be present in available options. For e.g If options are ['CAR', 'BIKE', 'BICYCLE'] then you can define defaultValue = 'BIKE'

Switch

(type: switch) It's an implement of React Native switch component.

Value Type : Boolean

Date

(type: string)

Extra Properties

Props Default Type Description
mode datetime string To define type of date picker, available values are date, time, datetime
maxDate N/A string or JS Date object To define the maximum date can be select in date picker
minDate N/A string or JS Date object To define the minimum date can be select in date picker

Value Type : String

Default Value Type : string or JS Date object

Select

Extra Properties

Props Required Default Type Description
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.
objectType No false string To define that the values can be of object or not.If true, then you need to specify labelKey & primaryKey
labelKey Yes if objectType = true N/A string To define the key which value need to be used as label.
primaryKey Yes if objectType = true N/A string To define the key which is unique in all objects.
options Yes N/A array of objects or strings An array of objects or strings containing all available options.

Value Type : Array of Strings or Objects

Array of Strings

For e.g. options = ['CAR', 'BIKE', 'BICYCLE']

Array of Objects

If you're using array of objects then please don't forget to define these properties:

objectType: true,
labelKey: 'name', // For Below example
primaryKey: 'id',  // For Below example

For e.g.

options: [
        {
          id: 1,
          name: 'CAR',
        },
        {
          id: 2,
          name: 'BIKE',
        },
        {
          id: 3,
          name: 'BICYCLE',
        },
      ]

Default Value Type : string or JS Date object

In case of object values:

defaultValue: [{
        id: 3,
        name: 'kuldeep2',
        title: 'saxena2',
      }],

In case of string values:

defaultValue: ['CAR', 'BIKE'],

Nested Forms

(type: group) 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. An example will better explain it:

{
      type: 'group',
      name: 'address',
      label: 'Address',
      fields: [
        {
          type: 'text',
          name: 'city',
          label: 'City',
        },
        {
          type: 'picker',
          name: 'country',
          label: 'Country',
          defaultValue: 'INDIA',
          options: ['US', 'INDIA', 'UK', 'CHINA', 'FRANCE'],
        },
      ],
    },

Value Type : Object

For above example the return value object will be something like that:

{ city: 'Bangalore', country: 'INDIA' }

Default Value Type : Object

You can set default value for above example like that:

{ city: 'Newyork', country: 'US' }

Prefill Form Values

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.
For e.g

formData = {
  first_name : 'Jon',
  last_name: 'Snow',
  house: 'Winterfell',
  status: 'Sad'
}

Add Custom Validations

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.

For e.g.

 function validate(field) {
   let error = false;
   let errorMsg = '';
   if (field.name === 'username' && !(field.value && field.value.trim())) {
     error = true;
     errorMsg = 'Username is required';
   }
   if (field.name === 'password' && !(field.value && field.value.trim())) {
     error = true;
     errorMsg = 'Password is required';
   }
   return { error, errorMsg };
 }

Note: Always return an object which will have two entities error type of boolean & errorMsg type of string.

Customize your form

  • Eject Theme by running this command
node node_modules/react-native-form-builder/ejectTheme.js

It will create a file named form-theme.js in your project's root folder.

Customize your theme.

Import theme from form-them.

Use it by passing as theme prop.

import theme from '../form-theme';
....
 <GenerateForm
  ref={(c) => { this.formGenerator = c; }}
  theme = {theme}
  ....
  />

Add Custom Components

Build your custom type's components & handle them easily with the help of form builder. Use the customComponents prop of form builder.

Prototype

It's an object of key value pair where key will be the type of the component & value will be your custom Component.

customComponents = { type1: {component: ComponentName1, props: Props }, type2: {component: ComponentName2} .....}

How To Use

  • Define your custom type in field's object.
  • Form builder extends the props of your component by adding some extra props.
  • You can also pass some extra props in your custom components.
  • In your component you can access these props to handle the state of the field.
Props Type Description
attributes object In this prop you can access the field's attributes (value, error, errorMsg ....).
updateValue(fieldName, Value) function You need to use this method to update the value of the field
onSummitTextInput(fieldName) function If you're using TextInput then you can use this function to automatically manage the text input focus.For example you can define it in the onSubmitEditing prop of TextInput
theme object Use the theme variables to style your component

Add Custom Error Component

  • Now you can use your custom error component to display error messages.
  • In your custom component you will receive two props attributes & theme variables.
  • You can access the error & error message as a property of the attributes object.

Example

The form in second gif is generated by these fields

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].