0.1.9 • Published 4 years ago

react-shared-fields v0.1.9

Weekly downloads
37
License
-
Repository
github
Last release
4 years ago

React-Shared-Fields

A package used to generate form fields like Input, Choice, DatePicker, etc. fields, built with ReactJS and CSS.

Project Status

This package is currently in development. Users can generate some form fields bypassing some required data to the component. Functionality to edit form fields and some design changes is in progress.

Installation and Setup Instructions

Installation:

npm install --save react-shared-fields

Usage

An import the package into your project.

import React from "react";
import { ReactSharedField } from "react-shared-fields";

export default class App extends React.Component {
  ...
}

Quick Start

Using React class component

import React  from "react";
import { ReactSharedField } from "react-shared-fields";

export default class App extends React.Component {
  state = {
    formConstant: [],
    formData: {},
    formError: {}
  }

  componentDidMount = () => {
    let fields = [
      {
        type: "input",
        subType: "text",
        id: "inputEmail",
        label: "Email",
        placeholder: "Email",
        key: "email",
        formClass: "form-control",
        required: true,
      },
      {
        type: "input",
        subType: "password",
        id: "inputPassword",
        label: "Password",
        placeholder: "Password",
        key: "password",
        formClass: "form-control",
        required: true,
      },
      {
        type: "number",
        subType: "text",
        id: "inputPhone",
        label: "Company Phone",
        placeholder: "Enter Phone",
        key: "company_phone",
        format: "(###) ###-####",
        prefix: "+1",
        required: true,
      },
      {
        type: "textarea",
        subType: "text",
        id: "inputAboutCompany",
        label: "About Company",
        key: "about_company",
        required: true,
      }
    ];

    this.setState({ formConstant: fields });
  }

  handleFieldChange = ({ key, value }) => {
    this.setState({
      formData: {
        ...this.state.formData,
        [key]: value
      }
    });
  }

  render() {
    return (
      <React.Fragment>
        <form>
          <ReactSharedField
            fields={this.state.formConstant}
            data={this.state.formData}
            error={this.state.formError}
            handleOnChange={this.handleFieldChange}
          />
        </form>
      </React.Fragment>
    );
  }
}

Using React hooks

import React, { useState }  from "react";
import { ReactSharedField } from "react-shared-fields";

function App(props) {
  const [formData, setFormData] = useState({});
  const [formError, setFormError] = useState({});
  const formConstant = [
    {
      type: "input",
      subType: "text",
      id: "inputEmail",
      label: "Email",
      placeholder: "Email",
      key: "email",
      formClass: "form-control",
      required: true,
    },
    {
      type: "input",
      subType: "password",
      id: "inputPassword",
      label: "Password",
      placeholder: "Password",
      key: "password",
      formClass: "form-control",
      required: true,
    },
    {
      type: "number",
      subType: "text",
      id: "inputPhone",
      label: "Company Phone",
      placeholder: "Enter Phone",
      key: "company_phone",
      format: "(###) ###-####",
      prefix: "+1",
      required: true,
    },
    {
      type: "textarea",
      subType: "text",
      id: "inputAboutCompany",
      label: "About Company",
      key: "about_company",
      required: true,
    }
  ];

  const handleFieldChange = ({ key, value }) => {
    setFormData({
      ...formData,
      [key]: value,
    });
  };

  return (
    <React.Fragment>
      <form>
        <ReactSharedField
          fields={formConstant}
          data={formData}
          error={formError}
          handleOnChange={handleFieldChange}
        />
      </form>
    </React.Fragment>
  );
}

export default App;

Params

NameTypeDefaultDescription
fieldsArray-Required. List of form fields array with type, label, placeholder, etc.
dataObject-Required. Object of form fields
errorObject-Required. Object of form fields error
handleOnChangeFunction-Form field change handler and set form field object

Fields constant

Input Field

