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

formx-js

v3.0.0

Published

Develop and manage forms easily.

Downloads

13

Readme

FormX

FormX is a lightweight and flexible form handling library for React applications. It simplifies form state management, validation, and asynchronous validation with minimal boilerplate.

Features

  • Easy form state management with useForm hook
  • Built-in synchronous and asynchronous field validation
  • Customizable input components
  • FormProvider for managing form submission and validation logic
  • Debounced input changes to minimize unnecessary renders
  • Custom styling support

Installation

Install the package via npm:

npm install formx-js

Basic Usage

import * from "formx-test";
const BasicForm = () => {
  const handleSubmit = (formValues) => {
    console.log("Form Submitted with values:", formValues);
  const NameValidators = [
    {
      validator: (value: string) => value !== "",
      message: "Name cannot be empty",
    },
    {
      validator: (value: string) => value.length >= 6,
      message: "Minimum should be of 6 characters",
    },
  ];

  const EmailValidators = [
    {
      validator: async (value: string) => {
        let response = await axios.get(
          "https://jsonplaceholder.typicode.com/posts"
        );
        return response.status === 200;
      },
      message: "Email already in use",
    },
  ];

  return (
    <>
      <FormProvider
        onSubmit={handleSubmit}
      >
          <TextInput
            id="first_name"
            validators={NameValidators}
            placeholder="Enter your first name"
          />
          <NumberInput
            id="age"
            minNumber={18}
            maxNumber={65}
            placeholder="Select Your Age"
            required
          />
          <EmailInput
            id="email_id"
            placeholder="Enter your email"
            validators={EmailValidators}
            debounce={1000}
          />
          <PasswordInput
            id="password"
            placeholder="Enter your password"
          />
          <TextArea
            id="feedback"
            placeholder="Let us know "
            rows={4}
            required
          />
          <SelectInput
            id="location"
            options={[
              { label: "Karnataka", value: "karnataka" },
              { label: "Goa", value: "goa" },
            ]}
            placeholder="Select a city"
          />
          <SubmitButton text="Submit" />
      </FormProvider>
    </>
  );
}

export default App;


export default BasicForm;

Asynchronus Validation

The validator function can also be asynchronous if you need to perform server-side checks or other async validations.

<TextInput
  label="Username"
  id="username"
  validators={[
    {
      validator: async (value) => {
        const res = await fetch(`/api/check-username?username=${value}`);
        const data = await res.json();
        return data.isAvailable;
      },
      message: "Username is already taken",
    },
  ]}
/>

Custom Styling

You can apply custom styles to your input components by using standard React props. Use CSS classes or inline styles to achieve the desired look.

<TextInput
  label="Custom Styled Input"
  id="custom-input"
  className="custom-input-class" // Example of applying a custom CSS class
/>

Debounce

FormX includes a built-in default debounce of 300 milliseconds for handling the onChange events of input fields. This helps reduce the number of times validation is triggered while the user is typing.

<TextInput
  label="Username"
  id="username"
  required
  debounce={500} // Custom debounce duration in milliseconds
/>

Email Input

  • id: Unique identifier for the input element, used to register the field.
  • placeholder: Text displayed when the input is empty.
  • validators: Array of custom validation logic:
    • validator: Function returning boolean or Promise<boolean>.
    • message: Error message displayed when validation fails.
  • Default Validation : The Email Input component includes built-in regex validation to ensure the email is in a valid format.
  <EmailInput
    id="email_id"
    placeholder="Enter your email"
    validators={EmailValidators}
    debounce={1000}
  />

Number Input

  • id: Unique identifier for the input element.
  • minNumber: Sets the minimum allowable value.
  • maxNumber: Sets the maximum allowable value.
  • placeholder: Placeholder text displayed when the input is empty.
<NumberInput
    id="age"
    minNumber={18}
    maxNumber={65}
    placeholder="Select Your Age"
/>

Select Input

  • id: Unique identifier for the select input element.
  • options: Array of objects representing the available options.
  • placeholder: Placeholder text displayed when no option is selected.
<SelectInput
  id="location"
  options={[
      { label: "Karnataka", value: "karnataka" },
      { label: "Goa", value: "goa" },
  ]}
  placeholder="Select a city"
/>

Text Area

  • id: Unique identifier for the textarea element.
  • placeholder: Placeholder text displayed when the textarea is empty.
  • rows: Number of visible text lines for the textarea.
  • columns: Number of visible text columns for the textarea.
  • required: Indicates whether the textarea is a required field.
  <TextArea
    id="feedback"
    placeholder="Let us know "
    rows={4}
    required
  />
/>