npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dynamic-form-v2

v2.0.2

Published

React library for dynamic form generation using MUI components and Formik

Downloads

53

Readme

React Dynamic Form v2

react-dynamic-form-v2 is a React library for dynamically generating forms using JSON configurations. It leverages Material-UI (MUI) components and Formik for form handling and validation.

Features

  • Dynamic Form Generation: Create forms on the fly using a JSON configuration.
  • Material-UI Integration: Utilizes MUI components for a consistent and modern UI.
  • Formik Integration: Built-in form handling, validation, and error management.
  • Custom Validation: Supports custom validation rules using Yup.

Installation

You can install the library via npm:

npm install react-dynamic-form-v2

Usage

Here's a basic example of how to use the react-dynamic-form-v2 library:

import React from 'react'; 
import { DynamicForm } from 'react-dynamic-form-v2'; 

const formJson = {
  "form_data":[
   {
      "key":"name",
      "type":"input",
      "props":{
         "placeholder":"Enter name",
         "pattern":"^[a-z A-Z 0-9\\s]{1,50}$",
         "type":"text",
         "required":true,
         "label":"Name",
         "minLength":1,
         "maxLength":50
      }
   },
   {
      "key":"address",
      "type":"select",
      "defaultValue":"",
      "props":{
         "label":"Location",
         "options":[
            {
               "value":"40acb6ff-3f2f-4b95-9ef9-c15c8d050bbe",
               "label":"New Delhi"
            }
         ]
      }
   },
   {
      "key":"telephone",
      "type":"tel",
      "defaultValue":"",
      "props":{
         "placeholder":"Enter mobile number",
         "label":"Mobile"
      }
   },
   {
      "key":"states",
      "type":"autocomplete",
      "props":{
         "label":"States",
         "placeholder":"Type to search",
         "required":true,
         "options":[
            "Alabama",
            "Alaska",
            "American Samoa",
            "Arizona",
            "Arkansas",
            "California",
            "Colorado",
            "Connecticut",
            "Delaware"
         ]
      }
   },
   {
      "key":"description",
      "type":"textarea",
      "templateOptions":{
         "label":"Description",
         "placeholder":"Enter your comments",
         "required":true,
         "rows":4,
         "cols":50
      }
   },
   {
      "key":"dob",
      "type":"date",
      "templateOptions":{
         "label":"Date of Birth",
         "placeholder":"Select your date of birth",
         "required":true,
         "minDate":"1900-01-01",
         "maxDate":"2023-12-31"
      }
   },
   {
      "key":"gender",
      "type":"radio",
      "templateOptions":{
         "label":"Gender",
         "required":true,
         "options":[
            {
               "label":"Male",
               "value":"male"
            },
            {
               "label":"Female",
               "value":"female"
            },
            
         ]
      }
   },
   {
      "key":"agreeTerms",
      "type":"checkbox",
      "templateOptions":{
         "label":"I agree to the terms and conditions",
         "required":true
      }
   }
 ]
};

export default function App() {
  const onSubmit = (data) => {
    console.log("Form data: ", data);
  };

  const breakpoints = {
    xs: 12,
    md: 6,
    lg: 6
  };

  return (
   <div className="App">
      <h1>Hello Dynamic Form</h1>
      <DynamicForm
         formJson={formJson}
         buttonClick={onSubmit}
         breakpoints={breakpoints}
         formValues={apiData}
      >
         <button type='submit'>Submit</button>
      </DynamicForm>
   </div>
  );
}

Form values

To pre-populate form fields with data from an API, you can pass an formValues prop to the DynamicForm component. The keys in the formValues object should match the key values in the formJson to ensure correct field mapping.

Example of formValues:

const apiData = {
  legal_name: "Your name",
  address: "test",
  telephone: "+9190******23",
  states: "Alaska",
  description: "Test description",
  dob: "1990-01-01",
  gender: "male",
};

Breakpoints

breakpoints The breakpoints prop allows you to control how each field is displayed on different screen sizes (xs, md, lg, xl).

JSON Schema

The formJson object structure is as follows:

Common Field Properties:

Each field object in the form_data array should contain:

  • variant: The variant of input (e.g., outlined, filled, standard) by default is outlined.
  • key: A unique identifier for the form field.
  • type: The type of input (e.g., input, select, tel).
  • props: An object containing the properties for the form field.
  • label: The label for the field.
  • placeholder: The placeholder text.
  • type: The input type (e.g., text, tel).
  • required: Boolean indicating whether the field is required.
  • minLength: Minimum length for input.
  • maxLength: Maximum length for input.
  • pattern: A regex pattern for validation.
  • options: (For select fields) An array of option objects, each with a value and label.

Template Options (for Radio, Checkbox, Textarea, Date)

For certain types like radio, checkbox, textarea, and date, the templateOptions are used:

  • label: The label for the field.
  • required: Boolean indicating whether the field is required.
  • options: (For radio and checkbox) Array of options, each with a label and value.
  • rows: (For textarea) Number of rows for the textarea.
  • cols: (For textarea) Number of columns for the textarea.
  • minDate: (For date) The minimum allowed date.
  • maxDate: (For date) The maximum allowed date.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Explanation of Updates:

  • formValues Prop: The formValues object is added as a prop to DynamicForm to pre-populate the form fields. This is reflected in the example usage and explained in the documentation.
  • Form Configuration: Details about the structure of the formJson and how it can be used to define form fields dynamically.
  • Breakpoints: Added a section explaining how the breakpoints prop works to make the form responsive.

Let me know if you want further adjustments!