1.0.26 • Published 3 years ago

rn-formly v1.0.26

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

Note:

Typescript Suppoerted

Create beautiful React-Native forms within minutes with validator, progress-bar, custom error handling by passing just JSON

If you find this repo useful, please give it a star on github

Status
DependenciesDependencies Dev dependencies Peer dependencies
Packagenpm package version npm downloads
LicenseGitHub

⭐ Features

  • 10+ fully customisable component ready to be used by passing a json (including support for loading data asynchronously)
  • Can easily manage Safe Area for devices with notch
  • Ingerated with optional progress bar to show user their progress while filling up a form
  • Allows throwing custom error and data validation
  • Includes loading component
  • Instant support for feature and issue using git issue tracker

Index

Screenshots

Installation

RN-Formly use following as peerDependency 1. react-native-device-info 2. react-native-image-crop-picker

npm i react-native-image-crop-picker react-native-device-info rn-formly --save

For iOS, If you are using React Native => 0.60 then simple do

pod install 

else follow this guide

Setting up image crop picker

IOS setup

Image Crop picker requires you to have relevant permission to access camera and storage for both iOS and Android

  1. Go to info.plist and add this
<plist version="1.0">
  <dict>
    ...
    <key>NSPhotoLibraryUsageDescription</key>
    <string>$(PRODUCT_NAME) would like access to your photo gallery</string>
    <key>NSCameraUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your camera</string>
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to save photos to your photo gallery</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your microphone (for videos)</string>
  </dict>
</plist>

You refer to following documentation for more information (and probably for integration in complex project)

or look into this commit

Android Setup

Add following lines in your android/app/src/main/AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

look at this commit

If this does not work, refer to following instructions.

  1. For Image Croper
  2. For React Native Device Info

Usage

To get started/or a try it out, you can clone or check out the following repo

Minimilistic Setup Example

import SignupFormComponent from 'rn-formly';


const inputFields = [
  {
    key: 'name', 
    type: 'text', 
    label: `Your Full Name`,
    required: true,
    helper: 'Using your real name would make it more likely for you to get a match',
    templateOptions: { 
      componentProps: {
        placeholder: 'Frank Murphy'
      }
    }
  }, 
  {
    key: 'otp', 
    type: 'otp', 
    label: 'Enter OTP',
    helper: '(optional) but try to enter incorrect date', 
    templateOptions: {
      noOfTextInput: 5,
    }
  }]
  
  const App: () => React$Node = () => {

  const justLogDataFromForms = (index, key, currentValue,  payload) => {
     console.log('Logging from Parent log')
    console.log(index, key, currentValue, payload)
  }

  return (
    <>
      <SignupFormComponent 
      inputFields={inputFields}
      globalButtonText={'Next'}
      onButtonClick={justLogDataFromForms}
      defaultColor={'green'}
      progressBarProps={{
        blink: false
      }}
      /> 
    </>
  );
};

Example usage of all the component can be seen using this file

Required Props

SNOPropTypeDefaultRequireddescription
1.inputFieldsArrayNopeYesClick here
2.onButtonClickAsync functionNopeYesPassed function receives index of the element in array, key associated with it and the complete payload, This is triggered after user have clicked on the Next button, and before the iteration happens, you can throw errors here to prevent from incrementing to the text state

This is how error handling is done behind the scene: if (error.message) setErrorData({status: true, message: error.message})

Optional Props

Important Props are highlighted.

General Optional Props

