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-generated-form

v0.1.3

Published

<!-- omit in toc --> # React JSON Form > Create forms with ease

Downloads

58

Readme

React JSON Form

Create forms with ease

Table of Contents

Under The Hood

Unde the hood this packages uses react-hook-form to validate components and bootstrap for the style.

Installation

  npm install react-generated-form

  # or

  yarn add react-generated-form

Usage

import { useForm, FormProvider } from 'react-hook-form'

// Import your CSS on top
import 'bootstrap/dist/bootstrap.min.css';

// Or just import component style
import 'react-generated-form/dist/style.css'

import {
  GeneratedForm,
  FormStructure,
  GeneratedFormConfigProvider,
} from 'react-generated-form'

type FormData = {
  firstName: string;
  lastName: string;
  email: string;
  password: string;
}

const formStructure : FormStructure<FormData> = [
  // Section 1, Name and Surname
  [
    {
      name: 'firstName', // keyof FormData
      label: 'First Name',
      placeholder: 'John',
      required: true, // by default the error label will be `Field ${label} is required!`
      xs: 12, // 1 input per row on mobile
      md: 6 // 2 inputs on the same row if not mobile.
    },
    {
      name: 'lastName',
      label: 'Last Name',
      placeholder: 'John',
      required: true, // by default the error label will be `Field ${label} is required!`,
      xs: 12,
      md: 6
    },
  ],
  // Section 2, Email
  [
    {
      name: 'email',
      label: 'Email',
      placeholder: '[email protected]',
      required: true,
      type: 'email', // Specify input type,
      xs: 12 // We can skip this, it's the default behaviour
    }
  ],

  // Section 3, Password and Validation
  [
    {
      name: 'password',
      label: 'Password',
      placeholder: '*********',
      hint: 'Must be 8-16 characters.', // You can also add an hint
      required: true,
      validator: { // refer to react-hook-form API
        minLength: {
          value: 8,
          message: 'The password must be at least 8 characters.'
        },
        maxLength: {
          value: 16,
          message: 'The password must be maximum 16 characters.'
        }
      },
      xs: 12,
      md: 6
    },
    {
      name: 'password_confirm', // with the suffix `_confirm` the form auto validates the fields, so you don't need to manually check values.
      label: 'Confirm Password',
      required: true,
      xs: 12,
      md: 6
    }
  ]


  function Form() {
    const methods = useForm<FormData>({
      mode: 'onSubmit'
    })

    const onSubmit = useCallback(...);

    return (
      <form onSubmit={methods.handleSubmit(onSubmit)}>
        <FormProvider {...methods}>
          <GeneratedForm<FormData> structure={formStructure}>
        </FormProvider>

        <div className='form-group'>
          <button type='submit'>
            Sign Up
          </button>
        </div>
      </form>
    )
  }


  {/* Later */}
  {/* Example with bootstrap classes */}
  ReactDOM.render(
    <GeneratedFormConfigProvider
      value={{
        input: 'form-control w-100',
        inputGroup: 'd-flex flex-column mb-3',
        label: 'mb-1',
        error: 'text-danger',
        hint: 'text-muted',
        row: 'row',
        sizeClasses: {
          lg: 'col-lg-$',
          md: 'col-md-$',
          xs: 'col-sm-$'
        }
      }}
    >
      <Form />
    </GeneratedFormConfigProvider>
  )
]

Docs

(work in progress)

Field Props

label: string

The label of the input

name: string

Name of the input (will be the key in the json)

type?: <all the standard input types> | time | coords | select | checkbox - by default is text

Type of the input, some types such as the email have special validation.

placeholder?: string

Placeholder of the input

hint?: string

A small text under the input

noHint?: boolean

Hide the hint

required?: boolean | string

Mark a field as required, the default error text will be Field {label} is required.

validator: {}

Some options to validate the value

when?: (fields: FormFields) => boolean

The input will be hidden until this function returns true

watch?: string[]

Tell the input which values to watch for the when function.

xs?: number

Column size on mobile

md?: number

Column size on tablet

lg?: number

Column size on desktop

Contributing

Feel free to contribute in any way, or just open an issue ✌️