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

use-simple-form

v1.0.2

Published

simple form hook

Downloads

2

Readme

use-simple-form

use-simple-form is a React hook for handling state in simple form applications. It supports nested object and field validation right out of the box.

Usage

import React from "react";
import useSimpleForm from "use-simple-form";

function FormComponent() {
  const [form, updateForm] = useSimpleForm({
    name: "",
    email: "",
    address: {
      street: {
        line1: "",
        line2: ""
      },
      state: "",
      zipCode: ""
    }
  });

  const zipValidator = zip => /^\d+$/.test(zip.trim());

  return (
    <form>
      <input
        name="name"
        placeholder="Name"
        value={form.name}
        onChange={updateForm()}
      />
      <input
        name="email"
        placeholder="Email Address"
        value={form.email}
        onChange={updateForm()}
      />
      <input
        name="line1"
        placeholder="Street"
        value={form.address.street.line1}
        onChange={updateForm({ parent: "address.street" })}
      />
      <input
        name="line2"
        placeholder="Apt/Unit Number"
        value={form.address.street.line2}
        onChange={updateForm({ parent: "address.street" })}
      />
      <input
        name="state"
        placeholder="State"
        value={form.address.state}
        onChange={updateForm({ parent: "address" })}
      />
      <input
        name="zipCode"
        placeholder="Zip Code"
        value={form.address.zipCode}
        onChange={updateForm({ validator: zipValidator, parent: "address" })}
      />
    </form>
  );
}

Only input's are used in this example but the hook works fine with any element that will return a value from event.target.value during onChange, like select or textarea.

API

Hook Definition

To use the hook simple give the intitial state object to the useSimpleForm hook function. Make sure you define all of the nested objects you want to use in the form in your intial state!

useSimpleForm({
  /*Initial State Object*/
});

useSimpleForm returns a 'tuple' containing the current form state in the 0th position and an updator function in the 1st position.

const [form, formUpdator] = useSimpleForm({});

Form Updator

The formUpdator is a function that returns the updator function so that you can inject configuration into the updator.

const formFieldUpdator = formUpdator({
  validator: value => {}, // Input validation function that runs before onChange
  parent: "path.to.parent.object" // Object path separated by '.' used to set nested values
});

The formUpdator function that is returned only expects the event object passed by onChange and uses event.target.name to find the field in the form state object and event.target.value to set the field in the state object. Because of this you must supply a name to your input components.

Validator

The validator expects a function that takes in the value input by the user and returns a boolean (valid = true / invalid = false). The validator then sets an addiditional value in your form state equivalent to the name + '_valid'.

// Heres an example from the usage section where we set an 'invalid' class based on this value
const zipValidator = zip => /^\d+$/.test(zip.trim());

<input
  name="zipCode"
  placeholder="Zip Code"
  value={form.address.zipCode}
  className={!form.address.zipCode_valid && 'invalid' }
  onChange={updateForm({
    validator: zipValidator,
    parent: "address"
  })}
/>;

Parent

The parent configuration variable must be a string of keys in the form state object separated by . periods.

const parent = 'x.y.z';
const obj = {
  x: {
    y: {
      z: {
        a: 'value'
      }
    }
  }
}

In this example the object defined by the parent string would be: { a: 'value' }.