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
ad4b749330
commit
120748f508
47
README.md
47
README.md
@ -84,3 +84,50 @@ const Form = () => {
|
|||||||
```typescript
|
```typescript
|
||||||
const {auto, errors, update, form} = useForm(initial, validators, options)
|
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
|
||||||
|
|
||||||
|
```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',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user