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-input-easy

v1.0.3

Published

A input validation and change handler custom hook

Downloads

3

Readme

Use Input Easy

A custom hook for React to use input change and validation handler.

Instalation

npm install use-input-easy

How to use

This hook return an array with two elements.

First Element (values): An object contained "value, isValueValid, inputIsInvalid"

  • value -> Input value
  • isValueValid -> A boolean variable derived by value returned with your validationFunction
  • inputIsInvalid -> A boolean variable that indicates whether there is a validation error when it onBlur.

Second Element (setFunctions): An object contained "handleBlur, handleChange"

  • handleBlur -> Check if the input blur or not.
  • handleChange -> Input value change handler.

Example

import useInput from "use-input-easy";

//simply validation functions.
function checkConfirmPassword(pass: string, confPass: string) {
  return confPass.trim().length !== 0 && pass === confPass;
}

export default function Form() {
  //you can either pass built-in validation check methods or your own validation function

  //isEmail -->
  const [emailState, setEmailState] = useInput({ isEmail: true });

  //minLength -->
  const [passwordState, setPasswordState] = useInput({ minLength: 5 });

  //own validation function
  const [confPassState, setConfPassState] = useInput({
    validationFnc: (value) => checkConfirmPassword(value, passwordState.value),
  });

  function handleSubmit(event: FormEvent) {
    event.preventDefault();
    if (!emailState.isValueValid) setEmailState.handleBlur();
    if (!passwordState.isValueValid) setPasswordState.handleBlur();
    if (!confPassState.isValueValid) setPasswordState.handleBlur();

    if (
      !emailState.isValueValid ||
      !passwordState.isValueValid ||
      !confPassState.isValueValid
    )
      return;

    console.log("[Email]: ", emailState.value);
    console.log("[Password]: ", passwordState.value);
    console.log("[Confirm Password]: ", confPassState.value);

    setEmailState.handleChange("");
    setPasswordState.handleChange("");
    setConfPassState.handleChange("");
  }

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label htmlFor="email">Email</label>
        <p>
          <input
            id="email"
            type="email"
            onBlur={setEmailState.handleBlur}
            value={emailState.value}
            onChange={(e) => setEmailState.handleChange(e.target.value)}
          />
          {emailState.inputIsInvalid && <span>Please enter a valid email</span>}
        </p>
      </div>
      <div>
        <label htmlFor="password">Password</label>
        <p>
          <input
            id="password"
            type="password"
            onBlur={setPasswordState.handleBlur}
            value={passwordState.value}
            onChange={(e) => setPasswordState.handleChange(e.target.value)}
          />
          {passwordState.inputIsInvalid && (
            <span>Please enter a valid password min 6 chars</span>
          )}
        </p>
      </div>
      <div>
        <label htmlFor="confirmPassword">Confirm Password</label>
        <p>
          <input
            id="confirmPassword"
            type="password"
            onBlur={setConfPassState.handleBlur}
            value={confPassState.value}
            onChange={(e) => setConfPassState.handleChange(e.target.value)}
          />
          {confPassState.inputIsInvalid && (
            <span>Passwords should matched.</span>
          )}
        </p>
      </div>
      <div>
        <button>Submit</button>
      </div>
    </form>
  );
}

Check values

  • isEmail -> true or undefined
  • isNum -> true or undefined
  • minLength -> number or undefined
  • maxLength -> number or undefined
  • ownFunction -> (value: string) => boolean;