2.0.0 • Published 6 years ago

react-form-utils v2.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

react-form-utils

Build Status

react-form-utils provides helper functions to retrieve form props and form values

import {inputPropsLookup, inputValueLookup, formSerialize} from 'react-form-utils';

inputPropsLookup(fieldsProps, fieldName)

ArgumentTypeExample
fieldsPropsobject{name: {type: 'text'}, email: {type: 'email'}}
fieldNamestringname, email[0], email[1]

returns

object: props, e.g. {type: 'text'}

e.g.

const fieldsProps = {
  name: { type: 'text' },
  age_group: {
    type: 'select',
    size: 4,
    options: []
  }
  address: {
    type: 'nested',
    fields: {
      street: {
        type: 'text'
      }
    }
  }  
};
const fieldName = 'address[0][street]';

const props = inputPropsLookup(fieldsProps, fieldName);
// <input name={fieldName} {...props} />

formSerialize(form)

formSerialize(form)  
ArgumentTypeExample
formDOMNodeform element

returns

object. serialized, e.g. {type: 'text'}

e.g.

// <form><input name="message" value="hello"/></form>

const values = formSerialize(this.form);
// { message: 'hello' }

inputValueLookup(values, fieldName)

ArgumentTypeExample
valuesobject{address: [ {zip: '55555'} ]}
fieldNamestringaddress[0][zip]

returns

any. input value, e.g. {type: 'text'}

e.g.

const values = {
  name: 'Ali',
  age_group: '31-40'
  address: {
    0: {
      street: '13 Calle 49a'
     }
};
const fieldName = 'address[0][street]';

const value = inputValueLookup(values, fieldName);

// <input name={fieldName} value={value} />