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-auto-formgenerator

v1.2.1

Published

React component to auto generate forms using JSON objects

Downloads

20

Readme

React-Auto-FormGenerator

A form generator for React. If you don't want to write lot of forms then use this component to generate forms by providing array of fields and it will generate forms from it.

See react-auto-formgenerator for live storybook.

Installation and usage

The easiest way to react-auto-formgenerator is to install it from npm and build it into your app with Webpack.

npm install react-auto-formgenerator

Then use if in your app:

With React Component

import { Card, CardBody, CardHeader } from 'reactstrap';
import { FormGenerator } from 'react-auto-formgenerator';

function App() {

  const fields = {
    username: {
      type: 'text',
      label: 'Username',
      name: 'username',
      col: 6,
      className: 'userinput'
    },
    email: {
      type: 'email',
      label: 'Email',
      name: 'email',
      col: 6
    },
    password: {
      type: 'password',
      label: 'Password',
      name: 'password',
      col: 6
    },
    role: {
      type: 'advanceSelect',
      label: 'Role',
      name: 'role',
      options: [
        {
          value: 'admin',
          label: 'Admin'
        },
        {
          value: 'customer',
          label: 'Customer'
        }
      ],
      col: 6
    },
    product: {
      type: 'advanceSelect',
      label: 'Product',
      name: 'product',
      target: 'http://localhost:5000/products',
      optionValue: 'id',
      optionLabel: 'name',
      multi: false,
      col: 6
    },
    file: {
      type: 'file',
      label: 'Image',
      name: 'file',
      col: 6
    },
    active: {
      type: 'switch',
      label: 'Active',
      name: 'active'
    },
    educationSection: {
    label: 'Education',
    type: 'section',
    name: 'educationSection',
    show: false,
    fields: {
      school: {
        type: 'text',
        label: 'Name of School/College',
        name: 'school',
        col: 12
      }
    }
  }
  }

  return (
    <div className='row w-100 vh-90 pt-5'>
      <div className='col-10 mx-auto'>
        <Card>
          <CardHeader>
            <strong>Add User</strong>
          </CardHeader>
          <CardBody>
            <FormGenerator 
              fields={fields}
              idKey='_id'
              apiUrl='http://localhost:5000' 
              entity='users'
              showToast={true}
              submitCb={data => console.log('form submitted', data)}
            />
          </CardBody>
        </Card>
      </div>
    </div>
  );
}

Schema for sections, these sections act as collapsible elements which render fields.

export const sections = {
  educationSection: {
    label: 'Education',
    type: 'section',
    name: 'educationSection',
    show: false,
    fields: {
      school: {
        type: 'text',
        label: 'Name of School/College',
        name: 'school',
        col: 12
      }
      ...
    }
  }
}

Schema for dynamic fields, these fields are added inside sections for better UI.

export const dynamicFields = {
  groupPrices: {
    label: 'Group Prices',
    type: 'repeater',
    name: 'groupPrices',
    newBtnLabel: 'Add Group Price',
    fields: {
      group: {
        type: 'advanceSelect',
        placeholder: 'Group',
        name: 'group',
        options: [
          {
            value: 'all',
            label: 'All groups'
          },
          {
            value: 'guest',
            label: 'Guest'
          }
        ],
        col: 4,
      },
      qty: {
        type: 'number',
        placeholder: 'Qty',
        name: 'qty',
        col: 4
      },
      discount: {
        type: 'number',
        placeholder: 'Discount',
        name: 'discount',
        col: 3
      }
    }
  }
}

Props

Common props you may want to specify include:

  • idKey - id key name which is used in your database, for MongoDB it is generally _id
  • apiUrl - your base api url
  • fields - object of fields which will generate form
  • entity - endpoint entity
  • targetId - record id for edit case
  • submitCb - callback function which will trigger after success submit of form
  • showToast - to display default toast
  • formClassName - class for form
  • btnClassName - class for form button

Note

This component handles server side errors as well, but errors should be in this format:

{
  "errors": {
    "email": "Invalid email",
    "username": "This field is required",
    "password": "Password should be between 4-20 characters",
    ...
  }
}

If you like React-Auto-Formgenerator, please give it a star!