# rocket-useform

> Rich useForm hooks. Make complex forms easily.

Latest version **0.1.0** (published 2021-07-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install rocket-useform
pnpm add rocket-useform
yarn add rocket-useform
bun add rocket-useform
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-07-14 |
| First published | 2021-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 208.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | anthonyjeamme |
| Maintainers | jinga |

## Links

- npm: https://www.npmjs.com/package/rocket-useform
- Repository: https://github.com/anthonyjeamme/rocket-useform
- Homepage: https://github.com/anthonyjeamme/rocket-useform#readme
- Issues: https://github.com/anthonyjeamme/rocket-useform/issues
- npm.io page: https://npm.io/package/rocket-useform

## Recent versions

- 0.1.0 (latest) — 2021-07-14
- 0.0.27 — 2021-06-29
- 0.0.26 — 2021-06-29
- 0.0.25 — 2021-06-29
- 0.0.24 — 2021-06-18
- 0.0.23 — 2021-06-01
- 0.0.22 — 2021-05-24
- 0.0.21 — 2021-05-24
- 0.0.20 — 2021-05-21
- 0.0.19 — 2021-05-21
- 0.0.18 — 2021-05-21
- 0.0.17 — 2021-05-20
- 0.0.16 — 2021-05-20
- 0.0.15 — 2021-05-18
- 0.0.14 — 2021-05-18
- … 12 more at https://npm.io/package/rocket-useform/versions

## README

<center>
  <img height="200" src="https://res.cloudinary.com/anthony-jeamme-stuff/image/upload/v1626271277/libraries/rocket-useform/logo.png">

# Rocket useForm

</center>

<b>WARNING: library in alpha version</b>

Rocket useForm is a rich React library based on useRef.
It's an easy-to-use solution to build big and complex forms.

## Installation

```bash
npm i rocket-useform
```

## Getting started

```tsx

const data = {
	name: 'john doe'
}

const schema = Schema({
	name: textField({required:true, validation: (name)=>name.length > 3})
})

const MyForm = () => {

	const form = useForm(data, schema)

	const handleSave = () => {

		if(form.checkForm()){
			console.log(form.toJSON())
			form.setModified(false)
		} else {
			console.warn('Il y a des champs incorrects !')
		}
	}

	return (
		<div>
			{form.modified ? 'Modifié':''}
			<input {...matchInput(form.get('name))}>
			<button onClick={()=>handleSave()}>SAVE</button>
		</div>
	)
}

```

## Main concepts

### Schema based

```tsx
const schema = Schema({
  name: textField()
})
```

Possible fields types:

- `textField`
- `numberField`
- `booleanField`
- `arrayField`

Each field schema can take a param argument, where you will define validation rules, defaultValue and so on:

```tsx
const schema = Schema({
  email: textField({
    validators: [Validator.TEmail]
  })
})
```

See docs for more details

### Path system

```jsx
const cityField = form.get('user.address.city')
```

Can also be splitted :

```jsx
const addressField = form.get('user.address')
const cityField = addressField.get('city')
```

Get parent :

```jsx
const cityField = form.get('user.address.city')
const addressField = addressField.get('parent')
```

From root :

```jsx
const cityField = form.get('user.address.city')
const nameField = addressField.get('root.user.name')
```

### Node

Form is a tree of Nodes reachable through path:

```jsx
const rootNode = form.get('')
const userNode = rootNode.get('user')
```

### Stateless

Rocket useForm is stateless for performance. It means that data flow is unidirectional.

- You need sur match input with defaultValue :

```jsx
	<input
		defaultValue={form.get('name').value}
		onChange={e => form.get('name').update(e.target.value)}
	>
```

But don't worry ! You can force refresh this way :

```jsx
	<input
		defaultValue={form.get('name').value}
		onChange={e => form.get('name').update(e.target.value, true)}
	>
```

update function take a boolean as second argument : forceRefresh

## Schema

### Field

- Text field

```tsx
const schema = {
  name: textField()
}
```

- Number field

```tsx
const schema = {
  name: numberField()
}
```

- Boolean field

```tsx
const schema = {
  name: booleanField()
}
```

### Object

### Array

```tsx
const schema = {
  users: arrayField({
    name: textField()
  })
}
```

### Params

---
_Source: https://npm.io/package/rocket-useform · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
