0.0.5 • Published 2 years ago

json-formik v0.0.5

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

formik-json

Developed by Flipbyte

Build Status npm package Coverage Status license Codacy Badge

formik-json is a wrapper for Formik to easily create forms using JSON / Javascript Object for defining the elements.

Examples

Pre-requisites

The component depends on a few third-party plugins for adding WYSIWYG, select, auto-complete, validation etc. Few of them already come packaged with the extension and few need to be installed separately in your project.

Installation

You can install Formik-json using following steps.

$ npm i @flipbyte/formik-json

Quick Start Guide

Once you've finished installation, you can add the form to any of your components as follows:

Import the Form component

import { Form } from '@flipbyte/formik-json';
Prepare your form object
{
    "id": "my-new-form",
    "label": "My New Form",
    "type": "container",
    "renderer": "form",
    "elements": {
        "save": {
            "type": "field",
            "renderer": "button",
            "name": "save",
            "label": "Save",
            "htmlClass": "btn-success",
            "buttonType": "submit"
        },
        "main": {
            "type": "container",
            "renderer": "div",
            "htmlClass": "row",
            "elements": {
                "title": {
                    "name": "attributes.0.title",
                    "label": "Title",
                    "type": "field",
                    "renderer": "text",
                    "fieldType": "text"
                },
                ....
            }
        },
        ....
    }
}
Add the component anywhere you want
<Form
    schema={ /* Your form object here */ }
    { .../* Other formik props */ } />

Form Component


Form component requires the following properties:

KeyDescription
schemayour schema object
onUpdatecallback when the values are updated
initialValuescheck
Formik propertiesYou can add any of the component props

Schema object


Schema object contains elements which can be one of 2 types: either "container" or "field" Each type needs a renderer to render the specific component. The "container" has an "elements" key within which you can define either new containers or fields.

schema object that has the following keys (all required):

KeyDescription
idthe ID for the form
labelthe title for the form
type"container"
renderer"form" (you can use other renderers but if you want the form to have a <form /> tag use the "form" renderer.)
elementsis an object used to define the elements within the container

Note: The schema object can only have one container. You can have multiple containers and fields inside the elements object of the main schema object.

"elements" is an object with key-value pair where value is another object. The value object can either be a of type "container" or "field".

Each container or field requires a renderer which can be set using "renderer": "{your_renderer}". You can define you own renderers for both containers and keys or use the ones that come with the module.

Following are the properties for each type of container

Common container properties

Not applicableFieldPropertyDescription
nonetypeString"container"
button-groupnameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
editable-grid, tabselements{}is an object that can hold one or more fields or containers within it.
editable-grid, button-groupprefixNameToElementBool
showWhenStringCheck when-condition
commentStringcomment / description for the container
commentClassStringhtml class for the comment element

Container specific properties

ContainerFieldPropertyDescription
editable-gridrendererStringeditable-grid
fields{}An object with one or more field definitions in a key-value pair
buttons{"add": "Add", "remove": "X", "duplicate": "Duplicate"}has 3 properties, all optional. These can be either function that returns the button or string which is the label for a button
isObjectBoolwhether the grid displays an object. If set to true, buttons (add, remove and duplicate) will be disabled.
isSortableBoolwhether the grid rows can be dragged and sorted
tableContainerClassStringhtmlClass for the div wrapping the editable-grid
tableClassStringhtmlClass for the main editable grid
divrendererStringdiv
nameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
htmlClassStringhtmlClass for the div element
html-tagrendererStringhtml-tag
nameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
asStringhtml tag to be used (Default: 'div')
htmlClassStringhtmlClass for the html-tag element
fieldsetrendererStringfieldset
nameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
titleStringlabel for the fieldset
cardClassStringhtmlClass for the main wrapping container
cardHeaderClassStringhtmlClass for the header of the wrapping container
cardHeaderActionsClassStringhtmlClass for the container holding the disclose buttons in the header of the container
cardBodyClassStringhtmlClass for the body of the container
formrendererStringform
nameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
htmlClassStringany character
tabsrendererStringtabs
nameStringis used to prepend parent container's name to the children fields when "prefixNameToElement" is set to true.
tabs{}Object
cardClassStringsame as fieldset
rowClassStringhtmlClass for the row div
tabListClassStringhtmlClass for tab list
tabListItemClassStringhtmlClass for tab list item
tabContentClassStringhtmlClass for tab content container
tabColumnClassStringhtmlClass for tabs container
contentColumnClassStringhtmlClass for wrapping the tab content container
tabActiveClassStringhtmlClass for active tabs
tabPaneClassStringhtmlClass for single tab pane
button-grouprendererStringbutton-group
elements{}the elements can only be of type: "field" with renderer: "button".

