2.0.0 • Published 5 years ago

@savedo/re-form v2.0.0

Weekly downloads
16
License
MIT
Repository
github
Last release
5 years ago

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

PropertyTypeDescription
fieldsstring[]Unique field names
fieldOptions{ key (string): options (FieldOptionsValueType) }Field/Component options (See table below)
valuesobjectInit values for the fields
handleSubmitfunctionCallback function to handle submit behaviour if validation successful
validatemethodCallback function for validation (form data will be passed as an argument), supports sync/async functions.
submitSectionReact.FCReact component to provide submit button or submit event

FieldOptionsValueType

PropertyTypeDefaultOptionalDescription
namestringfield key nametruename of the field
labelstringfield key nametruelabel for the form field
elementinput, select, textareainputtype=texttrueHTML tag for the form field
typeinput types (eg. text, number, email, checkbox etc)texttruetype attribute for HTMLInputElement
componentFunctionalComponentN/AtruePass your FunctionalComponent with props (FormFieldPropsType). element and type becomes redundant when component is used.
keyValues{ key: string: any }N/AtrueOnly viable when element is select. This object provides the list of <option value="key">value</option>
classNamestringN/AtrueCSS class(es) for the element
defaultValuestringN/AtrueDefault value for a field.
disabledbooleanN/AtrueDisabled prop for inputs.
placeholderstringN/AtruePlaceholder text for the input.
checkedbooleanN/AtrueUsed for checkboxes to pass default value when type is checkbox.
checkboxTextstringN/AtrueUsed for pass label for chebox element, normal label is being used for input lable.

FormFieldPropsType

Props below will be passed to your custom component.

PropertyTypeDescription
optionsFieldOptionsValueTypeOptions defined for field (See table above)
errorstringError message to pass into component if the input is not valid
setValueFunctionN/A. For internal usage
valueanyN/A. For internal usage
checkedbooleanPassed when type is checkbox in place of Value.
namestringN/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

2.0.0

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago