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

croud-forms

v1.3.0

Published

Croud form building blocks

Downloads

10

Readme

croud-forms

npm version

A package for generating form inputs based on a JS object.

Installation

Install using yarn

yarn install croud-forms

Or if you're after the vue1 version.

yarn install [email protected]

And add to your Vue project as a plugin

import CroudForms from croud-forms

Vue.use(CroudForms)

Usage

Basics

Field object

The field object is made up of 3 mandatory keys and an additional contextual object that is needed for more complex fields, like select or cleave components.

{
    data(){
        return {
            field: {
                field_name: 'First name', // Sets the label text
                field_type: 'text', // Type of field we want to use
                field_slug: 'firstName', // Key of the data object to get and set

                field_options: {
                    select_options: {
                        google_adwords: 'Google Adwords', // key: value
                        adwords_editor: 'Adwords Editor',
                        bing_ads: 'Bing Ads',
                    },
                },
            },

            user: {
                firstName: '',
            },
        }
    },
}

Field components

Croud-forms provides two different components for displaying the form fields.

<!-- Form field component -->
<croud-form-field :field="field" v-model="user[field.field_slug]" />

<!-- Table row component -->
<table class="ui very basic table">
    <tr is="croud-form-row"  :field="field" v-model="user[field.field_slug]"></tr>
</table>

Read Only

Croud-forms allows a read-only flag to be passed into any field as a prop

<croud-form-field :field="field" v-model="user.firstName" :read-only="true"/>

Vuelidate integration

Croud-forms can integrate with vuelidate to provide visual feedback when the output of the form isn't what we expected.

You can add a validation object to your component which can dictate the rules for your fields, below is a very basic example, view the vuelidate docs for more inspiration.

import { validationMixin } from 'vuelidate'
import { required, minLength, numeric, email } from 'vuelidate/lib/validators'

export default {
    mixins: [validationMixin],

    validations: {
        user: {
            firstName: {
                required,
                minLength: minLength(2),
            },
        }
    },

And for the visual feedback, you can pass the validation object through to the croud-form-field, like so...

<croud-form-field field="field" v-model="user.firstName" :validation="$v.user.firstName" />

Bringing it all together

This project includes a croud-form-builder component that can build a form based on a JSON schema and can also handle the validation

You can add this component to your mark up with the following syntax

<croud-form-builder :read-only="false" :schema='schema' v-model="user" :validations="$v.user" />

And you can build up your model, schema and validations from within your component.

import { validationMixin } from 'vuelidate'
import { required, minLength, numeric, email } from 'vuelidate/lib/validators'

export default {
    mixins: [validationMixin],

    data() {
        return {
            user: {
                firstName: '',
                lastName: '',
                email: '',
                age: 0,
            },

            schema: [
                {
                    class: 'two fields',
                    children: [
                        {
                            field_name: 'First name',
                            field_type: 'text',
                            field_slug: 'firstName',
                        },
                        {
                            field_name: 'Last name',
                            field_type: 'text',
                            field_slug: 'lastName',
                        },
                    ],
                },
                {
                    field_name: 'Email Address',
                    field_type: 'email',
                    field_slug: 'email',
                },
                {
                    field_name: 'Age',
                    field_type: 'number',
                    field_slug: 'age',
                },
            ],
        }
    },

    validations: {
        user: {
            firstName: {
                required,
                minLength: minLength(2),
            },
            lasstName: {
                required,
                minLength: minLength(2),
            },
            email: {
                required,
                email,
            },
            age: {
                required,
                numeric,
            },
        },
    },
}

Custom components

If you need to create a more complicated form field than this package provides, you can add pass a render method through to the form builder. If you want to to use a custom component, you will need to globally register it before using it in the render method.

// Globally register custom component
Vue.component('test-component', {
    template: '<div>hello world</div>',
})
...
// Form builder config
{
  field_name: 'Test Component',
  field_type: 'text',
  field_slug: 'custom',
  render: (h) => h('div', {
    class: 'ui field',
  }, [
    h('label', 'Test Component'),
    h('test-component'),
  ]),
},

Which will produce the following markup

<div class="ui field">
  <label>Test Component</label>
  <div>hello world</div>
</div>