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

121
README.md
View File

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