1.0.61 • Published 4 days ago

aca-form-generator v1.0.61

Weekly downloads
181
License
-
Repository
-
Last release
4 days ago

aca-form-generator

You can find a succint storybook detailing main components props and events :
https://aca-form-generator.netlify.com/storybook/

FormGenerator

What is it

FormGenerator is a component used to display a form, without styling.

What it does

It takes a JSON object describing the form and data. It handles user inputs. It returns validation errors.

What it doesn't do

It doesn't handle sending to the form to any backend. It doesn't handle step navigation.

Installation

npm install aca-form-generator

Usage

import { FormGenerator } from 'aca-form-generator'
<script>
export default {
    components: {
        FormGenerator,
    },

    data() {
        return {
            errors: {},
            visibleErrors: {},
            schema: {},
            model: {},
        }
    }
}
</script>
<template>
    <FormGenerator
        :schema="schema"
        :model="model"
        :errors="errors"
        :visibleErrors="visibleErrors"
        @fieldChange="({ field, value }) => this.model = Object.assign({}, this.model, { [field.model]: value })"
        @errorsChange="({ errors }) => (this.errors = errors)"
    >
    </FormGenerator>
</template>

Props

schema : required
Literal object describing the form.

{
    fields: [
        {
            type: 'input',
            model: 'username'
        }, 
        // ...
    ]
}

You can find more example of common fields in this file :
https://github.com/Acadomia/aca-form-generator/blob/master/src/stepsSkeletonExample.js

model
Literal object representing the data displayed in the different fields :

{
    username: 'my-username'
}

errors
Literal object representing the errors :

{
    username: ['this field is required.']
}

visibleErrors
Literal object representing the errors displayed :

{
    username: true
}

Events

fieldChange
Emitted when the user is typing in an input for example.
Parent component is responsible for updating the model prop accordingly :

<FormGenerator
    :model="this.model"
    @fieldChange="({ field, value }) => this.model = Object.assign({}, this.model, { [field.model]: value })"
    <!-- ... -->

errorsChange
Emitted when a field validations messages change.
Parent component can listen to the event to disabled "submit" button for example :

<FormGenerator
    @errorsChange="({ errors }) => this.errors = errors"
    <!-- ... -->
<button 
    :disabled="errors.length" 
    <!-- ... -->

fieldFocus
Emitted when a field gains focus.

<FormGenerator
    @fieldFocus="({ field }) => ..."
    <!-- ... -->

fieldBlur
Emitted when a field loses focus.
Useful to manipulate visibleErrors.

<FormGenerator
    @fieldBlur="({ field }) => ..."
    <!-- ... -->

Accepted fields

For the moment FormGenerator can only render these fields:

  • checklist
  • select
  • checkbox
  • radio
  • input
  • numeric-input
  • textarea
  • zipcode
  • upload
  • html

New fields can easily be added to FormGenerator.vue
Every field must have at least a field props, describing the field,
And a value prop describing its value.
It must emit an event valueChange when the value changes.

Custom fields

Each field is wrapped in a slot.
So you can easily create a custom field, or change an existing field behaviour.

<FormGenerator
    :schema="{ field: [ { type: 'myCustomField', ... }, ... ] }"
    ...
>
    <template #myCustomField="{ field, value }">
        <!-- create your field here using {{ field }} and {{ value }} variables-->
    </template>
</FormGenerator>

Validation

Validation is done when the model changes by calling the validators functions defined in each schema.fields[].validators

Validators

Validators are defined in this file :
https://github.com/Acadomia/aca-form-generator/blob/master/src/fieldValidators.js Each validator is a function retuning undefined when the field value is valid.
Or an array of error messages when something is invalid.
The validator function takes 3 arguments:

  • field the field whe want to validate
  • model all the values for the given form
  • messages a facultative object with the translated error messages.

Usage

import { fieldValidators } from 'aca-form-generator'

A minimal validator could look like this :

checked(field, model) {
    if (!model[field.model]) {
        return ['This checkbox must be checked']
    }
},

A more complete example would be:

checked(field, model, messages) {
    if (!model[field.model]) {
        return [messages.mustBeChecked]
    }
},

You can transate or customize the error messages using the function withMessages.

fieldValidators.checked.withMessages({
    mustBeChecked: 'Ce champ doit être coché.'
})

Custom error

Using the scopped slot errors you can customize validation errors.

<FormGenerator
    ...
>
    <template #errors="{ field, errors }">
        <!-- Display errors here, the way you want. -->
    </template>
</FormGenerator>

Steps

Just like validators, to keep the code reusable, step logic is not coupled to the component FormGenerator

The mixin stepMixin.js is here to help you handle multi step forms.
You can find an example of how to use all these elements together to make a multistep form generated with a JSON comming from an API here :
https://github.com/Acadomia/aca-form-generator/blob/master/src/screens/ExampleScreen.vue

You can also find the code of the mixin here :
https://github.com/Acadomia/aca-form-generator/blob/master/src/stepsMixin.js

Usage

import { stepsMixin } from 'aca-form-generator'
1.0.61

4 days ago

1.0.60

4 months ago

1.0.59

4 months ago

1.0.58

10 months ago

1.0.55

11 months ago

1.0.54

11 months ago

1.0.53

11 months ago

1.0.52

12 months ago

1.0.57

11 months ago

1.0.56

11 months ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.49

1 year ago

1.0.51

1 year ago

1.0.50

1 year ago

1.0.38-beta.0

1 year ago

1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

2.0.0-beta.1

2 years ago

1.0.42

1 year ago

2.0.0-beta.0

2 years ago

1.0.41

1 year ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

1 year ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.19

2 years ago

1.0.2

3 years ago

1.0.18

2 years ago

1.0.1

3 years ago

1.0.17

2 years ago

1.0.0

3 years ago

1.0.16

2 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

0.7.24

3 years ago

0.7.23

3 years ago

0.7.22

3 years ago

0.7.21

3 years ago

0.7.20

3 years ago

0.7.19

3 years ago

0.7.18

3 years ago

0.7.17

3 years ago

0.7.16

3 years ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.1

3 years ago

0.8.2

3 years ago

0.8.0

3 years ago

0.7.15

3 years ago

0.7.14

4 years ago

0.7.13

4 years ago

0.7.12

4 years ago

0.7.11

4 years ago

0.7.10

4 years ago

0.7.9

4 years ago

0.7.8

4 years ago

0.7.7

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.2

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.2.26

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.3.0

4 years ago

0.2.28

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.3

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago