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

input-guard

v1.3.0

Published

A React library to simplify form field validation and state management.

Downloads

105

Readme

Input-Guard

Input-Guard is a powerful and easy-to-use React library designed to simplify the management of input fields in forms. It reduces the amount of code required for handling onChange validations for fields such as name, email, password, phoneNo etc. With this library, you just need to import the package and pass your field names to the useForm hook, and it will take care of the rest.

Features

  • Easy Integration: Quickly integrate into your React forms.
  • Automatic Validation: Handles field validations with minimal configuration.
  • Customizable: Easily customize the validation messages and styles.
  • Lightweight: No additional dependencies required.

Installation

To install Input-Guard, use npm or yarn:

npm install input-guard

or

yarn add input-guard

Quickstart

Here is a basic example of how to use Input-Guard in your project:

Importing the Package

import React from "react";
import { useForm, INPUT_TYPES, VALIDATION_MESSAGES } from "input-guard";

Example Form Component

function Form() {
  // [INPUT_TYPES.EMAIL, INPUT_TYPES.PHONE_NO] should be in line with your form input fields.
  // The useForm hook returns `formState` first followed by your inputstates, in the specified order.
  // The useForm hook predicts that the email field is at the top and then mobile.
  // You need to set className="form" to your form.
  // You need to set className="form-inp" to your input tages.

  const [formState, emailState, mobileState] = useForm([
    INPUT_TYPES.EMAIL,
    INPUT_TYPES.PHONE_NO,
  ]);

  const submitFormHandler = (eve) => {
    eve.preventDefault();
    // You will get a response as an array of data in the same order as passed in the `useForm` hook.
    const response = formState.onSubmit();
    if (response.success) {
      // Add your logic here for storing data or any other operations.
    }
  };

  return (
    <form onSubmit={submitFormHandler} className="form">
      <fieldset>
        <input
          type="text"
          placeholder="email"
          onChange={emailState.onChange}
          onBlur={emailState.onBlur}
          value={emailState.value}
          className="form-inp"
        />
        {emailState.isValid === false && (
          <span>{VALIDATION_MESSAGES.EMAIL}</span>
        )}
      </fieldset>

      <fieldset>
        <input
          type="text"
          placeholder="mobile"
          onChange={mobileState.onChange}
          onBlur={mobileState.onBlur}
          value={mobileState.value}
          className="form-inp"
        />
        {mobileState.isValid === false && (
          <span>{VALIDATION_MESSAGES.PHONE_NO}</span>
        )}
      </fieldset>

      <button>Submit</button>
    </form>
  );
}

export default Form;

API

useForm(fields)

This hook manages the state and validations for the specified fields.

  • fields: An array of input types that should be validated. Available types are INPUT_TYPES.EMAIL, INPUT_TYPES.PHONE_NO, etc.

Returns an array of states corresponding to the input fields and additional form state.

Input State

Each input state object contains:

  • value: The current value of the input field.
  • isValid: A boolean indicating if the current value is valid.
  • onChange: Function to handle change events.
  • onBlur: Function to handle blur events.

Form State

  • onSubmit(): Function to handle form submission, returns an object containing success and data.

Validation Messages

You can use the provided validation messages or customize them by using the VALIDATION_MESSAGES object.

VALIDATION_MESSAGES.EMAIL = "Please enter a valid email address.";
VALIDATION_MESSAGES.PHONE_NO = "Please enter a valid mobile number.";

With Input-Guard, you can effortlessly manage form validations and focus on building great user experiences.