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

nws-dynamic-form

v1.0.8

Published

A React Dynamic Library for Form, Input

Downloads

26

Readme

nws-dynamic-form

Dynamic Form

This library helps you generate a simple Form with JSON input in React. Makes use of @mui/material to generate the styled input. The main purpose for creating this library is to deal with dynamic form problem. Dynamic form is a form whose input list can be changed for different purpose. By using this library, you can custom your form input list to json format without fixing it on the database.

Sponsored by

Netpower.no

Installation

Use NPM to install the library.

$ npm install nws-dynamic-form

Usage

import { JSONForm } from 'nws-dynamic-form';

const testFormData = JSON.stringify({
  label: 'Declaration',
  formFields: [
    {
      id: 'mass_purity_uncertainty',
      name: 'mass_purity_uncertainty',
      label: '1. Is there any uncertainty about the mass purity?',
      type: 'radio',
      options: [
        {
          label: 'No',
          value: 'no',
        },
        {
          label: 'Yes',
          value: 'yes',
        },
      ],
    },
    {
      id: 'mass_purity_uncertainty_explanation',
      name: 'mass_purity_uncertainty_explanation',
      label: 'Describe impurity',
      display: { mass_purity_uncertainty: 'yes' },
      multiline: true,
      rows: 4,
      variant: 'filled',
    },
    {
      id: 'mass_origination',
      name: 'mass_origination',
      label: '2. Where the mass is originating from?',
      type: 'checkbox',
      options: [
        {
          name: 'road_originating',
          label: 'Roads or railroads',
        },
        {
          name: 'industy_originating',
          label: 'Industrial, gas station, workshop or similar',
        },
        {
          name: 'city_originating',
          label: 'City area',
        },
      ],
    },
    {
      id: 'polution_completed',
      name: 'polution_completed',
      label: '3. Has the pollution analysis been completed?',
      type: 'radio',
      options: [
        {
          label: 'No',
          value: 'no',
        },
        {
          label: 'Yes',
          value: 'yes',
        },
      ],
    },
    {
      id: 'polution_completed_explanation',
      name: 'polution_completed_explanation',
      label: 'Describe why not',
      display: { polution_completed: 'no' },
      multiline: true,
      rows: 4,
      variant: 'filled',
    },
  ],
});

const defaultFormErrors = {
  mass_purity_uncertainty: false,
  mass_purity_uncertainty_explanation: false,
  mass_origination: false,
  polution_completed: false,
  polution_completed_explanation: false,
}

function App() {
  const [controlledFormValue, setControlledFormValue] = React.useState<any>({});
  const [formErrors, setFormErrors] = React.useState({
    mass_purity_uncertainty: false,
    mass_purity_uncertainty_explanation: false,
    mass_origination: false,
    polution_completed: false,
    polution_completed_explanation: false,
  });

  const onSubmit = (e: any) => {
    if (!validateForm()) return;
    console.log("Controlled form value: ", controlledFormValue);
  }

  const validateForm = () => {
    let flag = true;
    const newFormErrors = { ...defaultFormErrors };
    if (!controlledFormValue?.mass_purity_uncertainty) {
      newFormErrors.mass_purity_uncertainty = true;
      flag = false;
    }
    if (!controlledFormValue?.mass_purity_uncertainty_explanation) {
      newFormErrors.mass_purity_uncertainty_explanation = true;
      flag = false;
    }
    if (!controlledFormValue?.mass_origination?.road_originating) {
      newFormErrors.mass_origination = true;
      flag = false;
    }
    if (!controlledFormValue?.mass_origination?.industy_originating) {
      newFormErrors.mass_origination = true;
      flag = false;
    }
    if (!controlledFormValue?.mass_origination?.city_originating) {
      newFormErrors.mass_origination = true;
      flag = false;
    }
    if (!controlledFormValue?.polution_completed) {
      newFormErrors.polution_completed = true;
      flag = false;
    }
    if (!controlledFormValue?.polution_completed_explanation) {
      newFormErrors.polution_completed_explanation = true;
      flag = false;
    }
    setFormErrors(newFormErrors);
    return flag;
  }

  return (
    <div className="App">
      <div style={{ width: 500, padding: 20 }}>
        <JSONForm 
          formData={testFormData} 
          formValue={controlledFormValue}
          onChange={setControlledFormValue}
          formErrors={formErrors}
        />
        <button
          style={{
            marginTop: 10,
          }}
          onClick={onSubmit}
        >
          Submit
        </button>
      </div>
    </div>
  );
}