1.0.17 • Published 1 year ago

react-formez v1.0.17

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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

ParameterTypeMandatoryDescription
formSpecsFormField[] | (props:FormSpecProps) => FormField[]RequiredForm 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) => voidRequiredForm Submit Handler.
classNamestringOptionalCustom CSS class name.
styleobjectOptionalInline CSS Properties.

FormField

ParameterTypeMandatoryDescription
type"text" \| "email" \| "date" \| "time" \| "datetime-local" \| "password" \| "color" \| "file" \| "range" \| "radio" \| "checkbox" \| "dropdown" \| "button" \| "submit"\| "row" \| "column"RequiredForm field type.
idstringRequiredForm field unique id.
labelstringRequiredForm field label.
optionsstring[]ConditionalForm field options. Mandatory for fields: checkbox, radio, dropdown.
onClickMouseEventHandler<HTMLButtonElement>ConditionalForm button onClick. Mandatory for field: button.
childrenJSX ElementConditionalForm button children. Mandatory for fields: button, submit.
validatorFormInputValidatorType[]OptionalForm field custom validation.
valuestringOptionalForm field default value.
requiredbooleanOptionalRequired field.
disabledbooleanOptionalDisabled field.
classNamestringOptionalCustom CSS class name.
styleobjectOptionalInline CSS Properties.
onChange(event: ChangeEvent<HTMLInputElement>) => voidOptionalForm field onChange handler.

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

FormSpecProps

ParameterTypeDescription
dataobjectForm data.
updateData(data: FormDataType): voidIn-built function to update the form state.
resetData(): voidIn-built function to reset the form state.

FormInputValidatorType

ParameterTypeDescription
messagestringValidation Message.
validate(value: string) => booleanFunction 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.

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago