1.0.2 • Published 7 years ago

wonder-form v1.0.2

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

WonderForm is a really lightweight form building and validation component for React.

Features

  • No built in styles to overwrite
  • Comes with validation for common inputs like date, email, etc
  • Easily add custom validations or overwrite default validation

Demo

Table of Contents

Installation

$ npm install wonder-form

Example

import React, {Component} from 'react'
import {render} from 'react-dom'
import { WonderForm, WonderField } from 'wonder-form'

class Demo extends Component {
  onSubmit(formContents) {
    console.log(formContents)
  }

  render() {
    return (
      <div>
        <WonderForm onSubmit={this.onSubmit.bind(this)}>
          <WonderField name='fullname' type='fullname' required errorMessage={"Error"} />
          <WonderField name='email' type='email' required errorMessage={"Error"} />
          <WonderField name='date' type='date' minDate='4/25/2017' errorMessage={"Error"} />
          <WonderField name='password' type='password' minLength={6} errorMessage={"Error"}/>
          <WonderField name='submit' type='submit' text='Create Account' />
        </WonderForm>
      </div>
    )
  }
}

render(<Demo/>, document.querySelector('#demo'))

Components

WonderField

You can import two components, a WonderForm and WonderField. WonderFields are nested inside WonderForms.

import { WonderForm, WonderField } from 'wonder-form'

WonderField props:

proptypedescription
*namestringMust be a unique identifier for a field
*typestringMust be one of the following: 'text', 'email', 'password', 'date', 'fullname', 'submit', 'custom'
placeholderstringA normal placeholder
classNamestringA class for the WonderField
errorMessagestringAn errorMessage to be displayed
labelstringA label that goes above the input
requiredboolMakes the field required
minDatestringMust be of the format MM/DD/YYYY.
maxDatestringMust be of the format MM/DD/YYYY.
maxLengthnumberMinimum length of the field.
minLengthnumberMaximum length of the field
buttonTextstringFor the submit type WonderField

The following table shows which validation props will work for a certain type of input. All other props will be ignored or throw an error.

typeprops
fullnamerequired, minLength, maxLength
textrequired, minLength, maxLength
passwordrequired, minLength, maxLength
daterequired, minDate, maxDate
emailrequired
submitbuttonText

Creating a custom WonderField

A custom test must return undefined or an object like { type: 'errorName'}

  customTest(input, props) {
    let error;
    if (input !== 'custard') {
      error = {type: 'custard'};
    }
    return error;
  }
  <WonderField name='custom' type='custom' errorMessage='Error' test={this.customTest.bind(this)}/>

Styling a WonderField

A WonderField is composed of a label, input, and span. Pass in a class for the prop 'className' in a WonderField.

  <WonderField className="custom-class"></WonderField>
.custom-class {}
.custom-class label {}
.custom-class input {}
.custom-class span {}

WonderForm

WonderForm props:

proptypedescription
childrenArray of Objects or an ObjectShould be a list of WonderFields
onSubmitfunctionCalled whenever the WonderField with type='submit' is clicked. The field values and errors if there are any are passed in
onSuccessfunctionCalled when there are no errors in any field in the WonderForm
onErrorfunctionCalled if there are any errors. The field values with errors are passed in.

Scripts

  • $ npm run test Runs the test suite
1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago