1.0.0 • Published 7 years ago

form_builder v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
7 years ago

#Form Builder

Is a component that generates a form using predefined input elements. The input elements available are:

##Form Builder Items

Form builder for line items. Some usage examples would be purchase orders, work orders and invoices where there is a main table and an items table related to it(hasMany relationships)

TODOS

  • Unit tests (Jest)
  • Continuous Integration (Circle Ci)
  • Extend for custom inputs
  • Documentation for FormBuilderItems
  • Add examples
  • Webpack tests

###Props

It receives the following props:

PropDefault ValueDescription
valueEmpty Stringvalue of the controlled input
labelEmpty Stringlabel of the input
labelOnFalse Booleanflag to show label
nameEmpty Stringhtml name attribute
labelClassesEmpty Stringclasses of label tag
inputContainerClassesEmpty Stringclasses of div containing input
onChangeCallback Functioncallback that modifies value
disabledFalse Booleanflag that disables input interaction
optionsEmpty Arrayon select inputs, array of all options that can be selected
multipleFalse Booleanon select inputs, enables multiple selection
placeholderEmpty Stringplaceholder html attribute
inputAttributesEmpty Objectkey-value pairs of input attributes (e.g placeholder, type, number)

###Form Configuration File

This is an array of fields and its props that will be passed FormBuilder.

Fields array example:

const fields = [
  {
    name: 'code',
    elt: Inputs.TextInput,
    label: 'Code',
    labelOn: true,
    labelClasses: 'col-sm-3',
    inputContainerClasses: 'col-sm-6'
    inputAttributes: {
      type: 'number',
      step: 0.1
    }
  },
  {
    name: 'description',
    elt: Inputs.TextArea,
    label: 'Description',
    labelOn: true,
    labelClasses: 'col-sm-3',
    inputContainerClasses: 'col-sm-6'
  }
];

####FormBuilder with React:

import React from 'react';
import {render} from 'react-dom'
import update from 'immutability-helper';
import FormBuilder from 'form_builder';

class Item extends React.Component{
    constructor(props){
        this.state = {
            form: {
                code: '',
                description: ''
            } // must have a form key
        }
    }

    onChange(field, value){
        this.setState({
          header: update(this.state.form,{
            $merge:{
              [field]: value
            }
          })
        });
    }

    render(){
        return (
            <FormBuilder
                fields={fields}
                form={this.state}
                formClasses='form-horizontal'
                onChange={this.onChange.bind(this)}
            />
        )
    }
}

render(
  <Item/>,
  document.getElementById('root')
)

####FormBuilder with React and Redux:

import React from 'react';
import {render} from 'react-dom'
import {Provider, connect} from 'react-redux';
import {bindActionCreators, createStore} from 'redux'
import * as itemActions from './actions/item_actions.js'
import rootReducer from './reducers/index.js';
import FormBuilder from 'form_builder';

// Set up form
const FormBuilderRedux = connect(
  state => state, 
  dispatch => ({actions: bindActionCreators(itemActions, dispatch)})
)(FormBuilder);

class Item extends React.Component{
    render(){
      return(
        <FormBuilderRedux
            fields={fields}
            formName='item'
            formClasses='form-horizontal'
            onChange='update'
        />
      )
    }
}

// Configure Store
const store = createStore(
    rootReducer,
);

//Render Component
render(
  <Provider store={store}>
    <Items/>
  </Provider>, 
  document.getElementById('root')
);

Item Actions (item_actions.js)

export const update = (name, value) =>({
  type: 'FORM_UPDATE_VALUE',
  name, 
  value
});

Reducer (item_reducer.js)

const initialState={
    item:{
        form:{
            code: '',
            description: ''
        }
    }
};

const item = (state=initialState.item, action) => {
  switch(action.type){
    case 'FORM_UPDATE_VALUE':
      return {
        ...state,
        form: {
            ...state.form
            [action.name]: action.value
        }
      }

    default:
      return state;
  }
}

import {combineReducers} from 'redux';

const rootReducer = combineReducers({
  item
})

export default rootReducer;

Redux: dispatch -> action -> reducer -> update state