0.0.187 • Published 16 days ago

react-native-form0 v0.0.187

Weekly downloads
166
License
MIT
Repository
github
Last release
16 days ago

react-native-form0

Build model based simple forms for React Native

Objective

  • Generate Cross platform mobile form fields on react native
  • Ability to track the state and values
  • Load the form with predefined values
  • Support nested forms and validations

Getting Started

Installation

npm i react-native-form0 --save

Usage

  1. Define the form schema as json
[
    {
        "type": "text",
        "name": "user_name",
        "required": true,
        "label": "Username"
    },
    {
        "type": "email",
        "name": "email",
        "required": true,
        "label": "Email"
    }
]
  1. Load the form schema in a react application
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, ScrollView } from 'react-native';

import Form0 from "react-native-form0";
import { Button } from 'native-base';

export default class App extends Component {

  render() {
    const fields = require("./schema/form0.json");
    return (

      <ScrollView>
        <Form0 fields={fields}
          ref={(c) => {
            this.formGenerator = c;
          }} />

        <View styles={styles.container}>
          <Button onPress={() => console.log(this.formGenerator.getValues())}><Text>Submit</Text></Button>
          <Button onPress={() => this.formGenerator.resetForm()}><Text>Clear Form</Text></Button>
        </View>
      </ScrollView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    backgroundColor: '#F5FCFF',
    justifyContent: 'space-between'
  }
});

Form Properties & Methods

Properties

PropTypeDefaultDescription
fieldsarrayrequiredArray of form fields
errorComponentReact ComponentN/AError display component
formDataobjectN/Aform values

Methods

getValues

Fetch the data from the form.

{
    "data_source" : "test",
    "prospect_name" : "sharath"
}

setValues

Set the values of fields in the form. Ensure that the object setting the values has same name as form field name

{
    "data_source" : "test",
    "prospect_name" : "sharath"
}

resetForm

Reset the form, clears data and errors

setToDefault

Form Fields

Field is the basic unit of the form which generates an UI components. The basic structure of the field is

{
    "type" : "text",
    "name" : "",
    "label": ""
    ... 
}    

Common Properties

The common properties for all the fields are :

PropTypeRequiredDescription
typetext, email, url, password, number, select, switch, dateYesType of field
namestringYesInternal name of the field
labelstringYesDisplay field lable
requiredbooleanNoField is mandatory or not ?
editablebooleanNoField is editable or not?
hiddenbooleanNoField is visible or not?
defaultValueNoSets the default value of the field

Field Types

TextInput

Text input field allows to enter the text. The types of text fields allowed are, text, email, url, password, number, phone_number, currency

Type

{ type: text }

Additional Properties
PropTypeDefaultDescription
iconNamestringN/ASets the icon name from react-native-vector-icons
iconOrientaionstringleftDisplay icon to left or right
propsObjectN/AAdditional properties for the text input
Value Type
  • String (text, email, url, password)
  • Number (number,phone_number,currency)

Picker

Type

{ type: picker }

Additional Properties
PropTypeDefaultRequiredDescription
optionsarrayN/AYesDefines the options
propsobjectN/AYesreact native picker props like {mode : 'dropdown'}
Value Type

String

Default Value Type

String (The string value must be a valid options)

Switch

Implementation of react native switch component

Type

{ type: switch }

Value Type

Boolean

Date

(type: string)

Additional Properties
PropTypeDefaultDescription
modestring (date, time, datetime)dateMode for the date picker
minDatestring (date in format "YYYY-MM-DD", "today", "tomorrow") or JS DateN/AMinimum date in datepicker
maxDatestring (date in format "YYYY-MM-DD", "today", "tomorrow") or JS DateN/AMiximum date in datepicker
Value Type

String

Default Value Type

string (date in format "YYYY-MM-DD", "today", "tomorrow") or JS Date integer(number of minutes to be added/subtracted from current date) e.g. +60 (means 60 minutes forward) -60

Select

Type

{ type: select }

Additional Properties
PropTypeDefaultRequiredDescription
multipleboolfalseNoAllow single or multiple selection
objectTypestringfalseNoMinimum date in datepicker
labelKeystringN/AYes, if Object Type is trueTo define the key which value need to be used as label.
primaryKeystringN/AYes, if Object Type is trueTo define the key which is unique in all objects.
optionsstringN/AYesTo define the key which is unique in all objects.
Value Type
  • Array of Strings
