0.1.1 • Published 4 years ago

masterform v0.1.1

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

MasterForm

Plug and play form library for React/React Native

WARNING - Not production ready yet


Installation

Use npm

npm install masterform

or yarn

yarn add masterform

Features

  • Supports both React and React Native
  • Plug and play form building
  • Form validation
  • External form state management

Usage

import React, { useState } from 'react';
import MasterForm from 'masterform';

const Form = () => {
    const [formState, setFormState] = useState({
        username: 'Mark', // keys in formState are from the name prop
        birthday: null, // passed to the different form fields
    });

    return (
        <MasterForm formState={formState} setFormState={setFormState}>
            <MasterForm.TextInput name={'username'} label={'Username'} />

            <div>
                {'Display anything in between form fields'}

                <MasterForm.DateTimePicker name={'birthday'} label={'Birthday'} />
            </div>
        </MasterForm>
    );
};

export default Form;

MasterForm

PropRequiredDescription
formStateYesState variable from useState
setFormStateYesUpdate function from useState
childrenNoCan have any arbitrary children

Available Fields

TextInput

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

Picker

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
optionsNo[]Array of labels and values [ {label: 'A', value: 'a'}, {label: 'B', value: 'b'} ]
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

CheckBox

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
radioNofalseAct as a radio button instead of a checkbox
noUncheckNofalseCan the box be unticked?
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

CheckBoxGroup

CheckboxGroup controls multiple CheckBoxes as a group so that only one can be ticked.

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
multipleNofalseCan tick multiple boxes
noUncheckNofalseCan the boxes be unticked?
optionsNo[]Array of labels and values [ {label: 'A', value: 'a'}, {label: 'B', value: 'b'} ]
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

DateTimePicker

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
modeNodatedate, time, datetime, month
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

FileInput

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
multipleNonullPick multiple files
typeNonullType of file to pick null, image, spreadsheet
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

ImagePicker

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
multipleNonullPick multiple images
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

ColorPicker

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
disabledNofalseIs field disabled (not editable)
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

PlaceInput

PropRequiredDefaultDescription
nameYesName of the field. Used as key in the form state
labelNonullLabel shown above the field
placeholderNonullPlaceholder content when field is empty
disabledNofalseIs field disabled (not editable)
modeNomapmap, text
validatorsNo[]See form validation
containerStyleNo{ minWidth: 200, maxWidth: 410, margin: 4 }Style of the field container

Section

A section can contain other form fields as children. Use this to divide a large form into sections for a cleaner UI.

PropRequiredDefaultDescription
nameNonullIf supplied, form fields within the section will store their values in a JSON key with this name, inside the main formState
valueNo{}If name is supplied, form fields within the section will use this JSON for their initial values
titleNonullTitle of the section
labelNonullAlternative to title
disabledNofalseIs field disabled (not editable)
styleNo{}Style of the section container
titleStyleNo{}Style of the section title
labelStyleNo{}Style of the section label

RepeatingRow

TODO

SubmitButton

A round button that validates all form fields when clicked. onClick is only called when all fields are valid.

PropRequiredDefaultDescription
nameNo'masterFormSubmitButton'Name of the field. Used as key in the form state
labelNo'SUBMIT'Label of the button
disabledNofalseIs field disabled (not editable)
onClickNonullFunction receives formState, areAllFieldsValid and validateAllFields function
onPressNonullAlternative to onClick
noValidationNofalseCall onClick without any validation
skipValidationNofalseCall onClick but do validation also
colorNo'orange'red, orange, green, blue, purple, white, gray, black
invertedNofalseSwitch button and label colors
styleNo{}Style of the button
labelStyleNo{}Style of the button label

Form validation

Each field has validators prop which is an array of validation functions and their corresponding error messages.

validators = [
    {
        function: (value, formState) => value.length >= 10, // error is displayed if this returns false
        errorLabel: 'At least 10 characters are required',
    },
];

Each function receives the current value of the field and the complete formState. If the function returns false, the errorLabel is displayed below the field.

License: MIT