mirror of
https://github.com/cupcakearmy/formhero.git
synced 2024-12-23 00:26:23 +00:00
Update README.md
This commit is contained in:
parent
4650339251
commit
ff1fe80bbb
47
README.md
47
README.md
@ -39,7 +39,9 @@ npm i formhero
|
|||||||
- [form](#form)
|
- [form](#form)
|
||||||
- [errors](#errors)
|
- [errors](#errors)
|
||||||
- [isValid](#isvalid)
|
- [isValid](#isvalid)
|
||||||
- [update](#update)
|
- [setField](#setfield)
|
||||||
|
- [setForm](#setform)
|
||||||
|
- [setErrors](#seterrors)
|
||||||
|
|
||||||
## 🤔 Motivation
|
## 🤔 Motivation
|
||||||
|
|
||||||
@ -360,27 +362,48 @@ const { errors } = useForm(...)
|
|||||||
|
|
||||||
`isValid` is a little simple helper that checks whether the `error` object is clear or if there are errors left.
|
`isValid` is a little simple helper that checks whether the `error` object is clear or if there are errors left.
|
||||||
|
|
||||||
## Update
|
## setField
|
||||||
|
|
||||||
The `update` function allows you to manually change and assign the state of the form. This can be usefull when you want to reset a field or the whole form. The input must have the same type as the initial state.
|
The `setField` function allows you to manually change and assign the state of a field. The type of the field must be the same as the initial type given in the constructor.
|
||||||
|
|
||||||
###### Example
|
###### Example
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const { form, update } = useForm(...)
|
const { form, setField } = useForm(...)
|
||||||
|
|
||||||
const resetUsername = () => {
|
const resetUsername = () => {
|
||||||
update({
|
setField('username', 'new value')
|
||||||
...form,
|
|
||||||
username: '',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## setForm
|
||||||
|
|
||||||
|
The `setForm` function allows you to manually change and assign the state of the form. This can be usefull when you want to reset a field or the whole form. The input must have the same type as the initial state.
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const initial = {username: '', password: ''}
|
||||||
|
|
||||||
|
const { form, setForm } = useForm(initial, ...)
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
update({
|
setForm(initial)
|
||||||
username: '',
|
}
|
||||||
password: '',
|
```
|
||||||
})
|
|
||||||
|
## setErrors
|
||||||
|
|
||||||
|
The `setErrors` function allows you to manually change and assign the state of the errors. This can be usefull when you want to set an error manually (e.g. sent from the server).
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
|
||||||
|
const { form, setErrors } = useForm(...)
|
||||||
|
|
||||||
|
const setError = () => {
|
||||||
|
setErrors({username: 'Already taken'})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user