1.0.14 • Published 3 years ago

formify-react v1.0.14

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

formify-react

Last form component you will need for you application which works with only a JSON you pass rest it will handle everything

NPM JavaScript Style Guide License Bundle Size

Installation \ Usage \ Props \ Example Form Field \ Form Fields \ Supported Input Fields \ Validators \ Upcoming \ Contributing

Edit formify-react

Install

#npm
npm i -S formify-react

#yarn
yarn add formify-react

Usage

import React, { Component, createRef } from 'react'

import Form from 'formify-react'
import 'formify-react/dist/index.css'

import { formConstants } from './config'

const budgetData = [
  { value: 5000, label: '$5000' },
  { value: 15000, label: '$15000' },
  { value: 25000, label: '$25000' },
  { value: 35000, label: '$35000' },
  { value: 50000, label: '$50000' }
]

class Example extends Component {
  formRef = createRef()

  handleSave = () => {
    const { current } = this.formRef
    const form = current.getFormData()
    if (form.isFormValid) {
      console.log(form.formData)
    }
  }

  handleSubmit = ({ formData, isFormValid }) => {
    if (isFormValid) {
      console.log(formData)
    }
  }

  render() {
    return (
      <div>
        <Form
          model={formConstants}
          ref={this.formRef}
          data={{ budget: budgetData }}
          onSubmit={this.handleSubmit}
        />
        <button onClick={this.handleSave}>Save</button>
      </div>
    )
  }
}

getFormData() returns an object of {isFormValid, formData}. Same object will be returned if you use onChange function to get latest change while you update any field. getFormData is only be accessible when you use ref.

New Update:- Now you can pass onSubmit callback function to enable native form submit (with pressing enter);

Props

PropertyDescriptionTypeDefaultRequired
modelForm model array with all fieldsArrayYes
refRef to get access to Form DOM RefReact RefNo
dataObject of dropdown data with field name of dropdown fieldObject{}No
valuesObject of all fields in forms with values to be populated in formObject{}No
onChangeThis is an optional callback if you need to get form values when an input is changedFunctionNo
onSubmitThis is an optional callback if you need to get form values when pressed enter to submit to your serviceFunctionNo

Model

import { Validators } from 'formify-react'

export const formConstants = [
  {
    field: 'email',
    type: 'email',
    validators: [
      { check: Validators.required, message: 'Email is mandatory' },
      { check: Validators.email, message: 'Email entered is not valid' }
    ],
    required: true,
    title: 'Email',
    placeholder: 'Enter your email',
    styleClass: 'col-sm-6',
    extraProps: {
      maxLength: 30
    }
  },
  {
    field: 'budget',
    type: 'dropdown',
    validators: [],
    required: false,
    title: 'Your Budget',
    styleClass: 'col-sm-6'
  },
  {
    field: 'message',
    type: 'textarea',
    validators: [],
    required: false,
    title: 'Message',
    placeholder: 'Enter your message',
    styleClass: 'col-sm-12'
  },
  {
    field: 'notify',
    type: 'checkbox',
    validators: [],
    required: false,
    title: 'Subscribe to our mailing list',
    styleClass: 'col-sm-12'
  }
]

Fields

PropertyDescriptionTypeDefaultRequired
fieldThis property will be used to identify the input and this will be returned with value which can be used to everywhere. e.g: sending to API BodystringYes
typeType of input field to generated in FormInput Type 'input','textarea','dropdown', 'checkbox', 'email', 'url', 'search', 'number', 'password'Yes
validatorsArray of validator functions imported from react-dynamic-formFunction[]Yes
requiredThis field is responsible to mark the field required in formBooleanfalseYes
titleThis field used to display label of the Input FieldString''Yes
styleClassThis field used to do the layout of the fields, you can use your style classes to style inputsString''No
placeholderThis field used to display placeholder in the fieldString''No
extraPropsThis field is used to send any extra html attributes to fieldObject{}No

For Dropdown Data format should be an array of {value:'', label:'',}

Supported

InputValueSample Data
Texttext
Emailemail
Passwordpassword
URLurl
Searchsearch
Numbernumber
Dropdowndropdown{value:'test', label:'Test',}
Checkboxcheckbox
Datedate

These values will be used in type field of form model

Validators

import { Validators } from 'formify-react'

  • {check: Validators.required, message: 'This field is required'}
  • {check: Validators.email, message: 'Email is invalid'}
  • {check: Validators.number, message: 'Only number allowed'}

You can now send custom validator to the form component

/*
  Custom Validator
  @params: value -> received value from inputs
  @params: message -> message which you will add while sending in the validators array of form component
*/
const checkGhazi = (value, message) => {
  if(value && value.includes('ghazi')) {
    return false;
  }
  return {error: true, message};
}

// Add your custom validator function to validators array of the form constant.
validators: [
  {check: checkGhazi, message: 'Text does not contain Ghazi'},
],

Upcoming

  • Custom Validation methods
  • Dropdown with MultiSelect and Autocomplete

Contribution

Contributing Info

License

MIT © gkhan205

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.6

3 years ago

1.0.5

3 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