4.0.4 • Published 3 years ago

ivoyant-form-builder v4.0.4

Weekly downloads
4
License
MIT
Repository
-
Last release
3 years ago

iVoyant Form Builder

Quickly build Ant Design forms with a JSON schema.

Installation

yarn add ivoyant-form-builder

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import Form from 'ivoyant-form-builder';
import config from './config.json';

ReactDOM.render(<Form config={config} />, document.getElementById('root'));

In ./config.json

{
  "schema": {
    "customerInfo": {
      "name": "@first @last",
      "address": "@address"
    },
    "isEligible": "@@age >= 65"
  },
  "meta": [
    {
      "columns": 2,
      "fields": [
        {
          "key": "first",
          "label": "First Name",
          "widgetProps": {
            "maxLength": 15
          }
        },
        {
          "key": "last",
          "label": "Last Name"
        },
        {
          "key": "address",
          "label": "Home Address"
        },
        {
          "key": "age",
          "label": "Select age",
          "widget": "select",
          "options": [64, 65, 66]
        },
        "@submitButton"
      ]
    }
  ]
}

The antd-form-builder API drives the creation of antd components managed under a single Form component.

In the above example, meta is an array of ant-form-builder meta objects that operate under the same API as ant-form-builder with some enhancements.

\

ivoyant-form-builder specific API

In order to solve some of the difficulties of creating dynamic forms with JSON, properties can be accessed with @ and functions can be parsed inline with the @@ syntax.

PropertyTypeDefault ValueDescription
schemaobject-Collects the values of your fields. How to use
encryptionKeystring-When an encryption key is provided, it is encrypted with crypto-js a default configuration using AES encryption.
encryptionMethodobject-An object with keys parse and options. parse is a string function that will encrypt the collected form values. The function has access to payload and the spread keys of options. options is an object that contains variables that can be accessed by the parsed function.
overrideEncryptionboolean-if true, encryption will be bypassed even if encryption methods are passed into Form component
forceUpdatebooleanfalseThis will rerender the form on all state changes. Enable if you aren't getting expected dynamic behavior.
formPropsobject-Contains props that will get passed to antd <Form> component

Collecting form values

In the example, schema is an object that conforms to the shape you want your fields in after form validation/collection. To reference a field value, get its key with the @ syntax. To run some processing on the field value, create a function and use the field as a variable.

Example collected meta keys for a form:

{
  "firstName": "Jane",
  "lastName": "Doe",
  "address": "Peachtree St",
  "city": "Atlanta",
  "state": "GA"
}

Example schema

{
  "customerName": "@firstName @lastName",
  "address": "@address, @city, @state",
  "livesInCA": "@@state === 'CA'"
}

parses to:

{
  "customerName": "Jane Doe",
  "address": "Peachtree St, Atlanta, GA",
  "livesInCA": false
}

Thorough example

{
  "schema": {
    "customerData": {
      "name": "@@useTitle ? `${title}. ${first} ${last}` : `${first} ${last}`",
      "address": "@address",
      "email": "@email"
    },
    "preferences": {
      "subscribe": "@@wantsSubscription",
      "sendOffers": "@@wantsOffers"
    }
  },
  "encryptionKey": "abc",
  "meta": {
    "columns": 1,
    "fields": [
      {
        "key": "first",
        "label": "First Name"
      },
      {
        "key": "last",
        "label": "Last Name"
      },
      {
        "key": "useTitle",
        "widget": "checkbox",
        "children": "Prefer title?"
      },
      {
        "key": "title",
        "widget": "select",
        "label": "Select title",
        "options": ["Mr.", "Ms.", "Mrs.", "Dr."]
      },
      {
        "key": "address",
        "label": "Address"
      },
      {
        "key": "email",
        "label": "Email"
      },
      {
        "key": "wantsSubscription",
        "widget": "checkbox",
        "children": "Check if you want to subscribe to emails"
      },
      {
        "key": "wantsOffers",
        "widget": "checkbox",
        "children": "Check if you want third party offers"
      }
    ]
  }
}
const customMethod = ({payload}) => {
  /**
   * EXAMPLE
   * payload = {
   *    customerData: {
   *       name: "Mr. Mike W",
   *       address: "123 Anywhere",
   *       email: "email@email.com"
   *    },
   *    preferences: {
   *       subscribe: false,
   *       wantsOffers: true
   *    }
   * }
   **/
  // do something with the payload

  return // encrypted value
}


React.render(<Form config={config} encryptionMethod={customMethod}>)
4.0.4

3 years ago

4.0.3

3 years ago

4.0.1

3 years ago

4.0.2

3 years ago

4.0.0

3 years ago

3.0.18

3 years ago

3.0.19

3 years ago

3.0.17

3 years ago

3.0.16

4 years ago

3.0.15

4 years ago

3.0.14

4 years ago

3.0.13

4 years ago

3.0.12

4 years ago

3.0.11

4 years ago

3.0.10

4 years ago

3.0.9

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago