2019-09-26 20:40:37 +00:00
![Logo ](./.github/Logo.svg )
2019-09-26 19:30:28 +00:00
2019-09-26 20:05:53 +00:00
![Version ](https://badgen.net/npm/v/formhero )
![Dependencies ](https://badgen.net/david/dep/cupcakearmy/formhero )
![Size Badge ](https://badgen.net/bundlephobia/minzip/formhero )
2019-09-26 20:40:37 +00:00
**Fully customisable react form utility.**
2019-09-26 20:05:53 +00:00
2019-09-26 20:06:16 +00:00
## 🌈 Features
2019-09-26 19:30:28 +00:00
- Typescript compatible
- Customizable extractor, validator, getter and setters. (More in the docs)
2019-09-27 12:34:58 +00:00
- **0** Dependencies
2019-09-26 20:05:53 +00:00
- Tiny
2019-09-26 20:57:36 +00:00
- React Hooks
2019-09-26 19:30:28 +00:00
2019-09-28 17:30:04 +00:00
###### Links
- [*See it in action HERE* ](https://cupcakearmy.github.io/formhero/ )
- [Examples ](#Examples )
- [Docs ](#Documentation )
- Contructor
- [Initial State ](#Initial )
- [Validators ](#Validators )
- [Options ](/#Options )
- Returns
- [auto ](#Auto )
- [form ](#Form )
- [errors ](#Errors )
- [isValid ](#isValid )
- [update ](#Update )
2019-09-27 12:33:51 +00:00
###### Installation
```
npm i formhero
```
2019-09-26 19:30:28 +00:00
## 🚀 Quickstart
2019-09-26 19:31:13 +00:00
```typescript
2019-09-26 19:30:28 +00:00
import ReactDOM from 'react-dom'
import { useForm } from 'formhero'
2019-09-27 12:33:15 +00:00
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 >
)
}
```
2019-09-28 17:14:40 +00:00
## 🔥 Examples
### Validation
2019-09-27 12:33:15 +00:00
```typescript
2019-09-28 17:14:40 +00:00
const Form = () => {
2019-09-28 17:16:45 +00:00
const { auto, form, errors } = useForm({
username: '',
email: '',
password: ''
}, {
username: value => value.length > 3,
email: {
validator: /@/,
message: 'Must contain an @',
},
password: [
{
validator: /[A-Z]/,
message: 'Must contain an uppercase letter'
},
{
validator: /[\d]/,
message: 'Must contain a digit'
},
]
})
return (
< form >
< h1 > Errors & Validation< / h1 >
< input { . . . auto ( ' username ' ) } placeholder = "Username" / >
{errors.username & & 'Must be longer than 3'}
< input { . . . auto ( ' email ' ) } placeholder = "EMail" / >
{errors.email}
< input { . . . auto ( ' password ' ) } placeholder = "Password" type = "password" / >
{errors.password}
< / form >
)
2019-09-28 17:14:40 +00:00
}
```
2019-09-27 12:33:15 +00:00
2019-09-28 17:14:40 +00:00
### Easy Customization
Often it happens that you use a specific input or framework, so the default getter, setter and extractor for the event won't cut it. No worries: formhero got you covered!
```typescript
2019-09-26 19:30:28 +00:00
const Form = () => {
2019-09-28 17:16:45 +00:00
const { auto, form, errors } = useForm({
awesome: true,
})
2019-09-26 19:30:28 +00:00
2019-09-28 17:16:45 +00:00
return (
< form onSubmit = {e = > {
e.preventDefault()
console.log(form)
}}>
2019-09-26 19:30:28 +00:00
2019-09-28 17:16:45 +00:00
< h1 > Custom< / h1 >
2019-09-26 19:30:28 +00:00
2019-09-28 17:16:45 +00:00
< label >
< input type = "checkbox" { . . . auto ( ' awesome ' , {
setter: 'checked',
getter: 'onChange',
extractor: (e) => e.target.checked
})} />
Is it awesome?
< / label >
2019-09-26 19:30:28 +00:00
2019-09-28 17:16:45 +00:00
< input type = "submit" / >
2019-09-28 17:14:40 +00:00
2019-09-28 17:16:45 +00:00
< / form >
)
2019-09-26 19:30:28 +00:00
}
```
2019-09-28 17:30:04 +00:00
## 📖 Documentation
2019-09-26 19:30:28 +00:00
### `useForm`
```typescript
2019-09-28 17:03:37 +00:00
const {auto, errors, update, form, isValid} = useForm(initial, validators, options)
2019-09-26 19:30:28 +00:00
```
2019-09-26 20:53:43 +00:00
### Initial
This is the base state of the form. Also the typescript types are inhered by this.
###### Example
```javascript
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
```javascript
const validators = {
// Only contains letters.
// This could also be a (also async) function that returns a boolean.
username: /^[A-z]*$/,
}
```
###### Example: Function
```typescript
const validators = {
username: (value: string) => value.lenght > 3,
}
```
###### Example: With Object
```javascript
const validators = {
username: {
validator: /^[A-z]*$/,
message: 'My custom error message',
}
}
```
2019-09-28 17:19:10 +00:00
###### Example: Multiple Validators
```javascript
const validators = {
2019-09-28 17:19:48 +00:00
username: [
{
validator: /^[A-z]*$/,
message: 'My custom error message',
},
/[\d]/,
async (value) => value.length > 0,
{
validator: (value) => true,
message: 'Some other error',
}
2019-09-28 17:19:10 +00:00
]
}
```