# @savedo/re-form

> react form builder library

Latest version **2.0.0** (published 2019-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @savedo/re-form
pnpm add @savedo/re-form
yarn add @savedo/re-form
bun add @savedo/re-form
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-10-30 |
| First published | 2019-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Savedo GmbH. |
| Maintainers | cameron_prebble, erhangundogan, lisovskyvlad, mrarguello, peeyushsingla, savedo-it |
| Keywords | react, form, hooks, validation, form-builder |

## Links

- npm: https://www.npmjs.com/package/@savedo/re-form
- Repository: https://github.com/Savedo/re-form
- Homepage: https://github.com/Savedo/re-form#readme
- Issues: https://github.com/Savedo/re-form/issues
- npm.io page: https://npm.io/package/@savedo/re-form

## 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

- 2.0.0 (latest) — 2019-10-30
- 1.9.1 — 2019-09-10
- 1.9.0 — 2019-09-02
- 1.8.0 — 2019-08-26
- 1.7.0 — 2019-08-19
- 1.6.0 — 2019-08-16
- 1.5.2 — 2019-08-12
- 1.5.1 — 2019-08-07
- 1.5.0 — 2019-07-29
- 1.4.0 — 2019-07-25
- 1.3.0 — 2019-07-25
- 1.2.0 — 2019-07-18
- 1.1.1 — 2019-05-17
- 1.1.0 — 2019-05-15
- 1.0.3 — 2019-05-03
- … 1 more at https://npm.io/package/@savedo/re-form/versions

## README

re-form
=======

React form library.

![re-form](http://g.recordit.co/rA3YW8gMsf.gif)

Installation
=======

re-form requires

* react/react-dom 16.8.3 or later to build your form.

`$ npm install --save @savedo/re-form`

or

`$ yarn add @savedo/re-form`

Usage
=====

Basic usage: Creating a react form component with three input fields (name, age, email) validation and values assignment.

Create your form component:

```jsx
import React from 'react';
import { FormBuilder } from '@savedo/re-form';

const MyForm = ({ handleSubmit }) => {
  const fields = [
    'name',
    'age',
    'email'
  ];

  const fieldOptions = {
    name: {
      label: 'Name:'
    },
    age: {
      label: 'Age:',
      type: 'number'
    },
    email: {
      label: 'E-mail:'
    }
  };

  const validate = (formData) => {
    const errors = {};
    const { name, age } = formData;

    if (!name) {
      errors.name = 'Name is required!';
    }
    if (!age) {
      errors.age = 'Age is required!';
    } else if (Number(age) < 18) {
      errors.age = 'You should be 18 years old or older!';
    }

    return errors;
  };

  return (
    <div>
      <FormBuilder { ...{ fields, fieldOptions, handleSubmit, validate } } />
    </div>
  );
};

export default MyForm;

```

And use form component elsewhere:

```jsx
import React from 'react';
import MyForm from './MyForm';

const App = () => {
  const handleSubmit = (formData) => {
    console.log(formData)
  };

  return (
    <MyForm handleSubmit={ handleSubmit } />
  );
};

export default App;
```

Configuration
=============

The `FormBuilder` component has props as shown below. It has `FormBuilderPropsType<T>` type. T refers to your object type having key (Field name, <string>) / values (Field options <FieldOptionsValueType>).

#### FormBuilderPropsType

Property | Type | Description
--- | --- | ---
fields | string[] | Unique field names
fieldOptions | { key (string): options (FieldOptionsValueType) } | Field/Component options (See table below)
values | object | Init values for the fields
handleSubmit | function | Callback function to handle submit behaviour if validation successful
validate | method | Callback function for validation (form data will be passed as an argument), supports sync/async functions.
submitSection | React.FC | React component to provide submit button or submit event

#### FieldOptionsValueType

Property | Type | Default | Optional | Description
--- | --- | --- | --- | ---
name | string | field key name | true | name of the field
label | string | field key name | true | label for the form field
element | input, select, textarea | input[type=text] | true | HTML tag for the form field
type | input types (eg. text, number, email, checkbox etc) | text | true | type attribute for HTMLInputElement
component | FunctionalComponent | N/A | true | Pass your FunctionalComponent with props (FormFieldPropsType). `element` and `type` becomes redundant when `component` is used.
keyValues |  { [key: string]: any } | N/A | true | Only viable when element is `select`. This object provides the list of `<option value="key">value</option>`
className | string | N/A | true | CSS class(es) for the element
defaultValue | string | N/A | true | Default value for a field.
disabled | boolean | N/A | true | Disabled prop for inputs.
placeholder | string | N/A | true | Placeholder text for the input.
checked | boolean | N/A | true | Used for checkboxes to pass default value when type is `checkbox`.
checkboxText | string | N/A | true | Used for pass label for chebox element, normal label is being used for input lable.

#### FormFieldPropsType

Props below will be passed to your custom component.

Property | Type | Description
--- | --- | ---
options | FieldOptionsValueType | Options defined for field (See table above)
error | string | Error message to pass into component if the input is not valid
setValue | Function | N/A. For internal usage
value | any | N/A. For internal usage
checked | boolean | Passed when type is checkbox in place of Value.
name | string | N/A. For internal usage

Development
=============

In order to start dev server, type

```
yarn develop
```

it opens your browser with url `http://localhost:9100/`

License
=============

MIT

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