2.0.1 • Published 3 years ago

form-generator-vue v2.0.1

Weekly downloads
88
License
BSD-3-Clause
Repository
-
Last release
3 years ago

Create beautiful forms using any component library for vue.

Features

  • reactive schema based form.
  • compatible with third party component libraries like vuetify, element etc and custom components.
  • customizable form layout.

Demo

Installation

npm install form-generator-vue

How to use

<template>
    <form-generator-vue 
        v-model="fields"
        :components="formComponents"
        :schema="schema"
        :on-submit="handleSubmit"
    >
        <template v-slot:footer>
            <button type="submit">submit</button>
        </template>
    </form-generator-vue>
</template>

<script>
import FormGeneratorVue from 'form-generator-vue';
export default {
    data() {
        return {
            fields: {}
        }
    },
    components: {
        FormGeneratorVue
    },
    computed: {
        formComponents: () => [
            {   
                name: 'v-text-field', //should be a globally registered component.
                type: ['text', 'password', 'email', 'number'],
                errorProp: 'errorMessages'
            },
            {   
                name: 'v-radio-group',
                type: ['radio']
            }
        ],
        schema() {
          return {
            fields: [
              {
                model: 'email',
                type: 'email'
              },
              {
                model: 'mobile',
                type: 'number'
              },
              {
                model: 'password',
                type: 'password'
              }
            ]
          }
        }
    },
    methods: {
        async handleSubmit() {
            // await network call ---------;
            console.log('form submitted', this.fields)
        }
    }
}
</script>

Props

propstypedescription
schemaobjjson schema to create form
componentsobjcomponent map to render component for specific type of field
onSubmitasync/sync functionsubmit success function
onSubmitFailasync/sync functionsubmit fail function.
disabledBooltoggle disable all fields
activeValidationbooltoggle validation on input for all fields. Default is false
activeValidationDelayNumberdebounced validation for all fields
logsbooltoggle validation and submit logs
classesobjTo add classes to all the rows and columns inside form body

field schema Options

optionstypedefaultpurpose
modelStringv-model with component.
typeString'text'Input type. Component for it is loaded from components.
propsobj{}Component props
hidebool/() => boolfalseshow/hide field. Not required when hidden
v-onobj{}Assigned to component v-on.
validator() => //return error datafunction returning error data(string, array etc) on validation fail.
componentStringfor using any component
errorPropString'errorMessages'name of error prop consumed by component to show error
activeValidationBooleanfalsetoggle validation on input
activeValidationDelayNumber0debounced validation

Layout

Row is a div containing column div containing the component. A row can have multiple cols. Nested rows are not supported.

fields: [   // 1 row 1 column -------
            {
                model: 'name',
                type: 'text'
            },
            // 1 row 1 column -------
            {
                model: 'email',
                type: 'email'
            },
            
            // 1 row 2 columns -------   
            [
                {
                    model: 'mobile',
                    type: 'number'
                },
                {
                    model: 'password',
                    type: 'password'
                }  
            ]
        ]

Slots

  • header
  • before-row
  • after-row`
  • before-col
  • after-col
  • <model> (for using the component slot)
  • before-<model>
  • after-<model>
  • footer

slot prop model is passed to all slots except for slots header and footer.

classes used

  • form - "fgv-form"
    • header - "fgv-form__header"
    • body - "fgv-form__body"
      • row - "fgv-formbodyrow"
        • col - "fgv-formbodyrow__col <model>"
    • footer - "fgv-form__footer"

Contributors

This project exists thanks to all the people who contribute. Contribute.

Changelog

2.0.1

3 years ago

2.0.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 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.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago