npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aca-form-generator

v1.0.67

Published

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

Downloads

431

Readme

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'