1.2.5 • Published 10 months ago

react-forms-ts v1.2.5

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

React Form Component Library

Overview

The React Form Component Library provides a set of reusable and customizable form components designed to simplify form handling in React applications. The library includes several input components, which are flexible and easy to integrate into any project.

import {DropDown} from "react-forms-ts";
import { FormProvider } from "react-forms-ts";
import {InputBox} from "react-forms-ts";
import {RadioButton} from "react-forms-ts";
import "react-forms-ts/dist/styles.css"

const App = () => {
  const emailValidity = (data:string)=>{
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return emailRegex.test(data);
  }
  const feedbackValidity = (data:string)=> data.length <= 10;
  const passowordValidity = (data:String) => data.length > 4;
  const dateValidity = (data:string)=>{
    const regex = /^(\d{4})-(\d{2})-(\d{2})$/;
    console.log(data.match(regex))
    return data.match(regex) ? true : false;
  }
  return (
      <FormProvider submitFunc={(data:string)=>{alert(data)}}>
        <InputBox fieldKey='email' required={true} placeholder='Enter email' value={""} validateFunc={emailValidity} defaultErrorText='enter valid email' type='email' minLength={5}/>
        <InputBox fieldKey='password' required={true} placeholder='enter passoword' value={""} validateFunc={passowordValidity} defaultErrorText='password must have atleast 5 characters' type='password'/>
        <InputBox fieldKey='dob' required={true} placeholder='enter date' value={""} validateFunc={dateValidity} defaultErrorText='enter valid date' type='date'/>
        <DropDown 
          fieldKey='country' 
          required={true} 
          options={["India", "USA", "Canada"]} 
          value={""} 
          width="200px" 
          height="40px" 
          color="black" 
          bgColor="transparent" 
          font="Arial" 
        />
        <RadioButton fieldKey='gender' required={true} options={["Male","Female"]} value={""}/>
        <InputBox fieldKey='feedback' required={true} placeholder='Enter work expeirence' value={""} validateFunc={feedbackValidity} defaultErrorText='Write within 10 letters' type='textarea' cols={26}/>
        <InputBox fieldKey='rating' required={true} placeholder='Welcome to newsletter' value={"Subscibe to Newsletter"} validateFunc={feedbackValidity} defaultErrorText='' type='checkbox'/>
        <InputBox fieldKey='submit' required={true} placeholder='something' value={"Click me"} validateFunc={()=>{return true;}} defaultErrorText='enter valid dob' type='submit' bgColor="#A020F0" height="3rem" color="white"/>
      </FormProvider>
  );
};

export default App;

Sample deployment link - https://react-forms-ts-deployment.onrender.com/

Features

  • Supports multiple input types including text, email, password, file uploads, date, telephone, and buttons.
  • Customizable styles through props and class names.
  • Integration of custom validation logic.
  • Easy to use with hooks for managing form state.
  • Micro-animations, animated error UI
  • Integrated Debouncer with custom debounce delay

Table of Contents

Installation

You can install the package via npm or yarn:

npm install react-forms-ts

or

yarn add react-forms-ts

Usage

Import styling

To use the react-forms-ts package, make sure to import the styles in your project. Add the following line in your main application file or wherever you use the components:

import 'react-forms-ts/dist/styles.css';

FormProvider

The FormProvider component wraps your form and manages the form state and validation logic across all child components. It ensures that validation functions and form states are accessible and consistent throughout the form.

<FormProvider>
  <InputBox/>
</FormProvider>

InputBox

The InputBox component is a versatile input field that supports various types:

  • text
  • email
  • password
  • textarea
  • date
  • file
  • tel
  • button

Example

import {InputBox} from 'react-forms-ts';
import { FormProvider } from 'react-forms-ts';

const App = () => {
  const emailValidity = (data:string)=>{
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return emailRegex.test(data);
  }
  return (
      <FormProvider>
        <InputBox fieldKey='email' required={true} placeholder='Enter email' value={""} validateFunc={emailValidity} defaultErrorText='enter valid email' type='email'/>
      </FormProvider>
  );
};

