About:
milliform is a super basic React form library that doesn't impose any constraints on rendering of your form.
milliform provides a useForm
React hook that provides all the utilities you'll need to manage the state of your form.
Installation:
milliform can be installed using your favorite package manager (e.g. yarn
, pnpm
, or npm
):
Or it can be imported from a CDN like unpkg
or esm.sh
:
Usage:
First let's start with a basic example using plain old inputs to build a login form:
In this example, we're passing the values down to the input elements, wrapping the onChange
handlers to extract the value from the event and pass it to the setters. Additionally, we conditionally render an error banner below each input if there are errors in the field.
Within the form
's onSubmit
handler, we call validate
to validate all the fields, if the fields are valid then we continue on to submit the form fields. In this example, we're only resetting the form by calling reset
.
In Depth:
To get started with milliform, you'll define the fields
that are within the form:
You can then call useForm
with the defined fields
and get back the following values:
values
:Record<string, string>
An object of key value pairs, each key maps to a name within the fields input (e.g.
ammount
in the above example)setters
:Record<string, (value: string) => void>
An object of key value pairs, each key maps to a name within the fields input (e.g.
ammount
in the above example), and the value is a setter function that takes in a value and returns void.errors
:Record<string, string> | null
An object of key value pairs, each key maps to a name within the fields input (e.g.
ammount
in the above example), and the value is the string returned from the validate function defined by the field (e.g."Invalid amount selected!"
).If there are no errors in any of the fields, then the value will be
null
validate
:() => Record<string, string> | null
A function that will validate all the fields within the form, both updating the
errors
return value fromuseForm
and returning those values as well.reset
:() => void
A function that resets all field values and errors