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

formgp

v1.0.4

Published

The simplest React form handler you can ever use. No components, just a simple hook that handles changes to your form values.

Downloads

1

Readme

FormGP

The simplest React form handler you can ever use. No components, just a simple hook that handles changes to your form values.

Wanted to use Formula1 or formulaone or Formula-One but those were taken. i cry 😭 😭 😭

Getting Started

Installing

# using npm
npm install formgp

# using yarn
yarn add formgp

Using

formgp exports a simple useForm hook that accepts an initialValue and some validators that you want to use. It then provides you with a values object that has everything you need for that particular value:

  • value: The actual value
  • error: Was there an error
  • dirty: Is the value dirty or touched

And some event handlers so that anything you type gets reflected. The main change handler, aptly named handleChange doesn't work on magic glue where it reads a property on the element like name. Instead when you use it, you have to specify what field you want changed. For example:

<input onChange={handleChange('someprop')}>

Nice thing about this is that everything is typed, so you get that nice auto complete when you run write handleChange

Let's go ahead with some examples.

Simple form

import { useForm } from 'formgp'

const MyForm = () => {
    const { values, handleChange } = useForm({ name: 'Daryl', nickName: 'Iceman'})
    return (
        <>
            <input 
                value={values.name.value} 
                onChange={handleChange('name')}
            />
            <br/>
            <input 
                value={values.nickName.value} 
                onChange={handleChange('nickName')}
            />
        </>
    )

}

Handling other events apart from change

The Simple Form example only uses the onChange event, but if you want to handle other events like onBlur (we still only support this hehe, teehee), you can use the eventHandlers function instead. Uses the same concept as handleChange where you have to explicitly specify what property you want to handle.

Under the hood, we're really just appending an onChange and onBlur handler to your input.

import { useForm } from 'formgp'

const MyForm = () => {
    const { values, handleChange } = useForm({ name: 'Daryl', nickName: 'Iceman'})
    return (
        <>
            <input 
                value={values.name.value} 
                {...eventHandlers('name')}
            />
            <br/>
            <input 
                value={values.nickName.value} 
                {...eventHandlers('nickName')}
            />
        </>
    )

}

Handling other events apart from change

The Simple Form example only uses the onChange event, but if you want to handle other events like onBlur (we still only support this hehe, teehee), you can use the eventHandlers function instead. Uses the same concept as handleChange where you have to explicitly specify what property you want to handle.

Under the hood, we're really just appending an onChange and onBlur handler to your input.

import { useForm } from 'formgp'

const MyForm = () => {
    const { values, handleChange } = useForm({ name: 'Daryl', nickName: 'Iceman'})
    return (
        <>
            <input 
                value={values.name.value} 
                {...eventHandlers('name')}
            />
            <br/>
            <input 
                value={values.nickName.value} 
                {...eventHandlers('nickName')}
            />
        </>
    )

}

Running validations

You can inject some validators to your form properties via the validator parameter in useForm. Unlike other libraries that lets you validate the entire form, with formgp it's done per property.

const initialValue = {
    name: 'Daryl',
    favoriteNumber: 0
} 

const validators = {
    // If for some reason you hate someone named Glenn, let's do some validation on that
    name: (val) => val === 'Glenn' ? 'No Glenn allowed' : undefined
    // If you insist they provide their favorite number
    favoriteNumber: (val) => !val ? 'Required' : undefined
}

const { values, handleChange } = useForm(initialValue, validators)

Let's use the snippet above, and show the errors below the input. Preferrably, you may want to check if the form is dirty first before you show the error, but hey if you don't wanna do that, the world is your oyster.

import { useForm } from 'formgp'

const MyForm = () => {
    const initialValue = {
        name: 'Daryl',
        favoriteNumber: 0
    } 

    const validators = {
        // If for some reason you hate someone named Glenn, let's do some validation on that
        name: (val) => val === 'Glenn' ? 'No Glenn allowed' : undefined
        // If you insist they provide their favorite number
        favoriteNumber: (val) => !val ? 'Required' : undefined
    }

    const { values, handleChange } = useForm(initialValue, validators)
    return (
        <>
            <input 
                value={values.name.value} 
                {...eventHandlers('name')}
            />
            <br/>
                {values.name.dirty && values.name.error && <label>{values.name.error}</label>}
            <br/>
            <br/>
            <input 
                type={'number'}
                value={values.favoriteNumber.value} 
                {...eventHandlers('favoriteNumber')}
            />
            <br/>
                {values.favoriteNumber.dirty && values.favoriteNumber.error && <label>{values.favoriteNumber.error}</label>}
        </>
    )

}

SEO

Some SEO to help me get searched on the wEbZ

#react #formgp #form #formhandler #deadsimple #simple #pleaseuseme #imuseful #imsimple

#BlazinglyFast (not actually but this is a hype word)