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

use-form-validation-hook

v1.0.20

Published

``` npm i use-form-validation-hook ``` ##Usage ``` import {useForm, RULES} from 'use-form-validation-hook';

Downloads

161

Readme

useForm - that is what you need to handle form

##Instalation

npm i use-form-validation-hook

##Usage

import {useForm, RULES} from 'use-form-validation-hook';

const Component = (data) => {
    const initialState = {
      merchants: [],
      name: '',
      password: '',
      repeatPassword: ''
    };

    const validation = [
      { rules: [RULES.required], name: 'name' },
      {
        rules: [{ regex: (value, formValues) => !!value.length, message: "Cannot be empty" }],
        name: 'merchants'
      },
      { rules: [RULES.required], name: 'password' },
      { name: 'repeatPassword', matchWithField: { field: 'password' } },
    ];
    
    const { values, errors, handleChange, handleSubmit, setValue } = useForm(
      initialState,
      validation
    );
    // You can use setValue if you want update fromData depending on props
    useEffect(() => {
        setValue(data);
    }, [data])

    return <form onSubmit={handleSubmit((formData) => Api.sendData(formData)}>
        <Input
            name="name"
            label="Name"
            value={values.name}
            onChange={handleChange}
            error={errors.name}
         />
         <Input
            name="password"
            label="Password"
            value={values.password}
            onChange={handleChange}
            error={errors.password}
         /> 
         <Input
            name="repeatPassword"
            label="Repeat password"
            value={values.repeatPassword}
            onChange={handleChange}
            error={errors.repeatPassword}
         />
         <MultiSelect
            name="merchants"
            label="Merchants"
            values={values.merchants}
            onChange={handleChange}
            error={errors.merchants}
         />
         <Button
            type="submit"
            label="Submit"
         />
    </form>
}

##Docs ###useForm takes three parameters:

  • initialValues - initial form values (required)
  • validation - array of object: (optional)
    [{
      rules: Array<Rule>;
      name: string;
      matchWithField?: {
        field: string;
        message: string;
      };
    }];
      
    // Rule -  { regex: /([^\s])/, message: 'Is required'}
    // regex - also can be a function to handle specific values 
    // matchWithField - object with field name and message (optional).
  • validateOnChange - boolean. (optional)
    If true validate function will be called on every change. By default it is equal to false - validate function will be called only on submit

###useForm return an object with:

  • values - object that contains current form values.
  • errors - object that contains current form errors.
  • touched - object that represent changed fields.
  • handleChange - will update values object. Should be passed to input onChange function. If validateOnChange equal to true. handleChange also will trigger validate function and update errors
  • handleSubmit - takes callback and should be passed to <form> onSubmit function. callback will be called if validation has succeed, or there is no validation.
  • reset - will drop values and errors to initial state
  • setValue - use this function on special cases, when you need directly pass some value to useForm values object.
  • triggerValidation - use this function when you want to trigger validation manually
  • isValid: function - return boolean