1.0.9 • Published 2 years ago

dynamic-form-json v1.0.9

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

GitHub license dynamic-form-json version npm version

Dynamic Form Json

dynamic-form-json is a tiny library to generate a Form in React automatically based on certain array of object that passed as a props. This library use regular css created from scratch, so user can customize it in the future. On top of it, i use Formik and Yup for form's skeleton and validations. Credit goes to vijayranghar for the inspiration how to create dynamic validation on Yup from this link.

Installation

npm install dynamic-form-json or yarn add dynamic-form-json

Peer Dependencies

Remember you also need to install the peer dependencies of this package. They are formik, yup, and styled-components.

How to Use

Incase you are curious to try this library, you can implement it as the source code below.

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.render(<App />, document.getElementById("root"));

App.js

import DynamicForm from 'dynamic-form-json';
import { formData } from './data/formData';

function App() {
  const handleSubmission = val => {
    console.log('Values : 'val)
  }
  return (
    <div>
      <h2>My Amazing Form</h2>
      <DynamicForm fields={formData} cbSubmit={handleSubmission} />
    </div>
  )
}

export default App;

formData.js

export const formData = [
  {
    id: "name",
    label: "Full name",
    placeholder: "Enter full name",
    type: "text",
    validationType: "string",
    value: "",
    validations: [
      {
        type: "required",
        params: ["name is required"],
      },
      {
        type: "min",
        params: [5, "name cannot be less than 5 characters"],
      },
      {
        type: "max",
        params: [10, "name cannot be more than 10 characters"],
      },
    ],
  },
];

Supported Fields

Currently this library supports form input types such as:

  • text

  • select

  • textarea

  • radio

  • checkbox

  • upload

API

DynamicFormJSON - DynamicForm(fields: Array[Object], cbSubmit: Func)

This library could be imported by any name you like because we export it by default approach. It also accepts two parameters which are named fields and cbSubmit. All params are required.

import DynamicForm from "dynamic-form-json";

fields is a property that accepts array of object to create the inputs element inside a Form. The last one is cbSumbit, which will handle the submission for you. It accepts a callback function.

TextField

These are the properties you can pass to an Object in formData array to create TextField component. Not all of them are required. The properties required are id, and type.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="email" => label="Email"
placeholderThe placeholder of the fieldstringfalse""
typeThe type of the fieldstring <= enum"text", "password", "number", "email"true""
valueThe default value of the fieldstringfalse""
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

SelectField

These are the properties you can pass to an Object in formData array to create SelectField component.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="city" => label="City"
placeholderThe placeholder of the fieldstringfalse"Please select"
typeThe type of the fieldstring <= "select"true""
valueThe default value of the fieldstringfalse""
optionsThe option list of the field / dropdownarrayfalse[]
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

TextArea

These are the properties you can pass to an Object in formData array to create TextArea component.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="address" => label="Address"
placeholderThe placeholder of the fieldstringfalse""
typeThe type of the fieldstringtrue <= "textarea"""
valueThe default value of the fieldstringfalse""
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

Radio

These are the properties you can pass to an Object in formData array to create Radio component.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="gender" => label="Gender"
placeholderThe placeholder of the fieldstringfalse""
typeThe type of the fieldstringtrue <= "radio"""
valueThe default value of the fieldstringfalse""
optionsThe option list of the radio fieldarrayfalse[]
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

Checkbox

These are the properties you can pass to an Object in formData array to create Checkbox component.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="hobbies" => label="Hobbies"
placeholderThe placeholder of the fieldstringfalse""
typeThe type of the fieldstringtrue <= "checkbox"""
valueThe default value of the fieldstringfalse""
optionsThe option list of the checkbox fieldarrayfalse[]
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

UploadField

These are the properties you can pass to an Object in formData array to create UploadField component.

NameDescriptionPropTypeRequiredDefault Props
idThis id will be put as the name of the field / input elementstringtrue"" / empty string
labelThe label of the fieldstringfalseId (uppercase the first letter of id props). Example: id="photo" => label="Photo"
placeholderThe placeholder of the fieldstringfalse""
typeThe type of the fieldstring <= "upload"true""
valueThe default value of the fieldstringfalse""
validationTypeThe validation type of the field. This is related to the type of data you'll enter in your field. If the data you will input to the field is number, you should make this as number.stringfalse"string"
validationsValidation rule for this field. This is similar to yup API because we used yup under the hood.arrayfalse"[]"

Info

This package is still on development. Not ready yet to use in production.

1.0.9

2 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago