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

@westbrookdaniel/form

v0.1.1

Published

Framework agnostic uncontrolled form utilities

Downloads

65

Readme

@westbrookdaniel/form

Framework agnostic uncontrolled form utilities. Available on npm and via github for deno (but requires browser APIs).

Framework adapters are only currently available for React and Preact.

Guide (React)

This libraries React adapter is made up of:

  • useForm - Accepts an onSubmit callback and returns a ref for the form element.
  • useFormValues - Accepts a form ref and returns the current form values as state.
  • useInput - Creates ref to be given to an input element for a given name and provides a setter for its value.

Example (w/ tailwindcss on @tailwindcss/form example)

import {
  // Form is just a shorthand for React.RefObject<HTMLFormElement>
  Form,
  useForm,
  useFormValues,
  useInput,
} from '@westbrookdaniel/form/react'

function Form() {
  /**
   * The `form` needs given to <form /> as a ref
   * any form field with a `name` will be tracked as a part of the form
   *
   * These fields will be transformed into a state object and
   * where nested fields can be represented by dot notation like so:
   *
   * {
   *   "user": {
   *     "fullName": "",
   *     "email": ""
   *   },
   *   "date": "",
   *   "type": "Corporate event",
   *   "details": "",
   *   "subscribe": false,
   *   "manual": "A content editable div's value stored in a hidden input"
   * }
   *
   */
  const form = useForm({
    onSubmit: (state) => {
      console.log('submit', state)
    },
  })

  return (
    <main className="max-w-xl my-8 mx-auto">
      <form ref={form} className="space-y-3">
        <label className="block">
          <span className="text-gray-700">Full name</span>
          <input
            name="user.fullName"
            type="text"
            className="mt-1 block w-full"
            placeholder=""
          />
        </label>
        <label className="block">
          <span className="text-gray-700">Email address</span>
          <input
            name="user.email"
            type="email"
            className="mt-1 block w-full"
            placeholder="[email protected]"
          />
        </label>
        <label className="block">
          <span className="text-gray-700">When is your event?</span>
          <input name="date" type="date" className="mt-1 block w-full" />
        </label>
        <label className="block">
          <span className="text-gray-700">What type of event is it?</span>
          <select name="type" className="block w-full mt-1">
            <option>Corporate event</option>
            <option>Wedding</option>
            <option>Birthday</option>
            <option>Other</option>
          </select>
        </label>
        <label className="block">
          <span className="text-gray-700">Additional details</span>
          <textarea name="details" className="mt-1 block w-full" rows={3} />
        </label>
        <div className="block">
          <div className="mt-2">
            <div>
              <label className="inline-flex items-center">
                <input name="subscribe" type="checkbox" />
                <span className="ml-2">Email me news and special offers</span>
              </label>
            </div>
          </div>
        </div>
        <ManualInput />
        <button className="button" type="submit">
          Submit
        </button>
      </form>
      <Preview form={form} />
    </main>
  )
}

function ManualInput() {
  const defaultValue = "A content editable div's value stored in a hidden input"
  const [ref, setValue] = useInput()
  return (
    <label className="block">
      <input
        defaultValue={defaultValue}
        type="hidden"
        ref={ref}
        name="manual"
      />
      <span className="text-gray-700">Manual</span>
      <div
        className="mt-1 p-2 border border-gray-500"
        contentEditable
        onInput={(e) => {
          setValue(e.currentTarget.textContent?.toString() || '')
        }}
      >
        {defaultValue}
      </div>
    </label>
  )
}

function Preview({ form }: { form: Form }) {
  // This will only cause rerenders within this component
  const values = useFormValues(form, {
    // Second argument is optional and defaults to 'change'
    mode: 'blur',
  })

  return (
    <pre>
      <code>{JSON.stringify(values, null, 2)}</code>
    </pre>
  )
}

export default App

Guide (Preact)

The preact adapter is currently the same as react but using preact/hooks.