Prop Types
model
(required)mapProps
updateOn
validators
validateOn
asyncValidators
asyncValidateOn
errors
parser
changeAction
controlProps
component
ignore
disabled
The <Control>
component represents a form control, such as an <input />
, <select>
, <textarea />
, etc.
It is a connected component, and will use the model
prop to connect itself to the Redux store and dispatch the appropriate actions for each event handler.
The following pre-defined <Control>
s are available:
<Control>
or<Control.input>
for standard<input />
controls<Control.text>
for<input type="text" />
<Control.radio>
for<input type="radio" />
<Control.checkbox>
for<input type="checkbox" />
<Control.file>
for<input type="file" />
<Control.select>
for<select></select>
<Control.button>
for<button></button>
You can add your own types to the basic <Control>
component as an attribute:
<Control type="password">
For making custom controls that work with React Redux Form, see the custom controls documentation.
import React from 'react';
import { Control } from 'react-redux-form';
class App extends React.Component {
render() {
return (
<form>
<label>Name:</label>
<Control.text model="user.name" />
<label>Favorite color:</label>
<Control.select model="user.faveColor">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</Control.select>
</form>
);
}
}
export default App; // no need to connect
(String | Function): The string representing the model value in the store.
// in store.js
export default createStore(combineForms({
'user': { name: '' },
}));
// in component's render() method
<Control.text model="user.name" />
It can also be a function that returns a string model. See the documentation on tracking for more information.
## `mapProps={{...}}` _(Object)_: A mapping of control-specific property keys to prop-getter functions that taken in the original props and return the result prop. See [the documentation on custom controls](../guides/custom-controls.md) for more information.Example:
<Control
mapProps={{
customChange: (props) => props.change,
}}
model="..."
/>
"change"
- will dispatch inonChange
"blur"
- will dispatch inonBlur
"focus"
- will dispatch inonFocus
So, <Control model="foo.bar" updateOn="blur">
will only dispatch the change(...)
action on blur.
You can also specify updateOn={['change', 'blur']}
as an array of one or more of the above values.
- Use the
changeAction
prop if you want to dispatch custom actions along with theactions.change(...)
action.
For example, this control validates that a username exists and is longer than 4 characters:
<Control.text
model="user.username"
validators={{
required: (val) => val.length,
length: (val) => val.length > 4
}}
/>
- If using ES2015 and you have validator functions, you can do this destructuring shortcut:
const required = (val) => val && val.length;
const length = (val) => val.length > 8;
<Control.text
model="user.username"
validators={{ required, length }}
/>
- Validation will always occur on load; i.e., when the component is mounted. This is to ensure an accurate validation state for a new form.
- To avoid displaying error messages on load (as controls might be invalid), use the
.pristine
property of the control when conditionally showing error messages, or use the<Errors>
component.
Each async validator function is called with 2 arguments:
value
- the model valuedone(validity)
- a callback function that should be called with the calculated validity
For example, this control validates that a username is available via a promise:
// function that returns a promise
import isAvailable from '../path/to/is-available';
<Control.text model="user.username"
asyncValidators={{
isAvailable: (value, done) => {
isAvailable(value)
.then((result) => done(result));
}
}} />
- Async validators will run on
blur
, unless you specify otherwise in theasyncValidateOn="..."
prop.
An error validator is a function that returns true
or a truthy value (such as a string) if invalid, and false
if valid.
For example, this control validates that a username exists and is longer than 4 characters:
<Control.text
model="user.username"
errors={{
isEmpty: (val) => !val.length,
tooLong: (val) => val.length > 16,
}}
/>
Example
function toAge(value) {
return parseInt(value) || 0;
}
<Control.text
type="number"
model="user.age"
parser={ toAge }
>
actions.change(model, value)
for text input controlsactions.toggle(model, value)
for checkboxes (single-value models)actions.xor(model, value)
for checkboxes (multi-value models)
The action creator takes in two arguments:
model
- the model that is being changedvalue
- the value that the model is being changed to
To create a custom <Control>
that submits the form on blur:
import { Control, actions } from 'react-redux-form';
const submitPromise = ... // a promise
function changeAndSubmit(model, value) {
return (dispatch) => {
dispatch(actions.change(model, value));
dispatch(actions.submit('user', submitPromise));
};
}
// Then, in your <Control> components...
<Control.text
type="email"
model="user.name"
changeAction= { changeAndSubmit }
updateOn="blur"
/>
- Use
changeAction
to do any other custom actions whenever your value is to change. - Since
changeAction
expects an action creator andredux-thunk
is used, you can asynchronously dispatch actions (like the example above).
The normal behavior is that any extraneous props on <Control>
that are not part of Control.propTypes
(which are documented here) will be given to the rendered input.
Example:
// Suppose your <CustomInput> takes in an "errors" prop:
<Control.text
model="..."
component={CustomInput}
controlProps={{errors: 'errors for CustomInput'}}
/>
import { Control } from 'react-redux-form';
const MyTextInput = (props) => <input className="my-input" {...props} />;
// usage inside render():
<Control
model="user.firstName"
component={MyTextInput}
/>
For instance, if you don't care whether a <Control>
is focused or blurred:
// will ignore onFocus and onBlur
<Control
model="..."
ignore={['focus', 'blur']}
/>
(Any): The disabled
prop works just like you'd expect for controls that support the HTML5 disabled
attribute.
However, in <Control>
, it can be a boolean, or a function, string, or object as a Lodash iteratee.
// Disable the submit button when the form is invalid
<Control.button
model="user"
disabled={{ valid: false }}
>
Submit!
</Control.button>
For example:
disabled={true}
ordisabled={false}
will disable or enable the control respectively, as will any other primitive value, such asundefined
,null
, or a numberdisabled="touched"
will disable if the field istouched
(works with any property on the field)disabled={{ valid: false, touched: true }}
will disable if the field is bothtouched
and notvalid
disabled={(fieldValue) => !fieldValue.valid}
will call the function provided with thefieldValue
to determine itsdisabled
state.
(since: version 1.3.0)
(Function): Calls the callback provided to the getRef
prop with the node instance. Similar to ref
.
<Control.text
model="user.name"
getRef={(node) => this.attach(node)}
/>