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

@watts-lab/rapid-forms

v1.0.1

Published

This project is a minimalist framework to allow for rapid creation of forms. The philosophy of this framework is to be extremely unopionated with styling, be extremely flexible, and easy to use.

Downloads

1

Readme

Rapid Forms

This project is a minimalist framework to allow for rapid creation of forms. The philosophy of this framework is to be extremely unopionated with styling, be extremely flexible, and easy to use.

Package created and maintained by Watts Lab at the University of Pennsylvania

** Contributors ** Sumant R. Shringari

Installation

npm install @watts-lab/rapid-forms

Contributors

Contributing to the codebase

The code base is organized with two key components-- src/index.tsx and src/inputs/*.tsx. Index.tsx is a main entrypoint for the code and exports components that can be used when rapid-forms is imported. Currently, there is one key component that is exported which is the SinglePageForms. inputs/*.ts contains all the individual input components that are used in Forms. Every input should be individually tested. No PR will be accepted without a test for your input

Publishing to npm with np

We use np for publshing to npm Install with the following command npm install --global np To Publish, simply np in the root directory

Usage

Styling

All forms come unformated. To format, these inputs with your css stylings, you can override global inputs such as

input[type="radio"] {
    // your css
}

Or you can override them through rapid forms classname in your css style sheets. This is an exhaustive list

.rapid-form-single-page {
   // your css
}

.rapid-forms-input-block {
   // your css
}

.rapid-forms-title {
    // your css
}

.rapid-forms-description {
    // your css
}

.rapid-forms-submit {
    // your css
}

.rapid-forms-checkbox-input {
    // your css
}

.rapid-forms-checkbox-label {
    // your css
}

.rapid-forms-radio-input {
    // your css
}

.rapid-forms-radio-label {
    // your css
}

Single Page Form

Questions

The format that a question takes is that of below. (? marks optional elements)

{
    "type": the type of input (currently only support radio and checkbox),
    "name": the name of the input note for (checkboxes each choice will have unique name as (name-index))
    "title": the question that you should be asking   
    "description"?: Text that appears below the title and accepts html as input
    "choices"?: array of string of choices (only applicable to radio groups and checkboxes)
}

Here is an example.

  const questions: Element[] = [
    {
      type: 'radio',
      name: 'color',
      title: 'What is your favorite color?',
      description: (<> <p> Let us know if we should add more </p> </>),
      choices: ['Red', 'Blue', 'Yellow'],
    },
    {
      type: 'checkbox',
      name: 'color-dont',
      title: 'Select all colors that you do not like',
      description: (<> <p> Let us know if we should add more </p> </>),
      choices: ['Red', 'Blue', 'Yellow'],
    },
  ]

Event handlers

Rapid-Forms is extremely unopinonated on how you handle your data and submission. So, you must define onChange and onSubmit function. We use a reference from react to keep track of data for changes in state.

Note that checkbox and radio groups will have checked element and must be handled seperately.

const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const value = event.target.value
    const name = event.target.name
    if (event.target.checked !== undefined) {
        data.current[name] = event.target.checked ? value : null
    } else {
        data.current[name] = value
    } 
}

On submission, the reference data will have all the information

const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    // do something with data
    setSubmitted(true)
    console.log(data.current)
}

Render Component

<SinglePageForm elements={questions} onChange={onChange} onSubmit={onSubmit} />

Full Code

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { SinglePageForm, Element } from '../.';

const App = () => {
  const data = React.useRef<Record<string, any>>({});
  const [submitted, setSubmitted] = React.useState(false);
  const questions: Element[] = [
    {
      type: 'radio',
      name: 'color',
      title: 'What is your favorite color?',
      description: (<> <p> Let us know if we should add more </p> </>),
      choices: ['Red', 'Blue', 'Yellow'],
    },
    {
      type: 'checkbox',
      name: 'color-dont',
      title: 'Select all colors that you do not like',
      description: (<> <p> Let us know if we should add more </p> </>),
      choices: ['Red', 'Blue', 'Yellow'],
    },
  ]

  const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const value = event.target.value
    const name = event.target.name
    if (event.target.checked !== undefined) {
      data.current[name] = event.target.checked ? value : null
    } else {
      data.current[name] = value
    } 
  }

  const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    // do something with data
    setSubmitted(true)
    console.log(data.current)
  }

  return (
    <>
      { 
        !submitted ? 
        <SinglePageForm elements={questions} onChange={onChange} onSubmit={onSubmit} />
        : <>Thank you for submitting! </>
      }
    </>
  )
  
};

ReactDOM.render(<App />, document.getElementById('root'));