An open API service indexing awesome lists of open source software.

https://github.com/enterwell/react-form-builder

Form builder functionality for React
https://github.com/enterwell/react-form-builder

Last synced: about 1 month ago
JSON representation

Form builder functionality for React

Awesome Lists containing this project

README

        





logo


Enterwell React Form Builder


Simplifies form rendering. Doesn't mess with your components. Simple and straight to the point.

[![npm version](https://img.shields.io/npm/v/@enterwell/react-form-builder)](https://www.npmjs.com/package/@enterwell/react-form-builder)
[![Build and publish](https://github.com/Enterwell/react-form-builder/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/Enterwell/react-form-builder/actions/workflows/npm-publish.yml)
[![Bundle size](https://badgen.net/bundlephobia/minzip/@enterwell/react-form-builder)](https://bundlephobia.com/package/@enterwell/react-form-builder)

## Features

- All funtionality of [@enterwell/react-form-validation]
- Keeps form's state and validation results
- Supports any kind of validation functions
- Dirty checking
- Separates data from view
- Relies on hooks, but can easily be used with class components
- Any UI library
- Support for custom layouts

## Getting started

### Installation

```bash
pnpm add @enterwell/react-form-builder
```

### Usage

Somewhere at the beginning of your application register component provider (this is required, but all component types are optional). For Next.JS project this can be in `_app.tsx`.

```js
import { FormBuilderProvider } from '@enterwell/react-form-builder';

const components = {
string: memo((props) => ),
wrapper: (props) => ,
fieldWrapper: memo(() =>
};

...

```

Inside component create form definition render using ``.

```js
import { FormBuilder, useFormField } from '@enterwell/react-form-builder';
import { isNonEmptyString } from '@enterwell/react-form-validation';

const form = {
name: useFormField('', isNonEmptyString, 'string', 'Name'),
};

```

### API

For details, see TypeScript generated typings.

#### ``

Holds shared components collection.

Use this to wrap your top-most form content component to enable rendering components for requested types.

There is no pre-defined components, all types must be defined on per-project basis.

#### `useFormField(initialValue, validationFn, type, label, config)`

Defines the form field.

Shared some properties with @enterwell/react-form-validation, adds type and label. Type is used to select which component will be rendered, label is injected as component prop.

#### ``

Renders the form.

- `wrapper` type is provided in `FormBuilderProvider` components, it will be used to wrap all fields of the form.
- `wrapperField` type is provided in `FormBuilderProvider` components, it will be used to wrap all `FormBuilderField` that are rendered in the form.
- children are passed to this component, it will only render the wrapper and children.

#### ``

Renders the form field.

Fields use `FormBuilderProvider` to select component based on field type and injects some props into the components:

- value
- label
- error
- helperText
- onChange
- onKeyDown

### Advanced usage

#### Custom layout

Use `` manually position fields inside form using custom layout.
When children are present in form builder, automatic field and wrapper rendering is disabled - user is responsible for rendering fields.

```js
import { FormBuilderField } from '@enterwell/react-form-builder';




```

#### Override components

You can nest `FormBuilderProvider` inside other provider. The bottom provider will take all components from its parent and override matching with newly provided component types.

```js



```

## Development

For development:

```bash
pnpm install
pnpm dev
```

To builde production:

```bash
pnpm install
pnpm build
```