mirror of
https://github.com/cupcakearmy/formhero.git
synced 2024-12-22 08:06:24 +00:00
Fully customisable React form utility
.github | ||
lib | ||
test | ||
.gitignore | ||
.npmignore | ||
LICENSE | ||
package.json | ||
README.md | ||
tsconfig.json |
Fully customisable react form utility.
🌈 Features
- Typescript compatible
- Customizable extractor, validator, getter and setters. (More in the docs)
- 0 Dependencies
- Tiny
- React Hooks
Installation
npm i formhero
🚀 Quickstart
import ReactDOM from 'react-dom'
import { useForm } from 'formhero'
const Form = () => {
const { auto, form } = useForm({
username: '',
password: '',
})
const _submit = (e: React.FormEvent) => {
e.preventDefault()
console.log(form)
}
return (
<div>
<form onSubmit={_submit}>
<input {...auto('username')} />
<input {...auto('password')} />
<button type="submit">Go 🚀</button>
</form>
</div>
)
}
🔥 Avanced Example
import ReactDOM from 'react-dom'
import { useForm } from 'formhero'
const Form = () => {
const { auto, form, errors } = useForm({
username: '',
password: '',
type: 'formhero',
awesome: true,
}, {
username: /^test/,
password: {
validator: /^.{3,}$/,
message: 'To short',
},
awesome: (value) => !!value
})
const _submit = (e: React.FormEvent) => {
e.preventDefault()
console.log(form, errors)
}
return (
<div>
<form onSubmit={_submit}>
<div>Username</div>
<input {...auto('username')} />
{errors.username && 'Something went wrong'}
<br />
<div>Password</div>
<input {...auto('password')} />
<br />
<div>Which one to choose?</div>
<select {...auto('type')}>
<option value="redux-form">Redux-Form</option>
<option value="react-hook-forms">React-Hook-Forms</option>
<option value="formik">Formik</option>
<option value="formhero">FormHero</option>
</select>
<br />
<div>Is it awesome?</div>
<input type="checkbox" {...auto('awesome', {
setter: 'checked',
getter: 'onChange',
extractor: (e) => e.target.checked
})} />
<br />
<button type="submit">Go 🚀</button>
</form>
</div>
)
}
📖 Docs
useForm
const {auto, errors, update, form} = useForm(initial, validators, options)
Initial
This is the base state of the form. Also the typescript types are inhered by this.
Example
const initial = {
username: 'defaultValue',
password: '',
rememberMe: true,
}
Validators
A validator is an object that taked in either a RegExp
or a Function
(can be async). Optionally you can pass a message string that will be displayed instead of the default one.
Example: Regular Expression
const validators = {
// Only contains letters.
// This could also be a (also async) function that returns a boolean.
username: /^[A-z]*$/,
}
Example: Function
const validators = {
username: (value: string) => value.lenght > 3,
}
Example: With Object
const validators = {
username: {
validator: /^[A-z]*$/,
message: 'My custom error message',
}
}