Email Output

RadioButton

The RadioButton component allows users to select one option from a list of choices.

Example

import {InputBox} from 'react-forms-ts';
import {RadioButton} from 'react-forms-ts';
import { FormProvider } from 'react-forms-ts';

const App = () => {
  return (
      <FormProvider>
        <RadioButton fieldKey='country' required={true} options={["India","USA","Canada"]} value={""}/>
      </FormProvider>
  );
};


export default App

Radio Output

DropDown

The DropDown component allows users to select an option from a dropdown list.

Example

import {DropDown} from 'react-forms-ts';
import { FormProvider } from 'react-forms-ts';

const App = () => {
  return (
      <FormProvider>
        <DropDown 
          fieldKey='country' 
          required={true} 
          options={["India", "USA", "Canada"]} 
          value={""} 
          width="200px" 
          height="40px" 
          color="black" 
          bgColor="transparent" 
          font="Arial" 
        />
      </FormProvider>
  );
};

export default App;

Email Output

Button

The Button component allows users to handle or induce on click events in the form

Example

import { FormProvider, InputBox} from "react-forms-ts";
import "react-forms-ts/dist/styles.css";

const App = () => {
  return (
      <FormProvider>
        <InputBox fieldKey='btn' required={true} placeholder='' value={"Click me"} validateFunc={()=>{}} defaultErrorText='' type='button' bgColor="#A020F0" height="3rem" color="white"/>
      </FormProvider>
  );
};

export default App;

Button Output

Props

InputBox Props

PropTypeDescription
typestringType of input (e.g., text, email, password, textarea, file, tel, button, date).
requiredbooleanIndicates whether the input is required.
placeholderstringPlaceholder text for the input (applicable for text types).
widthstringWidth of the input field.
heightstringHeight of the input field.
colorstringText color for the input.
bgColorstringBackground color for the input.
fontstringFont family for the input.
defaultErrorTextstringDefault error message to display when validation fails.
classNamestringAdditional CSS classes for customization.
valuestringInitial value of the input (applicable for text types).
onClickfunctionFunction to call on button click (applicable for button type).

RadioButton Props and DropDown Props

PropTypeDescription
optionsstring[]Array of options for the radio buttons.
fieldKeystringUnique key for the radio button group.
requiredbooleanIndicates whether a selection is required.
widthstringWidth of the radio button container.
heightstringHeight of the radio button container.
colorstringText color for the radio button labels.
bgColorstringBackground color for the radio button container.
fontstringFont family for the radio button labels.
classNamestringAdditional CSS classes for customization.

Form Props

PropTypeDescription
debounceDelaynumberAdjusts the custom debounce delay
submitFuncfunctionFired on submission of the form

Customization

The react-forms-ts package is designed to be smart enough to accept native arguments that work on standard HTML input elements. This means you can pass additional props to customize your input fields according to your needs.

For example, you can add class names, htmls arguments, set placeholder text, define input types, and more to achieve greater flexibility in your UI. Here’s a quick example:

import {InputBox} from 'react-forms-ts';
import { FormProvider } from 'react-forms-ts';

const App = () => {
  const emailValidity = (data:string)=>{
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return emailRegex.test(data);
  }
  return (
      <FormProvider>
        <InputBox fieldKey='email' required={true} placeholder='Enter email' value={""} validateFunc={emailValidity} defaultErrorText='enter valid email' type='email' aria-label={"This is input field"}/>
      </FormProvider>
  );
};

Versions

  • 1.2.4:

    • New feature Introducing micro-animations.
  • 1.2.2:

    • Update Added a sample deployment link.
  • 1.2.0:

    • New Feature Added major customizations for checkbox.
    • Improvement Default label support for checkbox.
  • 1.1.3:

    • Improvement Removed unwanted validation controller from updation pipeline.
  • 1.1.2:

    • Improvement Fixed dropdown error validation check.
  • 1.1.1:

    • Improvement Fixed form submission control.
  • 1.1.0:

    • Initial Release Version 1.1 with major UI and form validation improvements.
1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.3

10 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago