Git Product home page Git Product logo

re-form's Introduction

re-form

React form library.

re-form

Installation

re-form requires

  • react/react-dom 16.8.3 or later to build your form.

$ npm install --save @savedo/re-form

or

$ yarn add @savedo/re-form

Usage

Basic usage: Creating a react form component with three input fields (name, age, email) validation and values assignment.

Create your form component:

import React from 'react';
import { FormBuilder } from '@savedo/re-form';

const MyForm = ({ handleSubmit }) => {
  const fields = [
    'name',
    'age',
    'email'
  ];

  const fieldOptions = {
    name: {
      label: 'Name:'
    },
    age: {
      label: 'Age:',
      type: 'number'
    },
    email: {
      label: 'E-mail:'
    }
  };

  const validate = (formData) => {
    const errors = {};
    const { name, age } = formData;

    if (!name) {
      errors.name = 'Name is required!';
    }
    if (!age) {
      errors.age = 'Age is required!';
    } else if (Number(age) < 18) {
      errors.age = 'You should be 18 years old or older!';
    }

    return errors;
  };

  return (
    <div>
      <FormBuilder { ...{ fields, fieldOptions, handleSubmit, validate } } />
    </div>
  );
};

export default MyForm;

And use form component elsewhere:

import React from 'react';
import MyForm from './MyForm';

const App = () => {
  const handleSubmit = (formData) => {
    console.log(formData)
  };

  return (
    <MyForm handleSubmit={ handleSubmit } />
  );
};

export default App;

Configuration

The FormBuilder component has props as shown below. It has FormBuilderPropsType<T> type. T refers to your object type having key (Field name, ) / values (Field options ).

FormBuilderPropsType

Property Type Description
fields string[] Unique field names
fieldOptions { key (string): options (FieldOptionsValueType) } Field/Component options (See table below)
values object Init values for the fields
handleSubmit function Callback function to handle submit behaviour if validation successful
validate method Callback function for validation (form data will be passed as an argument), supports sync/async functions.
submitSection React.FC React component to provide submit button or submit event

FieldOptionsValueType

Property Type Default Optional Description
name string field key name true name of the field
label string field key name true label for the form field
element input, select, textarea input[type=text] true HTML tag for the form field
type input types (eg. text, number, email, checkbox etc) text true type attribute for HTMLInputElement
component FunctionalComponent N/A true Pass your FunctionalComponent with props (FormFieldPropsType). element and type becomes redundant when component is used.
keyValues { [key: string]: any } N/A true Only viable when element is select. This object provides the list of <option value="key">value</option>
className string N/A true CSS class(es) for the element
defaultValue string N/A true Default value for a field.
disabled boolean N/A true Disabled prop for inputs.
placeholder string N/A true Placeholder text for the input.
checked boolean N/A true Used for checkboxes to pass default value when type is checkbox.
checkboxText string N/A true Used for pass label for chebox element, normal label is being used for input lable.

FormFieldPropsType

Props below will be passed to your custom component.

Property Type Description
options FieldOptionsValueType Options defined for field (See table above)
error string Error message to pass into component if the input is not valid
setValue Function N/A. For internal usage
value any N/A. For internal usage
checked boolean Passed when type is checkbox in place of Value.
name string N/A. For internal usage

Development

In order to start dev server, type

yarn develop

it opens your browser with url http://localhost:9100/

License

MIT

re-form's People

Contributors

erhangundogan avatar peeyushsingla avatar mrarguello avatar lisovskyvlad avatar sandeepgujela avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.