["Option 1", "Option 2, "Option 3"]
  • Array of Objects
"options": [
            {
                "user_id": 1,
                "user_name": "Sharath"
            },
            {
                "user_id": 2,
                "user_name": "Rabindra"
            },
            {
                "user_id": 3,
                "user_name": "Nitheesh"
            }
        ],
Default Value Type
  • String

If the options are array of strings

["Option 1", "Option 3"]
  • Object If options are array of objects
defaultValue : [
    {
        "user_id": 3,
        "user_name": "Nitheesh"
    }
]

Lookup

Type

{ type: lookup }

Lookup is similar to select. Lookup is useful is auto-populating data from the master data. e.g if there is a master data of users, then lookup can be used to select the user and then other reference fields will be auto populated

see the example 10-lookup.json (https://github.com/sharathchandramg/react-native-formo/blob/master/example/sampleformo/schema/10-lookup.json)

Additional Properties
PropTypeDefaultRequiredDescription
multipleboolfalseNoAllow single or multiple selection
objectTypestringfalseNoMinimum date in datepicker
labelKeystringN/AYes, if Object Type is trueTo define the key which value need to be used as label.
primaryKeystringN/AYes, if Object Type is trueTo define the key which is unique in all objects.
optionsstringN/AYesTo define the key which is unique in all objects.
Value Type
  • Array of Strings
["Option 1", "Option 2, "Option 3"]
  • Array of Objects
"options": [
            {
                "user_id": 1,
                "user_name": "Sharath",
                "email" : "sharath@xyx.com",
                "phone" : "1234567890"
            },
            {
                "user_id": 2,
                "user_name": "Rabindra",
                "email" : "rabindra@xyx.com",
                "phone" : "1234567890"
            },
            {
                "user_id": 3,
                "user_name": "Nitheesh",
                "email" : "nitheesh@xyx.com",
                "phone" : "1234567890"
            }
        ],
Default Value Type
  • String

If the options are array of strings

["Option 1", "Option 3"]
  • Object If options are array of objects
defaultValue : [
    {
        "user_id": 3,
        "user_name": "Nitheesh",
        "email" : "nitheesh@xyx.com",
        "phone" : "1234567890"
    }
]
Extended Features
  • Populate the options list from remote API/Storage. We have exposed 'onGetQuery' function, so you can bind your function and update the options list. To achive it you have to configure the 'data_source'.You will able to access the data_source's params and call remote API/Storage. If your data_source type is remote, you have to bind the 'onGetQuery'.function else it is not required. The default type is local,which means options list is pre populated.
"data_source" : {
    "type" : "local/remote",
    "key" : "" ,
    "url": "",
}  
  • Search and Filter We have exposed 'onSearchQuery` function, so if your options list is large and you need to search,bind your function and update the options list. To enable it confure 'additional'. Similary for filter, but for filter you have to configure the 'filterCategory' also. The filterCategory is the list of key from options's object on which filter will be applied.
"additional":{
    "searchEnable":true,
    "filterEnable":true
},

"filterCategory":["user_name","phone"] 

Sub Form

Allows to have multiple nested complex objects. This is useful if we need to record multiple instances of data e.g. see the example 10-lookup.json (https://github.com/sharathchandramg/react-native-formo/blob/master/example/sampleformo/schema/11-sub-form.json)

Type

{ type: sub-form }

Location

Allows to record the geo-location of the device. This requires the user to provide access to the read the location data

Type

{ type: location }

Value Type

Object ({ latitude : "", longitude : "" })

Image

Type

{ type: image }

Image with additional configuration

Allows to configure additional properties like mode, multiple and number of files.The default value for mode, multiple and max_files are low-resolution,false and 1 respectively,

{
"additional_config" : { "mode":"high-resolution", "multiple":true, "max_files": 5 } }

Document

Calculated

Prefill form data (Edit mode)

Pass the data to be prefilled as formData property

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

Add custom validations

0.0.187

16 days ago

0.0.186

20 days ago

0.0.185

3 months ago

0.0.184

6 months ago

0.0.183

6 months ago

0.0.182

7 months ago

0.0.181

1 year ago

0.0.180

1 year ago

0.0.175

1 year ago

0.0.174

2 years ago

0.0.173

2 years ago

0.0.172

2 years ago

0.0.179

1 year ago

0.0.178

1 year ago

0.0.177

1 year ago

0.0.176

1 year ago

0.0.171

2 years ago

0.0.169

2 years ago

0.0.170

2 years ago

0.0.168

2 years ago

0.0.167

2 years ago

0.0.166

2 years ago

0.0.164

2 years ago

0.0.163

2 years ago

0.0.162

2 years ago

0.0.165

2 years ago

0.0.161

2 years ago

0.0.160

2 years ago

0.0.159

2 years ago

0.0.158

2 years ago

0.0.157

2 years ago

0.0.156

2 years ago

0.0.155

2 years ago

0.0.154

3 years ago

0.0.153

3 years ago

0.0.152

3 years ago

0.0.151

3 years ago

0.0.150

3 years ago

0.0.149

3 years ago

0.0.148

3 years ago

0.0.147

3 years ago

0.0.146

3 years ago

0.0.142

3 years ago

0.0.145

3 years ago

0.0.144

3 years ago

0.0.143

3 years ago

0.0.141

3 years ago

0.0.140

3 years ago

0.0.139

3 years ago

0.0.138

3 years ago

0.0.137

3 years ago

0.0.136

3 years ago

0.0.135

3 years ago

0.0.134

3 years ago

0.0.133

3 years ago

0.0.132

3 years ago

0.0.131

3 years ago

0.0.130

3 years ago

0.0.129

3 years ago

0.0.128

3 years ago

0.0.127

3 years ago

0.0.126

3 years ago

0.0.125

3 years ago

0.0.124

3 years ago

0.0.123

3 years ago

0.0.122

3 years ago

0.0.121

3 years ago

0.0.120

3 years ago

0.0.119

3 years ago

0.0.118

3 years ago

0.0.117

3 years ago

0.0.116

4 years ago

0.0.115

4 years ago

0.0.114

4 years ago

0.0.113

4 years ago

0.0.112

4 years ago

0.0.111

4 years ago

0.0.110

4 years ago

0.0.109

4 years ago

0.0.108

4 years ago

0.0.107

4 years ago

0.0.106

4 years ago

0.0.105

4 years ago

0.0.104

4 years ago

0.0.103

4 years ago

0.0.102

4 years ago

0.0.101

4 years ago

0.0.100

4 years ago

0.0.99

4 years ago

0.0.98

4 years ago

0.0.97

4 years ago

0.0.96

4 years ago

0.0.95

4 years ago

0.0.94

5 years ago

0.0.93

5 years ago

0.0.92

5 years ago

0.0.91

5 years ago

0.0.90

5 years ago

0.0.89

5 years ago

0.0.88

5 years ago

0.0.87

5 years ago

0.0.86

5 years ago

0.0.85

5 years ago

0.0.84

5 years ago

0.0.83

5 years ago

0.0.82

5 years ago

0.0.81

5 years ago

0.0.80

5 years ago

0.0.79

5 years ago

0.0.78

5 years ago

0.0.77

5 years ago

0.0.76

5 years ago

0.0.75

5 years ago

0.0.74

5 years ago

0.0.73

5 years ago

0.0.72

5 years ago

0.0.71

5 years ago

0.0.70

5 years ago

0.0.69

5 years ago

0.0.68

5 years ago

0.0.67

5 years ago

0.0.66

5 years ago

0.0.65

5 years ago

0.0.64

5 years ago

0.0.63

5 years ago

0.0.62

5 years ago

0.0.61

5 years ago

0.0.60

5 years ago

0.0.59

5 years ago

0.0.58

5 years ago

0.0.57

5 years ago

0.0.56

5 years ago

0.0.55

5 years ago

0.0.54

5 years ago

0.0.53

5 years ago

0.0.52

5 years ago

0.0.51

5 years ago

0.0.50

5 years ago

0.0.49

5 years ago

0.0.48

5 years ago

0.0.47

5 years ago

0.0.46

5 years ago

0.0.45

5 years ago

0.0.44

5 years ago

0.0.43

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago