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

helpful-form-builder

v0.1.6

Published

Generate basic React-Bootstrap forms which can be generated from a JSON object and dropped into any React component.

Downloads

57

Readme

Helpful Form Builder

Dynamic react form generator using react-bootstrap

General

This is a collection of React components (currently using React-Bootstrap...see Coming Soon... below) for input fields that can dynamically generate any form type given a set of current input values and a set of options for each of the selector type inputs. Since much of form building is basic boilerplate, this library allows you to simply pass in a JSON object indicating what the current values for each field are. For fields that have a finite number of options, a second JSON object can be passed in with the possible values for that field and it will be rendered as the respective input field.

Getting Started

  • Add helpful-form-builder as a dependency to your project
npm install --save helpful-form-builder
  • Require the main FormBuilder component wherever you decide to use the form builder
import FormBuilder from 'helpful-form-builder';
  • Add the FormBuilder component in your render function of the component passing in a JSON object of attributes to put in the form, the JSON object of selector values, a save handler and a delete handler.
export default class CustomComponent extends React.Component {

    ...
    savePressed() {
        //Save your data...
    }

    deletePressed() {
        //Delete the object here...
    }

    render() {

        // This can come from props or wherever the developer generates this data.
        const formValues = {
            FirstName: "Doctor"
            LastName: "Who",
            FavoritePlanet: "Earth"
        };

        const formOptions = {
            FavoritePlanet: ["Earth", "Gallifrey", "Jupiter"]
        };

        return (
            <div>
                <FormBuilder
                    editAttributes={formValues}
                    selectorOptions={formOptions}
                    onSavePressed={this.savePressed.bind(this)}
                    onDeletePressed={this.deletePressed.bind(this)}/>
            </div>
        )
    }
}

Generator Logic

The Form Builder makes some educated guesses about how data should be rendered in a form. It is important to understand how these logical decisions made in order to render the desired form.

Infinite vs. Finite Input Options

The first decision is made when the Form Builder looks at the two objects passed to it. If an input field in the editAttributes object has a matching value in the selectorOptions object, then it is assumed that the user should be presented with some type of selector field that limits the available input options. In the above example, because the form attribute FavoritePlanet is found in both formValues and formOptions it can be assumed that the FavoritePlanet field should be rendered as one of the following:

<select>...</select>
<select multiple>...</select>
<radio>...</radio>
<checkbox>...</checkbox>

In contrast, the FirstName attribute is not found in the selectorOptions object, therefore it is assumed that this field should be rendered as a type of text input.

<input type="text" .../>
<input type="field" .../>

Single Select vs. Multiple Select

The next decision point is whether to allow the user to select a single value or multiple values. The Form Builder distinguishes these two use cases by looking at the type of the value for the input attribute. Using the above example again, FavoritePlanet is of type String, therefore it renders the input as a single select.

<select>...</select>
<radio>...</radio>

If, however, the value is of type Array, Form builder assumes that the user should be allowed to select multiple options and will render one of the following.

<select multiple>...</select>
<checkbox>...</checkbox>

Few Options vs. Many Options

The next branching point looks at the number of options passed for a given selector field. Since it is undesirable to render a radio or checkbox with a massive amount of options and likewise a dropdown menu with only two options, the Form Builder sets an arbitrary cutoff when the number of options reaches five. When the length of options goes beyond four, the input field is rendered as a drop down selector instead of a radio or checkbox selector.

Single Line Text vs. Multiple Line Text

Finally, a distinction is made between the text input and text field. If the value for a given text attribute contains either a carriage return or new line character then it is assumed that the text should be rendered in a text field box. Also, if the text length exceeds 50 characters the same assumption is made.

NOTE: Currently only distinguishes between text and text-field but password, email, etc. are on the todolist. This criteria is likely to change as this decision point is fleshed out into different text inputs and made more flexible.

Coming soon...

  • Decouple React-Bootstrap components so that styles can be added by the developer
  • Support for file upload field
  • More flexible event handlers and form validation functions
  • More flexibility for developers to pass in input parameters i.e. placeholder text, validation functions, etc.