react-native-dynamic-form
Dynamic form builder for React Native
Sneak Peak
Installation
$ npm install react-native-dynamic-form --save
or use yarn
$ yarn add react-native-dynamic-form
Usage
Note: Ensure to add and configure react-native-vector-icons in your project before using this package.
You can clone and try out the sample app.
Sample usage:
;;;const form =key: 'hdghhdbdfgh'type: 'header'subtype: 'h1'label: 'Dynamic Form';{return<View style=stylescontainer><DynamicFormform=formstyle=stylesformContainer/></View>;}const styles = StyleSheet;
Props
The component takes one compulsory prop - form
. Other props are optional. The table below explains more.
Prop | Required | Type | Purpose |
---|---|---|---|
form | Yes | Array | array of objects representing form components to render (see Form Components for more info) |
theme | No | Object | Theme to apply to entire dynamic form elements. See below for more info |
style | No | Object, Number | Style to apply to form container. View Style |
onFormDataChange | No | Function | Returns form responses as funtion argument whenever any change occur in form |
submitButton | No | Object | Object for displaying button at the end of form. More info below |
submitButton (prop)
Object for displaying button at the end of form. Holds configuration on button appeareance and beahaviour. See below for sample:
;;;const theme = ;{return<View style=stylescontainer><DynamicFormform=formtheme=theme // theme prop is optional and default theme is applied if theme is not providedsubmitButton={console;} // button action, takes form responses as argument, optionallabel: 'Submit' // button label, requiredbuttonStyle:backgroundColor: '#CCCCCC'borderRadius: 3height: 40// View PropTypes, optionalbuttonTextStyle:fontSiz3: 18// Text PropTypes, optionaldisabled: false // boolean to disable button, optional/></View>;}
_getFormResponses
Use component reference to get form responses at any point in time
;;;{const responses = thisformRef;// use responses here...}{return<View style=stylescontainer><DynamicFormref= thisformRef = refform=form/><ButtononPress=thisgetFormResponsestitle="Get Form Responses"/></View>;}
Theming
A global theme object can be passed to the Dynamic Form
component which gets applied to most of the form elements. Theming is still a work in progress and far from perfect, but it provides a basic way of customizing form elements. Theming instructions are provided below:
;;;const theme = ;{return<View style=stylescontainer><DynamicFormform=formtheme=theme // theme prop is optional and default theme is applied if theme is not provided/></View>;}
The code sample above builds a theme template and apply defaults configuration. This file conatins the default values. The defaultTheme
export in the styles config can be used as a template to build really customized theme object.
Full customization
To fully customize form elements, the buildTheme
export accepts three parameters in the order they appear below:
-
userColors - Object of colors to apply to form element. See file for exact format.
-
userFonts - Object of fonts to apply to form element. See file for exact format.
-
userTheme - Complete user theme object which is a clone of
defaultTheme
export in styles config file. This can be copied over and modified.
Below is an example of a fully customized theme builder
;const myColors =primary: '#00a5ff'textPrimary: '#2A3C53'primaryDark: '#0077cb'error: '#FF6565'iconDark: 'rgba(0,0,0,0.4)'textInputBorderColor: '#cac8c8'placeholderTextColor: '#A9A9A9'starFillColor: '#f5a623'black: '#000000'white: '#FFFFFF'success: '#50e3c2';const myFonts =defaultFontFamily: 'Roboto' // font should already be added to project;const myTheme =// labelslabel:marginTop: 10fontSize: 14color: myColorstextPrimary// errorerror:fontSize: 12color: myColorserror// headersheaders:h1:fontSize: 24color: myColorstextPrimaryfontFamily: myFontsdefaultFontFamilyh2:fontSize: 20color: myColorstextPrimaryfontFamily: myFontsdefaultFontFamilyh3:fontSize: 16color: myColorstextPrimaryfontFamily: myFontsdefaultFontFamily// paragraphp:fontSize: 16color: myColorstextPrimaryfontFamily: myFontsdefaultFontFamily// inputinput:placeholderTextColoriconColor: iconDarkstyle: {}// ratingrating:starFillColorremarkStyle:color: myColorsstarFillColorfontSize: 14// toggletoggle:knobColor: myColorsprimaryDarktintColor: myColorsprimary// selectselect:tagRemoveIconColor: myColorserrortagBorderColor: myColorstextInputBorderColortagTextColor: myColorsprimaryselectedItemTextColor: myColorsprimaryselectedItemIconColor: myColorsprimaryitemTextColor: myColorstextPrimarysubmitButtonColor: myColorssuccess;const theme = ;// The most important paramater is the colors parameter which will be applied to all form elements.// Other sections of the theme object will be set to their default values and provided colors applied where necessary// Most times you will only have to do as below and leave the remaining configuration in their default stateconst theme = ;
Form Components
All component fields are required, except stated otherwise.
Header
Represents a header component.
key: 'hdghhdbdfgh'type: 'header'subtype: 'h2' // one of h1, h2 and h3label: 'Dynamic Form'style:fontSize: 14// optional
Paragraph
Represents a paragraph component.
key: 'addsdfdvdvdd'type: 'paragraph'label: 'Instructions on how to fill dynamic form'style:fontSize: 15// optional
TextInput
Represents an input component.
key: 'manshgdsuudfg'type: "text"required: true // optionallabel: 'What is your last name?'placeholder: 'Last Name' // optionalsubtype: 'text' // one of text, tel, email and passwordmaxlength: 30 // optionalvalue: 'Salako' // optionaldisabled: false // optional,icon: 'lock' // optional{// do validation here and return bool status} // optional
TextArea
Represents a multi-line input component.
key: 'jahaughabdvad'type: 'textarea'label: 'Please describe yourself in not more than 400 characters'placeholder: "My name is John Doe and I am..." // optionalmaxlength: 400 // optionalrequired: true // optionalvalue: '' // optional{// do validation here and return bool status} // optional
Rating
Represents a rating component.
key: 'liaksunshdfjnbah'type: 'starRating'label: 'Rate your programming skill'maxStars: 5value: 3required: false // optionalconfig:iconSet: 'MaterialIcons' // react-native-vector-icon icon setsemptyStar: 'star-border' // empty star iconfullStar: 'star' // full star iconhalfStar: 'star-half' // half star icon, if enableHalfStar is set to trueenableHalfStar: false // enable half starratingRemark:1: 'Beginer'2: 'Enthusiast'3: 'Junior'4: 'Intermediate'5: 'Senior'
Radio Button
Represents a radio button component.
key: 'sbasgdsbdgffgf'type: 'radio-group'label: 'Favorite Programming Language'other: true // displays an other input for custom data entry, optionalvalues:label: 'JavaScript'value: 'javascript'selected: true // selected value (can be used to preselect values too) optionallabel: 'Ruby'value: 'ruby'
CheckBox
Represents a checkbox component.
key: 'avfsragsghgdbhfg'type: 'checkbox-group'label: 'Top 3 tech companies in the world'other: true // optionalvalues:label: 'Google'value: 'google'label: 'Apple'value: 'apple'label: 'Facebook'value: 'facebook'selected: truelabel: 'Microsoft'value: 'microsoft'label: 'Amazon'value: 'amazon'
Toggle
Represents a toggle component.
key: 'anhsgabgbfnhhdnbf'type: 'checkbox-group'label: 'Sorting Algorithms familiar with'toggle: true // renders a toggle instead of checkbox, optionalother: true // renders an other field for free text entryvalues:label: 'Bubble Sort'value: 'bubble sort'selected: true // selected value (can be used to preselect values too) optionallabel: 'Heapsort'value: 'heapsort'label: 'Insertion Sort'value: 'insertion sort'label: 'Merge Sort'value: 'merge sort'label: 'Quicksort'value: 'quicksort'
Date
Represents a date component.
key: 'aabnstfavahbdaas'type: 'date'label: 'Date of Birth'value: '26-11-2018'placeholder: '25-05-2018' // optionaldateFormat: 'DD-MM-YYYY' // optionaldisabled: false // optional
Number Input
Represents a number input component.
key: 'manbsgvagsdbdhh'type: 'number'label: 'Number of Data Structure and Algorithm books read'placeholder: 0 // optionalvalue: 0min: 0 // minimum allowed value, optionalmax: 100 // maximum allowed value, optionalstep: 2 // value step, optional, defaults to 1disabled: false // optionaldirectTextEdit: false // enable or disable free form data entry, optional, defaults to false
Select
Represents a select component.
key: 'nabsgsgdhyshdhf'type: 'select'label: 'Languages Spoken'multiple: false // enable multiple selection and displays selected items as tags, optionalsearchInputPlaceholder: 'Search Languages...'values:label: 'Yoruba'value: 'yoruba'selected: true // selected value (can be used to preselect values too) optionallabel: 'Igbo'value: 'igbo'label: 'Hausa'value: 'hausa'label: 'English'value: 'english'label: 'Spanish'value: 'spanish'label: 'French'value: 'french'
Contributing
Contributions are welcome and will be fully credited.
Contributions are accepted via Pull Requests on Github.
Pull Requests
-
Document any change in behaviour - Make sure the
README.md
and any other relevant documentation are kept up-to-date. -
Consider our release cycle - We try to follow SemVer v2.0.0. Randomly breaking public APIs is not an option.
-
Create feature branches - Don't ask us to pull from your master branch.
-
One pull request per feature - If you want to do more than one thing, send multiple pull requests.
-
Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
Issues
Check issues for current issues.
Contributors
License
The MIT License (MIT). Please see LICENSE for more information.