Skip to content

nbwar/ICFormable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ICFormable - React Forms

ICFormable provides a consistent way to create forms, inputs, and validations using React.

Features

  • Function and flexible validation using Validator.js (https://github.com/chriso/validator.js) or custom regex
  • Flexibility to build custom inputs
  • onSubmit callback with form model serialized. No more “e.target.value” for every input!
  • Pass “serverErrors” to the form that will map to each component appropriately

How to use

install

npm install ic-formable

Components

Form

The base component to wrap all inputs types in to inherit ICFormable functionality.

Props:

  • onSubmit - Submit callback that will provide model object w/ form values with key being “name” attribute
  • serverErrors - marks errors on each input based on the input's “name” attribute
  • formProps - Props to pass to html element
import { Form }     from 'icformable'
import ICTextField from 'store_app/components/shared/ICFormable/ICTextField'

class SomeComponent extends React.Component {
  state = {
    serverErrors: null
  }

  handleFormSubmit = (model) => {
    console.log(model)
  }

  render() {
    const formProps = {} // HTML form props

    return (
      <Form
        onSubmit={this.handleFormSubmit}
        serverErrors={this.state.serverErrors}
        formProps={formProps}
      >
        <ICTextField
          floatingLabelText="Email"
          name="email"
          hintText="[email protected]"
          validations={{isEmail: null, isLength: {min: 3, max: 15}}}
          validationErrorText="Sorry, please enter a valid email."
          required
        />
        <button type="submit"> Submit </button>
      </Form>
    )
  }
}

export default SomeComponent

Build Custom Components

FormComponent

In order to build custom inputs you'll want to use FormComponent as a Higher Order Component to inherit validation and form functionality.

Props:

  • name (required) - the key for the form model
  • disabled - to disable the input
  • required - to mark the field as a required field
  • regexValidation - a regex string to validate via regex
  • validations - Validator.js (https://github.com/chriso/validator.js) validations use syntax: {validatorMethod: arguments}
import { FormComponent } from 'icformable'

@FormComponent
class ICCustomInput extends React.Component {
  static propTypes = {
    name    : React.PropTypes.string.isRequired,
    disabled: React.PropTypes.bool,
    required: React.PropTypes.bool,
  }

  render() {
    return (
      <div>
        <input
          name={this.props.name}
          disabled={this.props.disabled}
          required={this.props.required}
        />
      </div>
    )
  }
}

export default ICCustomInput

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published