SNOPropTypeDefaultRequireddescription
1.textStyleTextStylenoneNoHeading text style (style for your label key in inputFields props)
2.progressBarProgressBarPropsblink: falseNoclick here to know more about ProgressBar Component
3.helperTextStyleTextStylenoneNoHelper text style (style for your helper key in inputFields )
4.backgroundViewColorstring'white'Nobackground for the view of your form
5.defaultColorstring'black'noFormats button color, text color and sub component color accordingly (can be override by passing styles for individual component)
6.onFinishfunctionnoneNoFunction which is triggered after user have itterated over all the elements in the passed inputFields
7.backIconStyleTextStylenoneNoStyling for the back button <
8.startingIndexnumber0 (inital number of your inputfieldsnowhich object to start with your input fields array

Button Optional props

Validation and Error throwing are done in inputFields array. See inputFields section to know more

SNOPropTypeDefaultRequireddescription
1.buttonSelectedStyleViewStylenoneNoStyling of the button when a user is allowed to move to the next field
2.buttonNotSelectedStyleViewStylenoneNostyling of the button when a user is not allowed to move to the next field
3.buttonSelectedTextStyleTextStylenoneNostyling of the text inside the button when a user is allowed to move to the next field
4.buttonNotSelectedTextStyleTextStylenonenostyling of the text inside the button when a user is not allowed to move to the next field
5.globalButtonTextstring"Next"nobutton Text to move to next screen (can be overwritten by passing button text in inputArray field object

Error Props

Validation and Error throwing is done in the inputFields array. See inputFeilds section to know more.

SNOPropTypeDefaultRequireddescription
1.defaultErrorMessagestring'Sorry Something went wrong'NoError message for user when error occurs but the error does not contain a message for failure
2.errorStylestylesheet objectnoneNoText Styling for error message

Note

  1. for stylesheet object type props. you need to pass props like this
const styles = StyleSheet.create({
  someStyle: {
    ....
    ....
  }, 
})

and then pass styles.someStyle in props

  1. For stylesheet object, Default None does not mean that element won't have any styles; the props provided by the user would override the default styling.

inputFields Props

inputFields Props supports most of the popular components you would need in a form. If we are missing any component, create an issue, and we should be able to add within 2-4 days.

Currently Supported Component

  1. Text
  2. dataTyper
  3. checkboxes
  4. picker
  5. image,
  6. images
  7. AutoComplete
  8. otp

Structuring your Array Object

  {
    key: 'name', 
    type: 'text', 
    label: 'Your Full Name',
    helper: 'Using your real name would make it more likely for you to get a match',
    templateOptions: { 
      componentProps: {
        placeholder: 'Frank Murphy'
      },
      templateStyle: styles.textStyle // refer to the style component
    }
  }

Global Properties for each object

S.NOkeytypedescriptionRequired
1.keystringUnique key to identify the objectYes
2.typestringtype of component you want to use (should be one of the above)Yes
3.labelstringHeading for the fieldYes
4.helperstringHelper for the fieldNo
5.templateOptionsobjectprops for specific componentsNo
6.Validatorfunctionsometimes you want to do validation in the real-time and not after the next button is clicked, in this case, pass the validator function. A validator function would receive the following data in its argument index, key, currentData, payloadNo
7.requiredBooleanif required: true the field cannot be empty i.e. button to iterate to the next component will be disabledno
8.buttonTextstringpassing buttonText in your key object will overwrite your globalText for that particular screen onlyno
3.defaultValue(based on component)this is default value (usually the value user have entered before)no

Text Component

{
    key: 'name', 
    type: 'text', 
    label: 'Your Full Name',
    helper: 'Using your real name would make it more likely for you to get a match',
    templateOptions: { 
      componentProps: {
        placeholder: 'Frank Murphy'
      },
      templateStyle: styles.textStyle // refer to the style component
    }
  }

Component level Props are mostly passed in templateOptions

Default Value type: string

TemplateOptions keys with value
S.NOkeytypedescriptionRequired
1.componentPropsobjectCan pass all the props for TextInputno
2.templateStylestyle objectstying for text inputno

dateTyper

  {
    key: 'dob', 
    type: 'dateTyper', //change this to Dob component
    label: 'Your Date of birth',
    helper: 'Your Birthdate will help us in connecting you with people of similar age',
    required: true
  }, 
TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.dateFormatstringshould be either DDMMYYYY, or MMDDYYYY or YYYYMMDDNo
2.componentPropsNextInputPropsRead more about next input dependency which we use hereno

Default Value type: Array of strings or string

checkboxes

  {
    key: "gender", 
    type: 'checkboxes',
    label: 'Select your Gender', 
    helper: '(Optional), Please select a gender', 
    templateOptions: {
      options: [
  {
  key: 'male',
  label: 'Male',
  value: false 
  },
  {
    key: 'female', 
    label: 'Female',
    value: false
  },
  {
    key: 'others', 
    label: 'Others',
    value: false
  }
]
    }
  }, 
TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.optionsArrayshould contain key, label and valueYesNone

image (single image)

  {
    key: 'image',
    type:'image', 
    label: "Upload your cover image", 
    helper: 'You can change your profile pic anytime from settings',
    templateOptions: {
     cropHeight: 200,
     cropWidth: 300
    }
  },

defaultValue type: ImageType;

TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.cropWidthnumberwidth of the image user is allowed to crop/selectnonone
2.cropHeightnumberheoght of the image user is allowed to crop/selectnonone
3.imageComponentStyleImageStylestyling your image componentno

images

  {
    key: 'image',
    type:'images', 
    label: "Upload your cover image", 
    helper: 'You can change your profile pic anytime from settings',
    templateOptions: {
     cropHeight: 200,
     cropWidth: 300
    }
  },

defaultValue type: Array of ImageType;

TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.cropWidthnumberwidth of the images user is allowed to crop/selectnonone
2.cropHeightnumberheoght of the images user is allowed to crop/selectnonone

OTP

   {
    key: 'otp', 
    type: 'otp', 
    label: 'Enter OTP',
    helper: '(optional) but try to enter incorrect date', 
    templateOptions: {
      noOfTextInput: 5,
    }
  }, 
TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.noOfTextInputnumbernumber of character in your otpyesnone
2.componentPropsNextInputPropsRead more about next input dependency which we use hereno

AutoComplete

{
    key: 'autoComplete', 
    type: 'autoComplete', 
    label: 'Type Programming language',
    helper: "This is an example of Auto Complete Component, Don't try to make sense out of the returned data",
    templateOptions: { 
      asyncFunction: async function (text) {
        return async data
      }
    }
  }

Where data should be object Array containg id, value, title

 [{
    { 
      id: 1,
      title: "Javascript",
      value: "javascript"
    },
    { 
      id: 2,
      title: "Java",
      value: "java"
  },
}]

Click here to see the example da

TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.componentPropsNextInputPropsRead more about next input dependency which we use hereno
2.asyncFunctionasync functionthis function will recive the input text and should return suggestions accordingly (via making some api call etc)yesnone
3.loaderRequiredBooleanshows loader until user recieves the data from async functionnofalse
4.listViewStylestyling objectstyling for the list list viewnonope
5.listTextStylestyling objectstyling for the text in the list viewnonope
6.textInputStyleTextStyleObjectInput box text stylenonope
7.debouncingEnablebooleanenable debouncing to fetch datanotrue
8.debouncingTimenumberdebouncing time to make request500false

Picker

  {
    key: "gender", 
    type: 'picker',
    label: 'Select your Gender', 
    helper: '(Optional), Please select a gender', 
    templateOptions: {
      options: [
  {
  key: 'male',
  label: 'Male',
  value: false 
  },
  {
    key: 'female', 
    label: 'Female',
    value: false
  },
  {
    key: 'others', 
    label: 'Others',
    value: false
  }
]
    }
  }, 
TemplateOptions key/value
S.NOkeytypedescriptionRequiredDefault
1.optionsArrayshould contain key, label and valueYesNone
1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.4

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.3

4 years ago