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

v1.0.17

Published

Create forms with validation using our easy form builder.

Downloads

25

Readme

React FormEZ

Create forms with validation using our easy form builder.

Install

npm install react-formez
yarn add react-formez

Quick start

import { Form, FormField, FormDataType } from "react-formez";

const App = () => {
    const formFields: FormField[] = [
        {
            id: "email",
            type: "text",
            label: "Enter email",
            required: true,
        },
        {
            id: "password",
            type: "password",
            label: "Enter your password",
            required: true,
        },
        {
            id: "submit",
            type: "submit",
            children: "Login",
        },
    ];
    const handleSubmit = (isFormValid: boolean, data: FormDataType) => {
        console.log(isFormValid, data);
    };
    return (
        <Form
            formSpecs={formFields}
            onSubmit={handleSubmit}
        />
    );
}
export default App;

Documentation

Form

| Parameter | Type | Mandatory | Description | | ----------- | ----------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | formSpecs | FormField[] | (props:FormSpecProps) => FormField[] | Required | Form schema to populate UI and corresponding states.(props: FormSpecProps) => FormField[] is used to utilize inbuilt form functions and validate using associated fields. | | onSubmit | (isFormValid: boolean, data: FormDataType) => void | Required | Form Submit Handler. | | className | string | Optional | Custom CSS class name. | | style | object | Optional | Inline CSS Properties. |

FormField

| Parameter | Type | Mandatory | Description | | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- | -------------------------------------------------------------------------- | | type | "text" \| "email" \| "date" \| "time" \| "datetime-local" \| "password" \| "color" \| "file" \| "range" \| "radio" \| "checkbox" \| "dropdown" \| "button" \| "submit"\| "row" \| "column" | Required | Form field type. | | id | string | Required | Form field unique id. | | label | string | Required | Form field label. | | options | string[] | Conditional | Form field options. Mandatory for fields: checkbox, radio, dropdown. | | onClick | MouseEventHandler<HTMLButtonElement> | Conditional | Form button onClick. Mandatory for field: button. | | children | JSX Element | Conditional | Form button children. Mandatory for fields: button, submit. | | validator | FormInputValidatorType[] | Optional | Form field custom validation. | | value | string | Optional | Form field default value. | | required | boolean | Optional | Required field. | | disabled | boolean | Optional | Disabled field. | | className | string | Optional | Custom CSS class name. | | style | object | Optional | Inline CSS Properties. | | onChange | (event: ChangeEvent<HTMLInputElement>) => void | Optional | Form field onChange handler. |

You can also include all the HTML Input attributes along with these.

FormSpecProps

| Parameter | Type | Description | | ------------ | ---------------------------- | ------------------------------------------- | | data | object | Form data. | | updateData | (data: FormDataType): void | In-built function to update the form state. | | resetData | (): void | In-built function to reset the form state. |

FormInputValidatorType

| Parameter | Type | Description | | ---------- | ---------------------------- | --------------------------------------------- | | message | string | Validation Message. | | validate | (value: string) => boolean | Function which validates form field onChange. |

Examples

1. Form with custom validation

import { Form, FormField, FormDataType } from "react-formez";

const App = () => {
    const formFields: FormField[] = [
        {
            id: "name",
            type: "text",
            label: "Enter your Name",
            required: true,
        },
        {
            id: "contact",
            type: "row",
            fields: [
            {
                id: "email",
                type: "text",
                label: "Enter email",
                required: true,
                validator: [
                {
                    message: "Enter valid email address",
                    validate: (val) => !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val),
                },
                ],
            },
            {
                id: "phoneno",
                type: "text",
                label: "Enter phoneno",
                required: true,
                validator: [
                {
                    message: "Phone number should be 10 character length",
                    validate: (val) => val.length !== 10,
                },
                {
                    message: "Enter a valid phone number",
                    validate: (val) => !/^[6-9]\d{9}$/.test(val),
                },
                ],
            },
            ],
        },
        {
            id: "info",
            type: "row",
            fields: [
            {
                type: "radio",
                id: "gender",
                label: "Select your gender",
                options: ["Male", "Female"],
                value: "Male",
                required: true,
            },
            {
                type: "dropdown",
                id: "occupation",
                label: "Select your occupation",
                options: ["Student", "Working"],
                required: true,
            },
            ],
        },
        {
            id: "pass",
            type: "row",
            fields: [
            {
                id: "password",
                type: "password",
                label: "Choose your password",
                required: true,
                validator: [
                {
                    message: "Password should be atlease six character length",
                    validate: (val) => val.length < 6,
                },
                ],
            },
            {
                id: "confirmPassword",
                type: "password",
                label: "Confirm your password",
                required: true,
                validator: [
                {
                    message: "Password should be atlease six character length",
                    validate: (val) => val.length < 6,
                },
                ],
            },
            ],
        },
        {
            id: "terms",
            type: "checkbox",
            label: "Accept the terms and conditions",
            required: true,
        },
        {
            id: "submit",
            type: "submit",
            children: "Submit",
        },
    ];
    const handleSubmit = (isFormValid: boolean, data: FormDataType) => {
        console.log(isFormValid, data);
    };
    return (
        <Form
            formSpecs={formFields}
            onSubmit={handleSubmit}
        />
    );
}
export default App;

In this example, we have utilized the validator attribute to implement custom validation for the email, phone number, and password fields.

Note: validator attribute can accept multiple validations. As demonstrated in the phone number validation, the validations are executed from top to bottom, and the first captured validation message will be displayed in the UI.

2. Form with associated fields and other inbuild functions

import { Form, FormField, FormSpecProps, FormDataType } from "react-formez";

const App = () => {
    const renderFormFields = ({ data, updateData, resetData }: FormSpecProps) =>{

        const formFields: FormField[] = [
            {
                id: "name",
                type: "text",
                label: "Enter your Name",
                required: true,
            },
            {
                id: "pass",
                type: "row",
                fields: [
                {
                    id: "password",
                    type: "password",
                    label: "Choose your password",
                    required: true,
                    validator: [
                    {
                        message: "Password should be atlease six character length",
                        validate: (val) => val.length < 6,
                    },
                    ],
                },
                {
                    id: "confirmPassword",
                    type: "password",
                    label: "Confirm your password",
                    required: true,
                    validator: [
                    {
                        message: "Password should be atlease six character length",
                        validate: (val) => val.length < 6,
                    },
                    {
                        message: "Password should match",
                        validate: (val) => val !== data['password'],
                    }
                    ],
                },
                ],
            },
            {
                id: "cta",
                type: "row",
                fields: [
                    {
                        id: "submit",
                        type: "submit",
                        children: "Submit",
                    },
                    {
                        id: "reset",
                        type: "button",
                        children: "Reset",
                        onClick: ()=>{
                            resetData()
                        }
                    }
                ]
            }
        ];
        return formFields
    }

    const handleSubmit = (isFormValid: boolean, data: FormDataType) => {
    console.log(isFormValid, data);
    };
    return (
        <Form
            formSpecs={renderFormFields}
            onSubmit={handleSubmit}
        />
    );
}
export default App;

In this example, we have demonstrated how to use associated fields to validate a field like confirmPassword.

Additionally, we have used the built-in function resetData to reset the form inputs, and if required, we can also use updateData.