# react-easy-form-hook

> A headless hook for creating forms in react. Created to be fast, configurable and easy to use.

Latest version **0.1.2** (published 2021-09-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-easy-form-hook
pnpm add react-easy-form-hook
yarn add react-easy-form-hook
bun add react-easy-form-hook
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2021-09-10 |
| First published | 2021-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 81 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Bartosz Szelc |
| Maintainers | bartoszsz |
| Keywords | react, headless, form, forms, inputs, input, hook |

## Links

- npm: https://www.npmjs.com/package/react-easy-form-hook
- Repository: https://github.com/Bartsz/react-easy-form-hook
- Homepage: https://github.com/Bartsz/react-easy-form-hook#readme
- Issues: https://github.com/Bartsz/react-easy-form-hook/issues
- npm.io page: https://npm.io/package/react-easy-form-hook

## Dependencies (1)

- [@types/react](https://npm.io/package/@types/react.md) >=16.8.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2021-09-10
- 0.1.1 — 2021-09-10
- 0.1.0 — 2021-09-10

## README

# Easy React Headless Forms

## Features

* Strongly typed
* Easily configurable
* Fast
* Supports dependent dropdowns
* Comes with built-in html field factory
* Can be used with other UI libraries

# Table of contents
<!-- TOC -->

- [Easy React Headless Forms](#easy-react-headless-forms)
    - [Features](#features)
- [Table of contents](#table-of-contents)
- [Examples](#examples)
    - [Simple](#simple)
    - [Nested Objects](#nested-objects)
    - [RegExp validation](#regexp-validation)
- [API](#api)
    - [useForm](#useform)
    - [settings: FormSettings](#settings-formsettings)
    - [FieldSettings\<TCustomProps>](#fieldsettings\tcustomprops)
    - [DefaultFieldSettings\<TCustomProps\>](#defaultfieldsettings\tcustomprops\)
    - [IForm\<TObject, TCustomProps>](#iform\tobject-tcustomprops)
        - [object: TObject](#object-tobject)
        - [getObject: () => TObject](#getobject---tobject)
        - [fields: FormField<TCustomProps>[]](#fields-formfieldtcustomprops)
        - [formState: IFormState](#formstate-iformstate)

<!-- /TOC -->
# Examples
## Simple
Let's create a simplest possible form with user data, address and start date

```ts
import React, { useMemo } from 'react';
import { FieldFactory, useForm, FieldSettings, FieldType } from 'easy-react-form-hook';

export function SimpleForm() {

    const fields = useMemo(() => [
        { type: FieldType.Text, accessor: "firstName", label: "First Name", },
        { type: FieldType.Text, accessor: "lastName", label: "Last Name", },
        { type: FieldType.Text, accessor: "city", label: "City", },
        { type: FieldType.Text, accessor: "street", label: "Street", },
        { type: FieldType.Date, accessor: "startDate", label: "startDate", },
    ] as FieldSettings[], []);

    const form = useForm({ fieldsSettings: fields });
    const submit = (object:  any) => {/** you api call here */}

    return <>
        {form.fields.map(field => <div key={field.accessor}><FieldFactory {...field} /></div>)}
        <button disabled={ (!form.formState.valid && !form.formState.changed)} onClick={() => submit(form.object)}>Submit</button>
    </>;
}
```

FieldFactory, will handle creation of all field types in following format
```tsx
<label {...field.labelAttributes}>{field.label}</label>
<input {...field.attributes} />
```
or

```tsx
<label {...field.labelAttributes}>{field.label}</label>
<select {...field.attributes}>
        {field.options?.map(option => <option key={option.value} {...option} />)}
</select>
```

You can always implement your own factory

## Nested Objects
Same as above, just change accessors and it is done.

```ts
import React, { useMemo } from 'react';
import { FieldFactory, useForm, FieldSettings, FieldType } from 'easy-react-form-hook';

export function SimpleForm() {

    const fields = useMemo(() => [
        { type: FieldType.Text, accessor: "user.firstName", label: "First Name", },
        { type: FieldType.Text, accessor: "user.lastName", label: "Last Name", },
        { type: FieldType.Text, accessor: "address.city", label: "City", },
        { type: FieldType.Text, accessor: "address.street", label: "Street", },
        { type: FieldType.Date, accessor: "startDate", label: "startDate", },
    ] as FieldSettings[], []);

    const form = useForm({ fieldsSettings: fields });
    const submit = (object:  any) => {/** you api call here */};

    return <>
        {form.fields.map(field => <div key={field.accessor}><FieldFactory {...field} /></div>)}
        <button disabled={ (!form.formState.valid && !form.formState.changed)} onClick={() => submit(form.object)}>Submit</button>
    </>;
}
```
## RegExp validation

Field is validated using RegExp everytime if regex property is provided and onChange event is called./
RegExp is also provided to field as attribute pattern so it is calculated by browser as well. This allows to use css with selector such as `input:valid` or `input:invalid`.
Also `<input type='submit'>` can laverage this validation as well.

Based on previous example, let's change input **user.fistName** and add regex.
```ts
    { type: FieldType.Text, accessor: "firstName", label: "First Name", regex: /[a-zA-Z]{2,15}/, },
```

If any of the fields in the form are not valid or not populated when required, state of the form is set to invalid `form.formState.valid` = `false`.

# API

## useForm

```ts
import { useForm } from 'east-react-from-hook'

const form = useForm(settings)
```

## settings: FormSettings
```ts
export interface FormSettings<TObject, TCustomProps> {
    fieldsSettings: FieldSettings<TCustomProps>[];
    defaultSettings?: DefaultFieldSettings<TCustomProps>;
    initialState?: TObject;
    disabled?: boolean;
    keepEmptyProperties?: boolean;
    refreshObjectOnChange?: boolean;
    formName?: string;
}
```
* fieldSettings
    * List of fields handled by form e.g.
    ```ts
    const fields =[
        { type: FieldType.Text, accessor: "name", label: "Name", },
        { type: FieldType.TextArea, accessor: "comment", label: "Comment",},
    ]
    ```
* defaultSettings
    * Default field settings that will be marged with all provided fields.

* initialState
    * Object from which, values will be read. If not provided, object will be initialized to empty object `{}`

* disabled
    * disables all form fields

* keepEmptyProperties
    * By default, fields with value = undefined, will not be included in object. 
    \
    On the example of fields above. If keepEmptyProperties is false or udefined and user hasn't populated input fields, then resulting object will be **`{}`**
    If user have only populated field **name** with value *John Smith*, then form object will look as below
    ```ts
        {
            name: "John Smith"
        }
    ```
    If both fields were populated, then form object will have both properties
    ```ts
        {
            name: "John Smith",
            comment: "This is a new user",
        }
    ```
* refreshObjectOnChange
    * If you don't need your object to be recreated on every onChange event, then you can set this property to `false`. To create a form object on demand, use function ` getObject: () => TObject;` from IForm interface returned by `useForm` hook.

* formName
    * name of the form that will be added to every field as a field attribute **`form`**

## FieldSettings\<TCustomProps>

Depending on the field type selected, there will be different options and field attribute to set. However, all fields have following properties in common

```ts
    type: FieldType;
    accessor: string;
    disabled?: boolean;
    label: string;
    tag?:string;
```

* type: FieldType
    * Directly corresponds to HTML input field types. Based on selected FieldType, type of FieldSettings object will change and different properties and field attributes will be accepted.
    ```ts
    enum FieldType {
        Checkbox = "checkbox",
        Color = "color",
        Date = "date",
        DateRange = "dateRange",
        Email = "email",
        File = "file",
        Month = "month",
        Number = "number",
        NumericRange = "numberRange",
        Range = "range",
        Radio = "radio",
        Passowrd = "password",
        Search = "search",
        Select = "select",
        SelectMultiple = "multiselect",
        Submit = "submit",
        Telephone = "tel",
        Text = "text",
        TextArea = "textarea",
        Time = "time",
        Week = "week",
    }
    ```

* accessor
    * A path to property in object, using js notation style.
    E.g., in object 
    ```ts
    {
        user: {
            firstName: "John",
            lastName: "Smith",
            address: {
                street: "Main St."
                city: "New Town"
            }
        },
        comment: "This is a new user",

    }
    ```
    we have following accessors\
    `user.firstName`\
    `user.lastName`\
    `user.address.street`\
    `user.address.city`\
    `comment`\

* disabled
    * Permanently disables a field

* label
    * label used for HTML tag `<label>`label value goes here`</label>`

* tag
    * Since this is a headless form, you might decide that you want to split this object to multiple tabs (e.g., Personal Information, Address, Account Data).\
    **tag** can be used to filter fields and use them in different parts of your app
## DefaultFieldSettings\<TCustomProps\>
Object used to provide properties and field attributes that will be merged with all fields. If specific field settings provide the same attributes, they will override default settings.

```ts
    className?: string;
    classNameLabel?: string;
    customProps?: TCustomProps;
    type?: FieldType;
```

## IForm\<TObject, TCustomProps>
Objet returned by hook `useForm`
```ts
    fields: FormField<TCustomProps>[];
    formState: IFormState;
    object: TObject;
    getObject: () => TObject;
```

### object: TObject
    Object created from all the fields.

### getObject: () => TObject
    Function used to get final object when a `refreshObjectOnChange` is set to false.
### fields: FormField<TCustomProps>[]
List of fields to be rendered. There are multiple type of fields with following attributes in common.

```ts
    accessor: string;
    accessors: string[];
    customProps?: TCustomProps;
    disabled?: boolean;
    initialValue?: string | number;
    labelAttributes?: FormFieldLabel;
    tag?:string;
    value?: any;
```

* accessor
    * As explained in section [FieldSettings\<TCustomProps>](#fieldsettings\tcustomprops)

* accessors
    * Array created by spitting **accessor** by dot ".". It is used internally to speed up updating form object.

* customProps
    * Object defined by user that was provided in **FieldSettings**

* disabled
    * Indicates if field is disabled

* initialValue
    * Value taken from object provided as `initialState` in [FormSettings](#settings-formsettings). It is used to decide if form field and consequently whole from has been change. See [formState: IFormState](#formstate-iformstate) **changed**.

* labelAttributes
    * attributes passed to `<label>` tag. Consists of following attributes

    ```ts
    id?: string
    className?: string,
    htmlFor?: string,
    ```

* tag
    * A sdescribed in section [FieldSettings\<TCustomProps>]

* value
    * Current value of field. It is of a type approperiate for given FieldType

### formState: IFormState
Object used to keep various information about the Form
```ts
    /** Defines if all fields with validation are valid */
    valid: boolean;
    /** Has form changed from inital value */
    changed: boolean;
    /** If true, properties that are undefined will set to null, otherwise thwy will not be created in final Object */
    keepEmptyProperties?: boolean;
    /** Is form dissabled */
    disabled?: boolean;
    /** If true, final object will be recreated every time onChange event is called on any input */
    refreshObjectOnChange?: boolean;
```

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