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

@nexapp/nexapp-react-forms

v0.1.8

Published

Form utilities in react

Downloads

1

Readme

nexapp-react-forms

Form utilities in React

Hooks

useFormData

Exposes a hook taking an initialData object. You can use it to receive the formData of the object as well as an onChange method. The onChange method will adapt it's parameters according to the type of the initialData.

For example you have an initialData like this:

{
    "details": {
        "name": "value"
    },
    "inventory": {
        "quantity": 15
    }
}

When giving an object like this to the useFormData hook, the onChange method will be callable like this: onChange("details")("name")("newValue"). Which will change the formData from the hook with the updated value in the details.name field. For the moment this method can only go one level 2 level deep so any complex objects will have to be update entirely when calling this method.

We use this hook to simplify our forms. We can then structure our form to represent each section of the data structure. Then each section will receive the underlying section. We can then give them formData.details as value and onChange("details") as onChange method. Giving us maximum flexibility to add/remove/change fields of the data structure without impacting the props we give to each components.

Form Validation

This library exposes a FormValidator to validate data received from the useFormData hook. It is possible to configure a FormValidator by extending the class, and passing the interface of your form data structure as template. You can then configure a set of rules for each property of the object. For exemple:

interface CreateAccountFormData {
  name: string;
  email: string;
  password: string;
  confirmedPassword: string;
}

class CreateAccountFormValidator
  extends FormValidator<CreateAccountFormData> {

  constructor(formData: CreateAccountFormData) {
    super(formData);
    this.rules = ({
      name: [isEmpty],
      email: [isEmpty, isEmail],
      password: [isEmpty, isPassword],
      confirmedPassword: [isEmpty, isSame(formData.password)],
    });
  }
}

A set of rules is given with the library but you can define your own rules by respecting the same signature. All you have to do after is to give your form data to your validator and call the validate method. Which will update the validator's error property.

const submit = (): void => {
    const validator = new CreateAccountFormValidator(account);
    validator.validate();
    if (validator.hasError()) {
        setErrors(validator.errors);
    }
};