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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-formify

v0.11.0

Published

An uncontrolled react Form component with validation capabilities.

Downloads

65

Readme

Slack Build Status Test Coverage Dependency Status devDependency Status

react-formify

An uncontrolled react Form component with validation capabilities.

Install

npm install react-formify

or

yarn add react-formify

Usage

import React from 'react';
import Form from 'react-formify';

const emailExpression = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

const rules = {
  email: (value) => {
    if (!value || value === '') {
      return 'Email is required';
    } else if (!emailExpression.test(value)) {
      return 'Email is not valid';
    }
    return undefined;
  },
  name: 'Name is required',
};

const UserForm = () => (
  <Form
    defaultErrors={{ email: 'This is an existing email' }}
    defaultValue={{ email: '[email protected]' }}
    onSubmit={(user, reset) => {
      reset();
      alert(JSON.stringify(user, null, 2));
    }}
    rules={rules}
  >
    {(state, errors, isValid) => (
      <fieldset>
        <input {...state.name} />
        {errors.name ? <span className="error">{errors.name}</span> : undefined}
        <input {...state.email} />
        <input {...state.get('optional.field')} />
        {errors.email ? <span className="error">{errors.email}</span> : undefined}
        <button type='submit'>Add</button>
        <button
          type='button'
          onClick={() => {
            state.set('name', 'Alan Test');
            state.set({
              email: '[email protected]',
            });
          }}
          disabled={isValid}
        >
          Set Default
        </button>
      </fieldset>
    )}
  </Form>
);
  • NOTE: :warning: it is the caller responsibility to add some control to submit the form. In this example we are adding a submit button.

Try

Form Props (API)

children

Required. A function that will be invoked with state and errors. State is an object that you can get the form properties per key defined in rules. For example, these rules:

const rules = {
  email: (value) => {
    if (!value || value === '') {
      return 'Email is required';
    } else if (!emailExpression.test(value)) {
      return 'Email is not valid';
    }
    return undefined;
  },
  name: 'Name is required',
};

will generate the following state:

{
  email: {
    name: 'email',
    value: '[email protected]',
    onChange: (event) => { ... },
  },
  name: {
    name: 'name',
    value: '',
    onChange: (event) => { ... },
  }
}

This approach of holding the values in a raw object allows you to use any form element you want. You can use <input /> from React, or <TextInput /> from Grommet. All you need to do is to make sure to call {...state.name} where name is the property you want to assign to a given form element.

  • errors object will hold the errors in a given form element. The errors will be present if you passed some defaultErrors or when the form is submitted.

You can call state.get('optional') to get the form properties object for fields that do not have any validation criteria. Similarly, you can call errors.get('address.street.home') to get the error of a given nested property in the object, returning undefined otherwise.

You can also call state.set('key', value) or state.set({ key: value, key2: value }) to update react-formify resource value.

defaultErrors

An object with default errors to show when the form is rendered.

defaultValue

An object with default values to show when the form is rendered.

onSubmit

Required. A function that will be invoked when the form is submitted and valid. The object (resource) is passed as the first argument to the function. The second argument is a reset function that, upon called, will reset the form state.

onError

Optional. A function that will be invoked when an error happens in the form. This function will pass all the current errors in the callback.

onChange

Optional. A function that will be invoked when any form element is changed. This function will pass an object that contains what has been changed.

reset

Optional. Whether or not the form should reset to its original state.

rules

An object or function that will validate the form based on predefined rules. If rules is a function, it will be invoked when validation is needed. The function passes the resource as the argument so that the caller can decide which set of rules to return. This can be useful when you want a completely different set of rules depending on a given selection in the form, for example:

const userRules = (user) => {
  const defaultValidation = {
    email: (value) => {
      if (!value || value === '') {
        return 'Email is required';
      } else if (!emailExpression.test(value)) {
        return 'Email is not valid';
      }
      return undefined;
    },
    name: 'Name is required',
    size: 'Size is required',
    confirm: 'Please confirm answers',
    address: {
      home: {
        street: 'Street is required',
      },
    },
  };
  if (
    user.address &&
    user.address.home &&
    user.address.home.street &&
    user.address.home.street !== ''
  ) {
    return deepMerge(defaultValidation, {
      address: {
        city: 'City is required if you provided street',
      },
    });
  }
  return defaultValidation;
};

In this example, city will be required if street name is provided.

Build

To build this library, execute the following commands:

  1. Install NPM modules
$ npm install (or yarn install)
  1. Run pack
$ npm run dist
  1. Test and run linters:
$ npm run check