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-group

v1.0.7

Published

reactive form group with validation

Downloads

17

Readme

Step 1. Install use-form-group package by running below command npm i use-form-group

Step 2. import the use-form-group hook in react component file.

    import { useFormGroup } from "use-form-group";

    const UserDetails = ()=>{

        const [mobileRequired, setMobileRequired] = useState(false);

        const [userForm, updateForm, updateValidator, clearForm] = useFormGroup({
            userName: {
                value: "",
                validation: {
                    required: true,
                    msgs: {
                    required: "firstName is required",
                    },
                },
            },
            mobileNumber: {
                value: "",
                validation: {
                    required: true,
                    pattern: /[0-9]{10}/,
                    msgs: {
                    required: "mobile number is required",
                    pattern: "Invalid mobile number",
                    },
                },
            },
        });

        const errorStyle = {
            fontSize: "10px",
            color: "red",
            fontStyle: "italic",
        };

        const sucessStyle = {
            fontSize: "10px",
            color: "green",
            fontStyle: "italic",
        };

        const isFormValid = Validator.isFormValid(userForm);

        useEffect(() => {
            if (!mobileRequired) {
                updateValidator([
                    {
                    id: "mobileNumber",
                    validation: {
                        required: false,
                        pattern: /[0-9]{10}/,
                        msgs: { pattern: "invalid mobile number" },
                    },
                    },
                ]);
            } else {
                updateValidator([
                    {
                    id: "mobileNumber",
                    validation: {
                        required: true,
                        pattern: /[0-9]{10}/,
                        msgs: {
                        required: "mobile number is required",
                        pattern: "Invalid mobile number",
                        },
                    },
                    },
                ]);
            }
            return () => {};
        }, [mobileRequired]);

        const toggleRequired = () => {
            setMobileRequired(!mobileRequired);
        };

            return (
                <div>
                <div>
                    <input
                        value={userForm.userName.value}
                        id="userName"
                        placeholder="Username"
                        onChange={updateForm}
                    />
                    {userForm.userName.error ? (
                    <span style={errorStyle}>{userForm.userName.errorMessage}</span>
                    ) : (
                    <span style={sucessStyle}>valid username</span>
                    )}
                </div>
                <div>
                    <input
                        value={userForm.mobileNumber.value}
                        id="mobileNumber"
                        placeholder="Mobile Number"
                        onChange={updateForm}
                    />
                    <input
                        type="checkbox"
                        id="mobile-required"
                        defaultValue="false"
                        value={mobileRequired}
                        onChange={() => toggleRequired()}
                    />
                    Required
                    {userForm.mobileNumber.error ? (
                    <span style={errorStyle}>{userForm.mobileNumber.errorMessage}</span>
                    ) : (
                    <span style={sucessStyle}>valid mobile number</span>
                    )}
                </div>
                <div>
                    <button disabled={!isFormValid}>Submit</button>
                    <button onClick={clearForm}>Clear Form</button>
                </div>
                </div>

            );

    }
        1.formgroup key name should be matched with input controller id attribute

        2.For select, control name should be matched with input name attribute

        3.You can check form error state using Validator
            Validator.isFormValid(form);

        4.for each control it's state {error,errorMesssage} is updated run time on user interaction

        5.Valid properties for validation are
            a. required - [true,false]<span style="color='blue'">Check if value is required or not</span>
            b. pattern - [regex]<span style='color="blue"'>valdiate user input against give regex</span>
            c  minLength - [number]<span>Check for min length of the string</span>
            d  maxLength - [number]<span>Check for max length of the string</span>

        6.For each validation , error message can be provided with same name as validation type in msgs.

            validation:{
                required:true,
                msgs:{
                    required:"this value is required"
                }
            }