This commit is contained in:
Niccolo Borgioli 2023-03-03 23:18:44 +01:00
parent ae53882a82
commit 26327ab91e
No known key found for this signature in database
GPG Key ID: D93C615F75EE4F0B
2 changed files with 27 additions and 27 deletions

View File

@ -1,9 +1,9 @@
![Logo](https://raw.githubusercontent.com/cupcakearmy/formhero/master/.github/Logo.jpg) ![Logo](https://raw.githubusercontent.com/cupcakearmy/formhero/master/.github/Logo.jpg)
![dependencies](https://badgen.net/david/dep/cupcakearmy/formhero)
![downloads badge](https://badgen.net/npm/dt/formhero)
![types badge](https://badgen.net/npm/types/formhero)
![version badge](https://badgen.net/npm/v/formhero) ![version badge](https://badgen.net/npm/v/formhero)
![types badge](https://badgen.net/npm/types/formhero)
![downloads badge](https://badgen.net/npm/dt/formhero)
![dependencies](https://badgen.net//bundlephobia/dependency-count/formhero)
![minzip size badge](https://badgen.net/bundlephobia/minzip/formhero) ![minzip size badge](https://badgen.net/bundlephobia/minzip/formhero)
**Fully customisable react form utility.** **Fully customisable react form utility.**
@ -22,7 +22,7 @@
npm i formhero npm i formhero
``` ```
*Note:* Requires at least typescript version `3.5`, otherwise the error object will not have the right inherited types. _Note:_ Requires at least typescript version `3.5`, otherwise the error object will not have the right inherited types.
### 👁 Demos ### 👁 Demos
@ -99,7 +99,7 @@ const Form = () => {
password: '', password: '',
}, },
{ {
username: value => value.length > 3, username: (value) => value.length > 3,
email: { email: {
validator: /@/, validator: /@/,
message: 'Must contain an @', message: 'Must contain an @',
@ -146,7 +146,7 @@ const Form = () => {
return ( return (
<form <form
onSubmit={e => { onSubmit={(e) => {
e.preventDefault() e.preventDefault()
console.log(form) console.log(form)
}} }}
@ -159,7 +159,7 @@ const Form = () => {
{...field('awesome', { {...field('awesome', {
setter: 'checked', setter: 'checked',
getter: 'onChange', getter: 'onChange',
extractor: e => e.target.checked, extractor: (e) => e.target.checked,
})} })}
/> />
Is it awesome? Is it awesome?
@ -176,28 +176,28 @@ const Form = () => {
Sometimes you don't know all the fields upfront. You can simply define a generic type and assign it to the initial object. Of course type assistance is limited in this case as formhero cannot be sure what keys are valid. Sometimes you don't know all the fields upfront. You can simply define a generic type and assign it to the initial object. Of course type assistance is limited in this case as formhero cannot be sure what keys are valid.
```typescript ```typescript
import React from "react"; import React from 'react'
import ReactDOM from "react-dom"; import ReactDOM from 'react-dom'
import { useForm } from "formhero"; import { useForm } from 'formhero'
type MyForm = { [field: string]: string | number }; type MyForm = { [field: string]: string | number }
const init: MyForm = { const init: MyForm = {
username: "unicorn", username: 'unicorn',
password: "" password: '',
}; }
const Form: React.FC = () => { const Form: React.FC = () => {
const { field, form, errors } = useForm(init); const { field, form, errors } = useForm(init)
return ( return (
<form> <form>
<input {...field("username")} placeholder="Username" /> <input {...field('username')} placeholder="Username" />
<input {...field("someother")} placeholder="Something else" /> <input {...field('someother')} placeholder="Something else" />
<input {...field("password")} placeholder="Password" type="password" /> <input {...field('password')} placeholder="Password" type="password" />
</form> </form>
); )
}; }
``` ```
## 📖 Documentation ## 📖 Documentation
@ -267,9 +267,9 @@ const validators = {
message: 'My custom error message', message: 'My custom error message',
}, },
/[\d]/, /[\d]/,
async value => value.length > 0, async (value) => value.length > 0,
{ {
validator: value => true, validator: (value) => true,
message: 'Some other error', message: 'Some other error',
}, },
], ],
@ -282,8 +282,8 @@ const validators = {
const validators = { const validators = {
username: async (s: string) => { username: async (s: string) => {
const taken = await API.isUsernameTaken(s) const taken = await API.isUsernameTaken(s)
return taken ? 'Username is taken': true return taken ? 'Username is taken' : true
} },
} }
``` ```
@ -307,7 +307,7 @@ const validators = {}
const options = { const options = {
setter: 'value', // This is not stricly necessarry as 'value' would already be the default. setter: 'value', // This is not stricly necessarry as 'value' would already be the default.
getter: 'onChangeText', getter: 'onChangeText',
extractor: text => text.toLowerCase(), extractor: (text) => text.toLowerCase(),
} }
export default () => { export default () => {
@ -339,7 +339,7 @@ export default () => {
{...field('username', { {...field('username', {
setter: 'value', // This is not stricly necessarry as 'value' would already be the default. setter: 'value', // This is not stricly necessarry as 'value' would already be the default.
getter: 'onChangeText', getter: 'onChangeText',
extractor: text => text.toLowerCase(), extractor: (text) => text.toLowerCase(),
})} })}
/> />
<Text>{form.username}</Text> <Text>{form.username}</Text>

View File

@ -1,6 +1,6 @@
{ {
"name": "formhero", "name": "formhero",
"version": "0.1.0", "version": "1.0.0-rc.0",
"type": "module", "type": "module",
"module": "./dist/index.js", "module": "./dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",