Update README.md

This commit is contained in:
Nicco 2019-09-28 19:14:40 +02:00 committed by GitHub
parent 34bcad56cc
commit 14e6f3d3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,66 +52,85 @@ const Form = () => {
}
```
## 🔥 Avanced Example
## 🔥 Examples
### Validation
```typescript
import ReactDOM from 'react-dom'
import { useForm } from 'formhero'
const Form = () => {
const { auto, form, errors } = useForm({
username: '',
password: '',
type: 'formhero',
awesome: true,
email: '',
password: ''
}, {
username: /^test/,
password: {
validator: /^.{3,}$/,
message: 'To short',
username: value => value.length > 3,
email: {
validator: /@/,
message: 'Must contain an @',
},
awesome: (value) => !!value
password: [
{
validator: /[A-Z]/,
message: 'Must contain an uppercase letter'
},
{
validator: /[\d]/,
message: 'Must contain a digit'
},
]
})
const _submit = (e: React.FormEvent) => {
e.preventDefault()
console.log(form, errors)
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>
)
}
```
### 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
const Form = () => {
const { auto, form, errors } = useForm({
awesome: true,
})
return (
<div>
<form onSubmit={_submit}>
<form onSubmit={e => {
e.preventDefault()
console.log(form)
}}>
<div>Username</div>
<input {...auto('username')} />
{errors.username && 'Something went wrong'}
<br />
<h1>Custom</h1>
<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>
<label>
<input type="checkbox" {...auto('awesome', {
setter: 'checked',
getter: 'onChange',
extractor: (e) => e.target.checked
})} />
<br />
Is it awesome?
</label>
<input type="submit" />
<button type="submit">Go 🚀</button>
</form>
</div>
)
}
```