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

react-rad-form

v0.2.2

Published

use-rad-form is a rad React hook that simplifies form handling with built-in validation and state management. Perfect for your next awesome project!

Downloads

49

Readme

react-rad-form

use-rad-form is a rad React hook that simplifies form handling with built-in validation and state management. Perfect for your next awesome project!

Installation

Get the latest version of use-rad-form from npm:

npm install react-rad-form

Usage

Here's a gnarly example of how to use use-rad-form to manage a form with ease:

Example Usage

import React from 'react';
import { useRadForm } from 'use-rad-form';

const RadForm = () => {
  type FormValues = {
    name: string;
    email: string;
    password: string;
  };

  const { handleChange, handleSubmit, resetForm, values, isLoading, errors } =
    useRadForm<FormValues>({
      initialValues: {
        name: '',
        email: '',x
        password: '',
      },
      validate: {
        name: (item: FormValues) => {
          if (!item.name) return 'Name is required';
        },
        email: (item: FormValues) => {
          if (!item.email) return 'Email is required';
        },
        password: (item: FormValues) => {
          if (!item.password) return 'Password is required';
        },
      },
    });

  const fakeRequest = (values: FormValues): Promise<void> => {
    return new Promise((resolve) => {
      setTimeout(() => {
        console.log('Submitted', values);
        resolve();
      }, 2000);
    });
  };

  return (
    <form className="bg-white p-4 m-4 rounded-lg">
      <input
        type="text"
        placeholder="Name"
        value={current.name}
        onChange={(e) => handleChange('name', e.target.value)}
      />
      <p>{formErrors.name}</p>
      <input
        type="email"
        placeholder="Email"
        value={current.email}
        onChange={(e) => handleChange('email', e.target.value)}
      />
      <p>{formErrors.email}</p>
      <input
        type="password"
        placeholder="Password"
        value={current.password}
        onChange={(e) => handleChange('password', e.target.value)}
      />
      <p>{formErrors.password}</p>
      <button
        onClick={(e) => {
          e.preventDefault();
          submit(async (values) => {
            await fakeRequest(values);
            resetForm();
          });
        }}
      >
        {loading ? 'Loading...' : 'Submit'}
      </button>
      {loading && <p>Loading...</p>}

      <pre>{JSON.stringify(values, null, 2)}</pre>
    </form>
  );
};

export default RadForm;

Hook API

useRadForm takes care of your form state and validation in a rad way.

useRafForm

const { handleChange, handleSubmit, resetForm, values, isLoading, errors } =
  useRadForm<FormValues>({
    initialValues: {
      // Initial form values
    },
    validate: {
      // Validation rules
    },
  });

Parameters

  • original: Initial form values
  • errors: Validation rules

Return Values

  • handleChange: Update form values
  • handleSubmit: Submit form values
  • resetForm: Reset form values
  • values: Current form values
  • isLoading: Loading state
  • errors: Form validation errors

Example Validation

Define validation rules for your form fields inside the errors object:

const { onChange, submit, reset, current, loading, formErrors } =
  useRadForm<FormValues>({
    initialValues: {
      name: '',
      email: '',
      password: '',
    },
    validate: {
      name: (item: FormValues) => {
        if (!item.name) return 'Name is required';
      },
      email: (item: FormValues) => {
        if (!item.email) return 'Email is required';
      },
      password: (item: FormValues) => {
        if (!item.password) return 'Password is required';
      },
    },
  });

Rad Features

  • Easy State Management: Manage your form state without the hassle
  • Very lightweight: Installed it's just 100kb in node_modules
  • Built-in Validation: Define validation rules for your form fields
  • Loading State: Know when your form is submitting
  • Reset Form: Reset your form to its initial state

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.