React-minimal-form
Tiny, simple, and blazing fast react forms using the new context API (Provider/Consumer pair).
The inputs you get from the package are unstyled. The components used this demo do have some basic styles applied.
React-minimal-form is:
- A small package (only 5kb!)
- Really easy to use, while also supporting complex forms
- Generic! You can build your own form components with our provided HOC (or use our preconfigured, unstyled form elements)
- Fast. Yes, even with hundreds of form elements on one page.
- Almost dependency free! Well, let's not count react ?. Right now the only dependency is a package to make the new context API backwards compatible for older versions of react. We plan to remove it in the future.
- Compatible with your version of React. While we use the new context API, older versions of react are supported.
install
npm i --save react-minimal-form
Usage
Building a form
Building a form in React shouldn't be hard. With react-minimal-form, you could even call it fun again.
Here's how it works...
Change handling & input values for form components are provided by the <Form>
component by leveraging the new React context API. It works, however deeply nested these components are.
Internally, Form is a context Provider. A higher order component, makeFormElement
is the context Consumer.
Called with any react component, the HOC will make sure values & change events are handled accordingly. makeFormElement
is wrapped around all our exposed form elements.
By implementing a context-aware shouldComponentUpdate
lifecycle in makeFormComponent
that does not break other functionality such as prop updates, react-minimal-form works blazingly fast, even with hundreds of form elements on one page.
Here's the code for a very basic form:
Custom form elements
This library exposes a couple of generic, minimalistic form components such as a textinput, textarea,... that automatically work with <Form>
.
However, with our exposed HOC makeFormElement
, you can build your own form inputs as well. As explained, it handles the onChange & value prop for you, but obviously you must pass it through to your own custom input elements as well.
This allows you to build your own styled form inputs, where you can implement your own custom behavior.
Validation
Right now, we leave form validation up to you. It is trivial to implement for example a joi
(or any other validator) scheme in the onChange
or the onSubmit
callbacks.
To get an overview of touched fields, simply look at all the keys that are currently available in formData
.
Documentation
<Form>
Form is a controlled component. Supply an onChange & formData prop to make it work.
Property | Type | Required | Description |
---|---|---|---|
children | any | no | Any form element |
onChange | function | yes | Callback executed on change of every form element |
onSubmit | function | yes | Callback executed on form submit |
formData | object | yes | Object with all the form data |
Form elements
All html properties are passed to a form element. In addition, these props are available:
Property | Type | Required | Description |
---|---|---|---|
id | string | yes | A unique ID used by Form to handle changes and set values |
onChange | function | no | Add your own custom onChange handler as well. Will execute after the form change |