An alternate forms framework for Redux+React.
redux-form
?npm install --save redux-formo
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);
Decorate a React Component, connecting the Redux form state and providing helper methods
for handling form and field events e.g. onSubmit()
, onChange()
, onBlur()
.
The name of the form.
Required.
The names of the fields.
Required.
The default values of the fields.
Optional.
A function used to filter a field value.
Optional.
Parameters:
field
- The name of the field being filteredvalue
- The value of the field being filteredvalues
- All the valid field valuesReturns:
The filtered value.
A function used to validate a field value.
Optional.
Parameters:
field
- The name of the field being filteredvalue
- The value of the field being filteredvalues
- All the valid field valuesReturns:
True if the value is valid. An error string if the value is not valid.
A function used to submit the field values.
Optional.
Parameters:
dispatch
- The dispatch methodvalues
- All the valid field valuesReturns:
Void, a Flux Standard Action or a Promise.
error
property will be set and the form will not be marked as submitted
error
property will be set and the form will not be marked as submitted
error
property will be set and the form will not be marked as submitted
error
property will be set and the form will not be marked as submitted
submitted
Whether a field should be filtered when the field value changes.
Optional. Defaults to false
.
Whether a field should be validated when the field value changes.
Optional. Defaults to false
.
Whether a field should be filtered when the field loses focus.
Optional. Defaults to true
.
Whether a field should be validated when the field loses focus.
Optional. Defaults to true
.
Whether a field should be filtered when the form is submitted.
Optional. Defaults to true
.
Whether a field should be validated when the form is submitted.
Optional. Defaults to true
.
The name of the property where the form reducer is mounted on the state.
Optional. Defaults to form
.
The name of the property where the form state is passed in props to the react component.
Optional. Defaults to none.
Whether the form state is destroyed when the component is unmounted.
Optional. Defaults to true
.
See the react-redux
documentation on the connect
function.
Optional.
See the react-redux
documentation on the connect
function.
Optional.
See the react-redux
documentation on the connect
function.
Optional.
See the react-redux
documentation on the connect
function.
Optional.
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.
The decorated component will receive the following props:
bool
- whether one or more values are currently being filteredbool
- whether one or more values are currently being validatedbool
- whether the form is currently being submittedbool
- whether the form has been submitted one or more timesstring|undefined
- the error message from the last failed submissionbool
- whether all the field values are validobject
object
string
- the name of the fieldbool
- whether the field is currently active (i.e. focussed)bool
- whether the filter function is currently running on the fieldbool
- whether the field has been filtered one or more times since the store was createdbool
- whether the validation function is currently running on the fieldbool
- whether the field has been validated one or more times since the store was createdstring|undefined
- the error message from the previous validationbool
- whether the current value is valid*|undefined
- the the last successfully validated value*|undefined
- the current valuebool|undefined
- true when the value is true, false when the value is false*|undefined
- the defaultValuebool|undefined
- true when the value is true, false when the value is falsemapDispatchToProps
always returns dispatch
so that redux-formo
continues to work when mapDispatchToProps
is used.destroyOnUmount
to false
to
restore the previous behavioursubmit()
function to access the component props, the submit()
function
is now passed as a parameter to the onSubmit()
handlerconfig.form
to config.name
config.formPropKey
to config.propKey
config.formStateKey
to config.stateKey
props.fields[fieldName].validValue
to props.fields[fieldName].lastValidValue
checkbox
inputs and custom inputs in the onChange event.filterOnChange
and .validateOnChange
properties to allow configuration of whether validation occurs
each time the user changes the value of a fieldafterValidate()
, is called after validation of each is complete - may change in the future!decorate.jsx
to make it more testablethe-other-redux-form
to redux-formo
formPropKey
filter
, validate
and submit
methods now receive an object instead of numerous parametersfilter
, validate
and submit
methods receive an object containing most recent
valid values instead of the current valuesredux-promise
),
if the submit
method returns/resolves a FSA error the form submission will be assumed to have failedfilter
and validate
methods can return a promise, like the submit
method, in order to perform asynchronous
filtration and validationMuch of this package is inspired by the work of @erikras
on redux-form
. Much thanks!