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

formflows

v1.0.4

Published

It is a custom hook, named useForm, that can help developers handle form inputs, validation, and form submission more easily.

Downloads

2

Readme

FormFlows: Streamlined React Form Management

The useForm hook provides an abstraction for managing form states, validation, and submissions. It simplifies the process of handling form inputs and reduces boilerplate for developers.

🚀 Getting Started

Prerequisites

  • Ensure you have Node.js and npm installed. If not, download them from Node.js Official Website.

Installation

Before using the hook, ensure that you've installed the package. You can install the package via npm:

npm install @yourusername/formflow --save

To use the useForm hook, simply import it from its module.

import useForm from '@yourusername/formflow';

API

useForm(initialValues, validators, onSubmit)

const {
  values,
  errors,
  isSubmitting,
  handleChange,
  handleSubmit
} = useForm(initialValues, validators, onSubmit);

Parameters

  • initialValues(Object): Initial values of the form fields. This should match the structure of your form's fields. For example: { username: '', password: '' }.

  • validators (Object): Validators for each form field. Each validator is a function that takes in the value of the field and returns an error message string if the validation fails.

  • onSubmit (Function): Callback function to execute when the form is submitted and passes validation. This function should handle the actual submission logic, such as making API calls.

Returns

  • values (Object): Current values of the form fields.

  • errors (Object): Errors for each form field, if any exist.

  • isSubmitting (Boolean): State indicating whether the form is currently being submitted.

  • handleChange (Function): Change handler for input fields. Attach this to each form input's onChange prop.

  • handleSubmit (Function): Submit handler for the form. Attach this to the form's onSubmit prop.

📖 Usage

  1. Define your initial form values and validation rules.
  2. Use the useForm hook in your form component.
  3. Bind handleChange to the onChange event of each input field.
  4. Bind handleSubmit to the onSubmit event of the form.

Example

import React from 'react';
import useForm from './path_to_useForm';

const validateRules = {
  username: (value) => (!value ? "Username is required!" : null),
  password: (value) => {
    if (!value) return "Password is required!";
    if (value.length < 8) return "Password should be at least 8 characters!";
    return null;
  }
};

const LoginForm = () => {
  const {
    values,
    errors,
    isSubmitting,
    handleChange,
    handleSubmit
  } = useForm({ username: '', password: '' }, validateRules, async (values) => {
    // Logic to submit data, e.g., API call
    console.log("Submitted", values);
  });

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <input name="username" value={values.username} onChange={handleChange} placeholder="Username" />
        {errors.username && <span>{errors.username}</span>}
      </div>
      <div>
        <input type="password" name="password" value={values.password} onChange={handleChange} placeholder="Password" />
        {errors.password && <span>{errors.password}</span>}
      </div>
      <button type="submit" disabled={isSubmitting}>Login</button>
    </form>
  );
};

export default LoginForm;

🔧 Features

  • Boilerplate Reduction: Streamline your form management process.
  • Integrated Validation: Ensure data integrity at the input level.
  • State Management: Effortlessly manage form data, errors, and submission states.
  • Flexibility: Customize validation rules and submission logic.
  • Enhanced UX: Offer real-time feedback through integrated validation.
  • Library Compatibility: Adaptable to various UI libraries or design systems.