NameTypeDescriptionExample
typeString-type="input"
subTypeStringDepends on input field typeInput, password
idStringAttribute specifies a unique id for an HTML elementid="username"
labelStringDefines a label for input elementUsername
placeholderStringSpecifies a short hint that describes the expected value of an input fieldplaceholder="enter username"
keyStringkey is a string (also called a “property name”) on formdata objectformdata={username: ""}
formClassString--
requiredBooleanSpecifies that an input field must be filled out before submitting the formrequired=true

Number Field

NameTypeDescriptionExample
typeString-type="number"
subTypeString-text
idStringAttribute specifies a unique id for an HTML elementid="phone"
labelStringDefines a label for input elementPhone
placeholderStringSpecifies a short hint that describes the expected value of an input fieldplaceholder="enter phone number"
keyStringkey is a string (also called a “property name”) on formdata objectformdata={phonenumber: ""}
formatStringIf format given as hash string allow number input inplace of hash. If format given as function, component calls the function with unformatted number and expects formatted number.{format: "(###) ###-####"}O/P: (011) 650-8962
prefixStringAdd a prefix before the numberString (ex : $)
requiredBooleanSpecifies that an input field must be filled out before submitting the formrequired=true

Textarea Field

NameTypeDescriptionExample
typeString-type="textarea"
subTypeString-text
idStringAttribute specifies a unique id for an HTML elementid="description"
labelStringDefines a label for input elementDescription
keyStringkey is a string (also called a “property name”) on formdata objectformdata={description: ""}
requiredBooleanSpecifies that an input field must be filled out before submitting the formrequired=true

Checklist Field

NameTypeDescriptionExample
typeString-type="checklist"
idStringAttribute specifies a unique id for an HTML elementid="cultivartype"
labelStringDefines a label for input elementCultivar type
keyStringkey is a string (also called a “property name”) on formdata objectformdata={cultivar_type: ""}
optionsArrayList of one or more options.options: "Sativa", "Indica"

Choice Field

NameTypeDescriptionExample
typeString-type="choices"
subTypeString-text
idStringAttribute specifies a unique id for an HTML elementid="designation"
labelStringDefines a label for input elementDesignation
placeholderStringSpecifies a short hint that describes the expected value of an input fieldplaceholder="enter designation"
keyStringkey is a string (also called a “property name”) on formdata objectformdata={designation: ""}
optionsArrayList of one or more options with key value pair.options: { label: "Owner", value: "owner" }, { label: "Manager", value: "Manager" }

InputRange Field

NameTypeDescriptionExample
typeString-type="input-range"
idStringAttribute specifies a unique id for an HTML elementid="quantity"
labelStringDefines a label for input elementQuantity
keyStringkey is a string (also called a “property name”) on formdata objectformdata={quantity: ""}
minValueNumberSet a minimum value for your component. You cannot drag your slider under this value.formdata={minValue: ""}
maxValueNumberSet a maximum value for your component. You cannot drag your slider beyond this value..formdata={maxValue: ""}
formatLabel(value: number, type: string): stringBy default, value labels are displayed as plain numbers. If you want to change the display, you can do so by passing in a function. The function can return something different, i.e.: append a unit, reduce the precision of a number..formdata={ formatLabel: (value) => ${value}}

Date Field

NameTypeDescriptionExample
typeString-type="date"
subTypeString-text
idStringAttribute specifies a unique id for an HTML elementid="dateofbirth"
labelStringDefines a label for input elementDate Of Birth
placeholderStringSpecifies a short hint that describes the expected value of an input fieldplaceholder="Date of birth"
keyStringkey is a string (also called a “property name”) on formdata objectformdata= { date_of_birth: ""}
maxDateNumberSpecify the maximum enterable date in the DatePicker.formdata={maxDate: moment().subtract(21, "years")}
formatStringThe date format, combination of d, dd, D, DD, m, mm, M, MM, yy, yyyy.formdata={format: "yyyy-MM-dd"}
requiredBooleanSpecifies that an input field must be filled out before submitting the formrequired=true