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

@presidenttree94/form-utils

v1.0.9

Published

[![npm version](https://img.shields.io/npm/v/@presidenttree94/form-utils)](https://www.npmjs.com/package/@presidenttree94/form-utils) [![npm downloads](https://img.shields.io/npm/dm/@presidenttree94/form-utils)](https://www.npmjs.com/package/@presidenttre

Readme

@presidenttree94/form-utils

npm version npm downloads license TypeScript bundle size

A tiny, type‑safe utility that turns form elements, objects, and schema into a fully typed form system with parsing and update helpers.

The problem

Without this package, wiring up a form with multiple fields means juggling individual useState calls and manual onChange handlers for each one:

function ProfileForm() {
  const [name, setName] = useState("");
  const [age, setAge] = useState(0);
  const [role, setRole] = useState("viewer");

  const handleSubmit = () => {
    console.log({ name, age, role });
  };

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Name
        <input
          type="text"
          value={name}
          onChange={e => setName(e.target.value)}
        />
      </label>
      <label>
        Age
        <input
          type="number"
          value={age}
          onChange={e => setAge(Number(e.target.value))}
        />
      </label>
      <label>
        Role
        <select value={role} onChange={e => setRole(e.target.value)}>
          <option value="viewer">Viewer</option>
          <option value="editor">Editor</option>
          <option value="admin">Admin</option>
        </select>
      </label>
      <button type="submit">Save</button>
    </form>
  );
}

This gets tedious fast — every field needs its own state, its own handler, and manual type coercion.

Who This Package Is For

This library is designed for developers who want strongly typed, schema‑driven form state without adopting a full form framework. It’s a great fit if you:

  • Prefer type‑safe form state that updates automatically as your schema changes
  • Want to avoid writing one useState per field or manually parsing values from inputs
  • Like the idea of defining your form once and getting helpers, parsing, and state management for free
  • Need a tiny, dependency‑free utility that plays nicely with React but doesn’t lock you into any framework
  • Want predictable, explicit control over your form logic without magic or hidden behavior

Installation

Run npm install @presidenttree94/form-utils in the terminal.

Using useForm

useForm combines state management and element binding into a single call. You define a schema once, and each field gets a value and a setValue that handles parsing for you.

import { useForm } from "@presidenttree94/form-utils";

function ProfileForm() {
  const { form, elements, reset } = useForm(
    { name: "", age: 0, role: "viewer" },
    {
      name: { label: "Name" }, // "text" type is default
      age:  { label: "Age",  type: "number" },
      role: { label: "Role", options: ["viewer", "editor", "admin"], defaultOption: "Select a role" },
    }
  );

  const handleSubmit = () => {
    console.log(form); // { name: string, age: number, role: string }
  };

  return (
    <form onSubmit={handleSubmit}>
      <label>
        {elements.name.label}
        <input
          type={elements.name.type}
          value={elements.name.value}
          onChange={e => elements.name.setValue(e.target.value)}
        />
      </label>
      <label>
        {elements.age.label}
        <input
          type={elements.age.type}
          value={elements.age.value}
          onChange={e => elements.age.setValue(e.target.value)}
        />
      </label>
      <label>
        {elements.role.label}
        <select
          value={elements.role.value}
          onChange={e => elements.role.setValue(e.target.value)}
        >
          <option disabled value="">{elements.role.defaultOption}</option>
          {elements.role.options.map(o => (
            <option key={o} value={o}>{o}</option>
          ))}
        </select>
      </label>
      <button type="submit">Save</button>
      <button type="button" onClick={reset}>Reset</button>
    </form>
  );
}

age is automatically parsed as a number via the type: "number" config — no manual Number() calls needed.

Using useFormState and buildFormElements separately

useForm is a convenience wrapper, but you can use the two underlying pieces independently. This is useful when you want to share form state across components, process data before showing the user, or derive elements at a different point in your render tree.

import { useFormState, buildFormElements } from "@presidenttree94/form-utils";

function ProfileForm() {
  // Step 1: manage state
  const { form, update, updateMany, reset } = useFormState({
    name: "",
    age: 0,
    role: "viewer",
  });

  // Step 2: build elements when needed
  const elements = buildFormElements(form, update, {
    name: { label: "Name" },
    age:  { label: "Age",  type: "number" },
    role: { label: "Role", options: ["viewer", "editor", "admin"], defaultOption: "Select a role" },
  });

  // Bulk-update multiple fields at once (e.g. pre-filling from an API response)
  const prefill = () => updateMany({ name: "Alice", age: 30 });

  const handleSubmit = () => {
    console.log(form);
  };

  return (
    <form onSubmit={handleSubmit}>
      {Object.entries(elements).map(([key, field]) => (
        field.options ?
        <label>
          {field.label}
          <select
            value={field.value}
            onChange={e => field.setValue(e.target.value)}
          >
            <option disabled value="">{field.defaultOption}</option>
            {field.options.map(o => (
              <option key={o} value={o}>{o}</option>
            ))}
          </select>
        </label>
        :
        <label>
          {field.label}
          <input
            type={field.type}
            value={field.value}
            onChange={e => field.setValue(e.target.value)}
          />
        </label>
      ))}
      <button type="submit">Save</button>
      <button type="button" onClick={prefill}>Pre-fill</button>
      <button type="button" onClick={reset}>Reset</button>
    </form>
  );
}

API

useForm(initial, schema)

Combines useFormState and buildFormElements. Returns { form, elements, update, updateMany, reset }.

useFormState(initial)

Manages form state. Returns:

  • form — current values
  • update(key, value) — update a single field
  • updateMany(partial) — update multiple fields at once
  • reset() — restore initial values

buildFormElements(form, update, schema)

Builds a map of form elements from the current state, update function, and schema. Each element extends its FieldConfig with:

  • value — the current field value
  • setValue(raw) — parses the raw string input and calls update

FieldConfig

| Property | Type | Description | |-----------------|-------------------------------------|--------------------------------------------------| | label | string | Display label for the field | | type | string | Input type ("text", "number", etc.) | | required | boolean | Whether the field is required | | options | Option[] | Available options for select fields | | multi | boolean | Whether multiple selections are allowed | | defaultOption | string | Placeholder text for the default option | | parse | (raw: string \| string[]) => Value | Custom parser, overrides the built-in type logic |