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

@new-knowledge/make-form

v1.1.11

Published

A higher-order component for making forms in React

Downloads

79

Readme

makeForm - it makes a form, in React.

Main purposes:

  • Keep track of form state
  • Expose getter/setter methods to the form state
  • Allow developers to declaratively specify form behavior
  • Validate form fields
  • Mask user input

The FormFields object

The behavior of your form is determined by a defining a FormFields object. Each key is the name of a field in your form, each value is an object describing that field. For example, if we wanted a field named "age" with a default value of 20, our FormFields object would be created like so:

const ageFormFields ={
	age: {value: 20}
};

Options that can be passed in to makeForm include:

  • value - String or number. Initial value for the field. Defaults to empty string.
  • validator - Function. Receives input value and returns an error message. Returns empty string by default.
  • accept - Function. Receives input value and returns boolean. Return true to allow input. Returns true by default.
  • reject - Function. Opposite of accept, return true to block input.
  • maskInput - String. Formats user output. "(xxx) xxx-xxxx" turns "1234567890" into "(123) 456-7890".

The makeForm function

Pass your handwritten form component into the makeForm function, along with a FormFields object created by setupFields, and it will return your component decorated with the following functions as props:

  • setSubmit - Takes in your submit handler to be called after form validations pass. You should call it in componentWillMount to register your submit handler:
componentWillMount() {
    this.props.setHandler(this.handleSubmit);	
}
  • handleSubmit - If all fields are valid, this will call the handler you (hopefully) set when the component mounted.
  • resetForm - Resets the form to its initial state.
  • prefillForm - Takes in an object where the keys are field names and the values are field values, and prefills the form with those values. A form can only be prefilled once until it is reset.
  • setFields - Manually update the fields inside of the form state.
  • setFieldValue - Takes in a field name and new value.

The formContext object

The formContext object is provided as a prop to your form component, it contains methods which access the state of the form. These methods include:

  • getFields - The fields of the form state, which contain current values, error messages, etc.
  • getShowErrors - Is true after a submission is attempted, false after reset.
  • getHandleChange - Takes in field name, returns its change handler.
  • getValue - Takes in field name.
  • get Error - Take in field name.

By passing in a "name" prop and the form context, you can write a stateless input component that is connected to the form state:

const TextInput = ({name, formContext}) => {
	const { getHandleChange, getValue, getShowErrors, getError } = formContext;
	const handleChange = getHandleChange(name);
    const showErrors = getShowErrors();
    const value = getValue(name);
    const error = getError(name);
    
	return (
    	<div>
		    <input name={name} value={value} type={text}/>
            {showErrors && error &&
            	<p>{error}</p>
            }
        </div>
    )
}