1.2.13 • Published 11 months ago

react-form-builder-helper v1.2.13

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

React Form Builder Helper

Overview

React Form Builder Helper is a lightweight package that helps you create dynamic form fields from JSON objects. It uses React Hook Form React hook form under the hood to provide real-time validation and a simple API for creating and managing form fields.

  • Note: Bootstrap is recomended

Features

  • Dynamic form fields: Create form fields dynamically from JSON objects
  • Real-time validation: Validate form fields in real-time using React Hook Form's built-in validation features
  • Easy to use: Simple and intuitive API for creating and managing form fields

Getting Started

Installation

To get started, install the package using npm:

npm i --save react-form-builder-helper

Peer Dependencies

This package requires React hook form as a peer dependency. Make sure to install it separately:

npm i --save react-hook-form

Documentation

You can see a usage documentation and interact with every kind of field form:

react-form-builder-helper

Usage

You have to import FieldPrinter component and use an array of field configurations to map them. Each field configuration is an object that defines the properties of the field, such as its tag, type, label, and validation rules.

Here's an example:

import { useForm } from 'react-hook-form'
import Field from 'react-form-builder-helper';

const fields = [
  {
    name: 'username',
    tag: 'input',
    label: 'Username',
    type: 'text',
    validations: {
      required: true,
      minLength: 3,
    }
  },
  {
    name: 'email',
    tag: 'input',
    label: 'Email',
    type: 'email',
    validations: {
      required: true,
    }
  }
];

const MyForm = () => {
  const form = useForm();

  const { register, control, handleSubmit, formState: { errors } } = form;
    
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      {fields.map((field, index) => (
        <Field
          key={index}
          field={field}
          form={form}
          error={errors[field.name]}
        />
      ))}
      <button type="submit">Submit</button>
    </form>
  );
};

Usage with DynamicFormBuilder

The DynamicFormBuilder component is an easy way to generate the entire form only passing some props. It takes a JSON object with the form fields and renders a dynamic form.

Here's an example of how to use:

import { DynamicFormBuilder } from 'react-form-builder-helper';

const formFields = [
    {
        label: "Name",
        name: "text",
        tag: "input",
        type: "text",
        placeholder: "Write name",
        validations: {
            required: true
        }
    },
    {
        label: "Type",
        name: "select",
        tag: "select",
        type: "simple",
        options: ["opcion1", "opcion2", "opcion3"],
        validations: {
            required: true
        }
    },
];

const MyForm = () => {
  const onSubmit = (data) => {
    console.log(data);
  };

  return (<div>
    <DynamicFormBuilder
      id="my-form"
      fields={formFields}
      onSubmit={onSubmit}
      onInvalidSubmit={(errors) => console.log(errors)}
    />
    
    <button form="id-form">Submit all</button>
  </div>);
};

DynamicFormBuilder Props

PropTypeDescription
idstringForm ID to use it in submit buttonRequired
fieldsarrayForm fields arrayRequired
defaultValuesobjectForm default values
onSubmitfunctionSubmit functionRequired
onInvalidSubmitfunctionCallback when form fields has errors
fieldWrapperobjectComponent to wrap every one form field
fieldComponentsobjectCustom form field component type
saveTemporalDatafunctionCallback for storage data before component unmounth
useFormPropsobjectExtra props to useForm hook
classNamestringClasses for main form container. Default: "row row-gap-3"

fieldWrapper

PropTypeDescription
componentComponentTypeWrapper component
props{ className?: string } & I_JsonObjectProps for wrapper component

fieldComponents

PropTypeDescription
labelComponentTypeSpecific component for labels of form fields
componentComponentTypeSpecific component for any form field
1.2.13

11 months ago

1.2.12

11 months ago

1.2.11

11 months ago

1.2.10

11 months ago

1.2.9

11 months ago

1.2.8

12 months ago

1.2.7

12 months ago

1.2.6

12 months ago

1.2.5

12 months ago

1.2.4

12 months ago

1.2.3

12 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago