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-react-form-hook

v1.0.9

Published

>

Downloads

6

Readme

use-react-form-hook

NPM JavaScript Style Guide

Install

npm install --save use-react-form-hook

Usage

Add Page

import React from 'react'
import { useForm } from 'use-react-form-hook';
import { VALIDATOR_REQUIRE, VALIDATOR_MINLENGTH, VALIDATOR_EMAIL, validate } from './utils/validators';
import './index.css';

const App = () => {
  //Pass all field details in hook
  const [formState, inputHandler, setFormData] = useForm({
    name: {
      value: '',
      isValid: false,
    },
    email: {
      value: '',
      isValid: false
    },
    address: {
      value: '',
      isValid: false
    }

  }, false);

  const changeHandler = (event, valid) => {
    let { id, value } = event.target;
    inputHandler(id, value, validate(value, valid))
  };

  const touchHandler = (id) => {
    const { name, email, address } = formState.inputs;
    setFormData(
      {
        ...formState.inputs,
        [id]: {

          ...formState.inputs[id],
          isTouched: true
        },
      },
      email?.isValid && address?.isValid && name?.isValid
    );
  }

  const OnFormSubmit = (e) => {
    e.preventDefault();
    console.log(formState.inputs);

    alert(JSON.stringify(formState.inputs,null,4))
  }


  return (
    <div className='form-box'>
      <form onSubmit={OnFormSubmit}>
        <div
          className={`form-control ${!formState?.inputs?.name?.isValid && formState?.inputs?.name?.isTouched &&
            'form-control--invalid'}`}
        >

          <input
            id="name"
            type="text"
            onChange={(e) => { changeHandler(e, [VALIDATOR_REQUIRE()]) }}
            onBlur={() => { touchHandler('name') }}
          />
          {!formState?.inputs?.name?.isValid && formState?.inputs?.name?.isTouched && <p>Please enter a name.</p>}
        </div>

        <div
          className={`form-control ${!formState?.inputs?.email?.isValid && formState?.inputs?.email?.isTouched &&
            'form-control--invalid'}`}
        >

          <input
            id="email"
            type="email"
            onChange={(e) => { changeHandler(e, [VALIDATOR_EMAIL()]) }}
            onBlur={() => { touchHandler('email') }} />

          {!formState?.inputs?.email?.isValid && formState?.inputs?.email?.isTouched && <p>Please enter a valid email address</p>}
        </div>

        <div
          className={`form-control ${!formState?.inputs?.address?.isValid && formState?.inputs?.address?.isTouched &&
            'form-control--invalid'}`}
        >
          <textarea
            id="address"
            onChange={(e) => { changeHandler(e, [VALIDATOR_MINLENGTH(5)]) }}
            onBlur={() => { touchHandler('address') }}
            rows="4"
          />
          {!formState?.inputs?.address?.isValid && formState?.inputs?.address?.isTouched && <p>Please enter a valid address (at least 5 character).</p>}
        </div>

        <div className='center'>
          <button disabled={!formState.isValid} type="submit" className='button'>Submit</button>
        </div>
      </form >
    </div >
  )
}
export default App

Edit Page


import React, { useEffect, useState } from 'react'
import { useForm } from 'use-react-form-hook';
import { VALIDATOR_REQUIRE, VALIDATOR_MINLENGTH, VALIDATOR_EMAIL, validate } from './utils/validators';


const EditPage = () => {
    const [formState, inputHandler, setFormData] = useForm(
        {
            name: {
                value: '',
                isValid: false
            },
            address: {
                value: '',
                isValid: false
            }
        },
        false
    );

    useEffect(() => {
        setFormData(
            {
                name: {
                    value: 'Jayesh Sojitra',
                    isValid: true
                },
                address: {
                    value: 'Ahmedabad, India',
                    isValid: true
                }
            },
            true
        );

    }, [])

    return (
        <div className='container'>
            <div className='main'>
                <div className='row'>
                    <div className='col-lg-8 mb-20'>
                        <div className='card pitch'>
                            <header className='auth-content'>
                                <h1>Edit Page</h1>
                            </header>
                            <form>

                                <Input
                                    id="name"
                                    type="text"
                                    label="Name"
                                    errorText="Please enter a name"
                                    validators={[VALIDATOR_REQUIRE()]}
                                    onInput={inputHandler}
                                    initialValue={formState.inputs.name.value}
                                    initialValid={true}
                                />
                                <Input
                                    id="address"
                                    type="textarea"
                                    label="Address"
                                    rows="5"
                                    errorText="Please enter a address, at least 10 characters"
                                    validators={[VALIDATOR_MINLENGTH(10)]}
                                    onInput={inputHandler}
                                    initialValue={formState?.inputs?.address?.value}
                                    initialValid={true}
                                />

                                <div className="input-container center">
                                    <Button

                                        type="button"
                                        disabled={!formState.isValid}>
                                        Submit
                                    </Button>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div className='col-lg-4 '>
                        <UserWidget user={auth?.user} tags={tags} />
                    </div>
                </div>
            </div>
        </div>
    );
}

export default EditPage;

License

MIT © jayeshsojitra103


This hook is created using create-react-hook.