react-fluid-form
Reactive forms for react and react native, using hooks and Mobx@6.
Installation:
Inspiration
I made this library for particular use, because some libraries I used did not satisfy me: formik, mobx-react-form, unform. Some features are inspired by these libs.
The purpose of this library is to be easy to use, with hooks and mobx, which in my use proved to be more performative. Validation is completely optional and customizable. I hope it helps who needs something like this.
I used Mobx for store the state (https://mobx.js.org)
Quick start
1. Initial configuration
1.1 Define components
First, you need to create a bind components
object, that tells to library how pass props to field component (like <input />):
1.2 Add <FormProvider />
After, you need wrap root component with <FormProvider />
, and pass components:
2. Create your form
2.1 Call useForm hook
Now, you need call useForm inside your component, that instantiate the form.
With instance, you can get the result values, errors, validate programmatically, check validation, and more.
2.2 Validate the form
Optionally, pass the validator. React fluid form, by default, uses yup
for validation, but you can create your custom validator, or use other libraries. There is no need to use react-fluid-form with validation, just you want.
Example code:
2.3 Wrap your component with <Form />
2.3 Wrap your field with <Field />
To show the error on screen, Field pass "error" prop forward. So, you can render in custom input component
3. Form instance
Form instance has some helper properties and methods:
Prop/Method | Return type | Description |
---|---|---|
form.isValid |
boolean | Check if the form is valid |
form.raw |
object | Get values to save |
form.errors |
object | Errors object |
form.validatePath(path) |
void | Validate path of form |
form.setValues(values) |
void | Pass new values to form |
form.setPathValue(path, value) |
void | Set value for specific path |
form.setPathError(path, error) |
void | Set error for specific path |
form.mergeValues(values) |
void | Merge values with new values to form |
Contributions
All contributions are welcome. Just make a PR.