1.0.5 • Published 9 months ago

react-form-validify v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

FormValidify Documentation

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. Props
  5. Examples
  6. Contributing

Introduction

FormValidify is a customizable and reusable form validation library built with React and TypeScript. It provides components for creating forms with built in and user defined validations, error handling, and customizable styles. The package is designed to be easy to use and integrates smoothly into your React applications.

Installation

To install FormValidify, use npm or yarn:

npm install react-form-validify

or

yarn add react-form-validify

Usage

To use FormValidify in your project, import the components and styles you need:

import { FormValidify, InputComponent, SelectComponent, TextAreaComponent, ButtonComponent } from 'react-form-validify';

// to import styles
import "react-form-validify/dist/styles.css";

Components

FormValidify comes with the following components:

  • FormValidify
  • InputComponent
  • SelectComponent
  • TextAreaComponent
  • ButtonComponent

FormValidify

The main component that wraps around your form elements to handle validation.

<FormValidify>
  {/* Form components go here */}
</FormValidify>

InputComponent

A customizable input field component.

<InputComponent
  name="username"
  label="Username"
  type="text"
  placeholder="Enter your username"
  minLength={6}
  maxLength={15}
  style={{
    backgroundColor:"gray";
    paddingTop:"10px";
    //custom stylings.
  }}
  validate={(value) => {
    if (value.length < 6) {
      return "Username must be at least 6 characters.";
    }
    if (!/^[a-zA-Z0-9]+$/.test(value)) {
      return "Username must contain only alphanumeric characters.";
    }
    return null; // No error
  }}
/>

SelectComponent

A dropdown select component for choosing from predefined options.

<SelectComponent
  name="gender"
  label="Gender"
  options={[
    { value: 'male', label: 'Male' },
    { value: 'female', label: 'Female' },
    { value: 'other', label: 'Other' },
  ]}
  style={{
    backgroundColor:"gray";
    paddingTop:"10px";
    //custom stylings.
  }}
/>

TextAreaComponent

A customizable text area component.

<TextAreaComponent
  name="bio"
  label="Bio"
  placeholder="Tell us about yourself"
  rows={10}
  cols={20}
  style={{
    backgroundColor:"gray";
    paddingTop:"10px";
    //custom stylings.
  }}
  validate={(value) => {
    if (value.length < 6) {
      return "Username must be at least 6 characters.";
    }
    if (!/^[a-zA-Z0-9]+$/.test(value)) {
      return "Username must contain only alphanumeric characters.";
    }
    return null; // No error
  }}
/>

ButtonComponent

A customizable button component for submitting the form.

<ButtonComponent
  type="submit"
  label="Submit"
  style={{
    backgroundColor:"gray";
    paddingTop:"10px";
    //custom stylings.
  }}
/>

Props

FormValidify Props

PropTypeDescription
childrenReactNodeChild components (form elements).

InputComponent Props

PropTypeDescription
namestringThe name of the input field.
labelstringLabel text for the input field.
typestringType of the input field (e.g., text, email).
placeholderstringPlaceholder text for the input field.
minLengthnumberMinimum length for validation (optional).
maxLengthnumberMaximum length for validation (optional).
styleReact.CSSPropertiesCustom styles defined by the user (optional).
validate(value: string) => stringnull Custom validation function that returns an error message or null.

SelectComponent Props

PropTypeDescription
namestringThe name of the select field.
labelstringLabel text for the select field.
optionsArray<{ value: string; label: string }>Array of options for the select field.
styleReact.CSSPropertiesCustom styles defined by the user (optional).

TextAreaComponent Props

PropTypeDescription
namestringThe name of the text area.
labelstringLabel text for the text area.
placeholderstringPlaceholder text for the text area.
styleReact.CSSPropertiesCustom styles defined by the user (optional).
validate(value: string) => stringnull Custom validation function that returns an error message or null.

ButtonComponent Props

PropTypeDescription
typestringType of the button (e.g., submit).
labelstringLabel text for the button.
onSubmit(values: Record<string, string>) => voidFunction to handle button click (optional).
styleReact.CSSPropertiesCustom styles defined by the user (optional).

Examples

Basic Usage

import React from 'react';
import { FormValidify, InputComponent, SelectComponent, TextAreaComponent, ButtonComponent } from 'react-form-validify';
import "react-form-validify/dist/styles.css"

function App() {
  return (
    <FormValidify>
      <InputComponent
        name="username"
        label="Username"
        type="text"
        placeholder="Enter your username"
        minLength={6}
      />
      <InputComponent
        name="email"
        label="Email"
        type="email"
        validate={(value) => {
          const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
          if (!emailRegex.test(value)) {
            return "Please enter a valid email address.";
          }
          return null;
        }}
      />

      <SelectComponent
        name="gender"
        label="Gender"
        options={[
          { value: 'male', label: 'Male' },
          { value: 'female', label: 'Female' },
          { value: 'other', label: 'Other' },
        ]}
      />
      <TextAreaComponent
        name="bio"
        label="Bio"
        placeholder="Tell us about yourself"
      />
      <ButtonComponent type="submit" label="Submit" onSubmit={(values) => console.log(values)} />
    </FormValidify>
  );
}

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your changes.

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago