redux-form-inspector
An HOC for computing dynamic props from values inside an existing redux-form component.
Installation
Install with:
npm install -S redux-form-inspector
or
yarn add redux-form-inspector
Usage
Redux-form is a fantastic library which let you create forms inside your react application. In the following example, we create a simple form component with hello
as an unique identifier thanks to the reduxForm HOC .
;; const HelloForm = <form onSubmit=handleSubmit> <Field name="message" component="input" type="text"/> <button type="submit">Submit</button> </form>; form: 'hello' HelloForm;
redux-form-inspector
let you add some dynamic props
to you component (wrapped by your HOC) who are based on the values of any registered form
of your application. For example, you can disable fields, change the background color of your form, ... the sky is the limit.
;;;; const HelloForm = <form onSubmit=handleSubmit style= backgroundColor > showSecret && <span>Hello John</span> <Field name="email" component="input" type="text"/> <Field name="message" component="input" type="text"/> <button type="submit">Submit</button> </form>; const fieldsToProps = message ? 'red' : 'blue' messagelength > 0 && email === 'john@doe.com'; HelloForm;
API
The formInspector
function take a configuration object of the following form as input. In result of this call, it return a new HOC which can be used on any component (not just form).
const fieldsToProps = { ... } ...; const myCustomFormInspector = ;
form
The form name must be the same as the name you have passed to the reduxForm on the form that you're inspect. In the previous example, the form name was hello
.
If the provided form name does not exist or is not registred by redux-form
, each value of the resulting fieldsToProps
object will be equal to null
.
fieldsToProps
fieldsToProps
is the most important part of redux-form-inspector
. It is defined as a simple object with a prop name as key and a callback as value. At runtime, each callback is executed with the form field values and errors (both sync and async) as arguments. Each result is assigned to the following prop name.
The strength of fieldsToProps
lies in the fact that it can be easily tested.
inspectorKey [optionnal]
This attribute let you assign a custom root key for your fieldsToProps
result object.
If not specified, formInspector
will merge the fieldsToProps
result object inside your existing component props.
Contributing
Run the tests with this command:
make test