Following are the properties for each type of field

Common field properties

FieldTypePropertyDescription
nameStringhtml field name attribute
labelStringthe label for the field
typeString"field"
labelClassStringhtml class for the label html element
formGroupClassStringhtml class for the div that wraps the form field
validationStringCheck yup-schema
showWhenStringCheck when-condition
enabledWhenStringCheck when-condition
fieldClassStringhtml class for the main html/3-rd party form field
commentStringcomment / description that goes below the field
commentAsStringdefine the HTML tag to be used for wrapping the comment. (Default: )
commentClassStringhtml class for the comment element
templateReact ComponentStringdefine your custom template for the field (check src/Template/Default.js) or set the template in the template registry using registerTemplate and pass the string key here
errorAsStringdefine the HTML tag to be used for wrapping the error. (Default: )
errorClassStringhtml class for the error element

Field specific properties

FieldTypePropertyDescription
checkboxrendererStringcheckbox
nameStringhtml field name attribute
labelStringthe label for the field
typeString"field"
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
optionsArrayArray of objects with keys "value" and "label"
code-editorrendererStringcode-editor
nameStringhtml field name attribute
labelStringthe label for the field
options{}
defaultValueStringdefault field value (untested)
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
radiorendererStringradio
nameStringhtml field name attribute
labelStringthe label for the field
options{}
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
react-selectrendererStringreact-select
nameStringhtml field name attribute
labelStringthe label for the field
options{}array of objects with value and label keys. Example: [{"label": "Item 1", "value": "value-1"}]
defaultValueStringdefault field value (untested)
isMultiBoolwhether it's a mult-select
isClearableBooldisplays a clear select button on the select which will clear the selected options
isDisabledBooldisables the select when set to true
noOptionsMessageFunctionrefer ReactSelect Props
switchrendererStringswitch
textrendererStringtext
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
fieldTypeStringHTML input type. The value that goes into
defaultValueStringdefault field value (untested)
iconStringfontawesome icon class
inputGroupClassStringhtml class for the div that wraps an icon and an input element together
textarearendererStringtextarea
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
rowsNumberNumber of rows that the text-area container should show by default
wysiwygrendererStringwysiwyg
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
options{}React-quill wysiwyg options
rowsNumberNumber of rows that the wysiwyg container should show by default
textareaClassStringthe class for the textarea that will show the raw html for the content entered in the wysiwyg
autocompleterendererStringautocomplete
attributes{}is an object that can hold other html field related attributes (if any). Only ones that are not defined using any other key will be used. For example: name already has it's own key and hence "name" key inside the attributes object will do nothing.
defaultValueStringdefault field value (untested)
options{}Options available in react-autosuggest plugin
file-uploaderrendererStringfileuploader
options{}Options available in react-dropzone plugin
formGroupClassStringhtml class for the div that wraps the form field
inner-textrendererStringinner-text
asStringHTML tag to use for the inner-text field
htmlClassStringHTML class for the tag used
defaultValueStringEither used as a static value for the HTML element or as a placeholder when is not defined
buttonrendererStringbutton
contentStringbutton inner html

Thank You

If you have suggestions, comments or ideas to develop more such solutions, you can write to us at Flipbyte.com. PRs are welcome.

License

The MIT License (MIT)