jameslnewell / redux-formo

An alternate forms framework for Redux+React.
8 stars 3 forks source link

redux-formo

Build Status

An alternate forms framework for Redux+React.

Why not redux-form?

Installation

npm install --save redux-formo

Usage

createStore.js

import {createStore, applyMiddleware, combineReducers} from 'redux';
import thunk from 'redux-thunk';
import {reducer} from 'redux-formo';

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
export createStoreWithMiddleware(combineReducers({
  form: reducer
}));

App.jsx

import React from 'react';
import form from 'redux-formo';
import filter from './filter';
import validate from './validate';
import submit from './submit';

class App extends React.Component {

  render() {
    const {
      valid,
      submitting,
      submitted,
      error,
      fields: {name, phone},
      onSubmit
    } = this.props;

    return (
      <form onSubmit={onSubmit(submit)}>

        {error && <p>{error}</p>}

        <div>
          <label>
            Name: <input {...name}/>
          </label>
          {name.error && <p>{name.error}</p>}
        </div>

        <br/>

        <div>
          <label>
            Phone: <input {...phone}/>
          </label>
          {phone.error && <p>{phone.error}</p>}
        </div>

        <br/>

        <input
          type="submit"
          value={submitted ? 'Saved.' : (submitting ? 'Saving...' : 'Save')}
          disabled={submitting || submitted}
        />

      </form>
    );
  }

}

export default form({
  name: 'personal-details',
  fields: ['name', 'phone'],
  filter, validate
})(App);

Methods

form(config : Object, [mapStateToProps], [mapDispatchToProps], [mergeProps], [options])(Component)

Decorate a React Component, connecting the Redux form state and providing helper methods for handling form and field events e.g. onSubmit(), onChange(), onBlur().

config : Object

.name : String

The name of the form.

Required.

.fields : Array

The names of the fields.

Required.

.defaults : Array

The default values of the fields.

Optional.

.filter({field, value, values}) : String | Promise

A function used to filter a field value.

Optional.

Parameters:

Returns:

The filtered value.

.validate({field, value, values}) : Boolean | String | Promise

A function used to validate a field value.

Optional.

Parameters:

Returns:

True if the value is valid. An error string if the value is not valid.

.submit({dispatch, values}) : void | FSA | Promise

A function used to submit the field values.

Optional.

Parameters:

Returns:

Void, a Flux Standard Action or a Promise.

.filterOnChange : Boolean

Whether a field should be filtered when the field value changes.

Optional. Defaults to false.

.validateOnChange : Boolean

Whether a field should be validated when the field value changes.

Optional. Defaults to false.

.filterOnBlur : Boolean

Whether a field should be filtered when the field loses focus.

Optional. Defaults to true.

.validateOnBlur : Boolean

Whether a field should be validated when the field loses focus.

Optional. Defaults to true.

.filterOnSubmit : Boolean

Whether a field should be filtered when the form is submitted.

Optional. Defaults to true.

.validateOnSubmit : Boolean

Whether a field should be validated when the form is submitted.

Optional. Defaults to true.

.stateKey : String

The name of the property where the form reducer is mounted on the state.

Optional. Defaults to form.

.propKey : String

The name of the property where the form state is passed in props to the react component.

Optional. Defaults to none.

.destroyOnUnmount : Boolean

Whether the form state is destroyed when the component is unmounted.

Optional. Defaults to true.

mapStateToProps(state, ownProps) : Object

See the react-redux documentation on the connect function.

Optional.

mapDispatchToProps(dispatch, ownProps) : Object

See the react-redux documentation on the connect function.

Optional.

mergeProps(stateProps, dispatchProps, ownProps) : Object

See the react-redux documentation on the connect function.

Optional.

options : Object

See the react-redux documentation on the connect function.

Optional.

reducer.plugin(reducers : Object) : Function

Returns a new reducer that will return a new state as a result of chaining the result of the original reducer and then the result of the dictionary of reducers.

Props

The decorated component will receive the following props:

CHANGE LOG

v2.0.1

v2.0.0

v1.3.0

v1.2.0

v1.1.0

v1.0.0

THANKS

Much of this package is inspired by the work of @erikras on redux-form. Much thanks!