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

@reside-ic/vue-next-dynamic-form

v1.1.5

Published

A metadata driven form component

Downloads

222

Readme

vue-next-dynamic-form

Build Status

Vue component for generating a form dynamically from metadata.

Installation

  • Install from npm:
    npm install @reside-ic/vue-next-dynamic-form
  • Import into your project and register as a global or local component:
    import {DynamicForm} from "@reside-ic/vue-next-dynamic-form"
      
    // global
    const app = Vue.createApp({})
    app.use(DynamicForm);
      
    // or local
    createApp({
      components: {
        DynamicForm
      }
    }).mount('#app')
      
  • Include the following css file in your app:
    dist/css/style.min.css
    Note that this css file contains some Bootstrap styles. If you already have Bootstrap in your project and don't want to duplicate style declarations you can import src/style-partial.scss into a style bundle, but you must first include Bootstrap's functions, variables, mixins, forms and buttons partials for it to compile.

Browser

To use the dynamic form component directly in the browser, just include dist/js/vue-dynamic-form.min.js on the page, after Vue:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="text/javascript" src="node_modules/@reside-ic/vue-next-dynamic-form/dist/vue-dynamic-form.min.js"></script>

In this case the component will be automatically registered. See example/index.html.

Usage

Example

<template>
    <dynamic-form v-model:form-meta="myFormMeta" 
                  @submit="handleSubmit"></dynamic-form>
</template>
<script>

const app = createApp({
     data() {
          return {
             myFormMeta
           }
        },
     methods: {
         submit(data) {
            axios.post("/my-form", data)
        }
     }
 });
vueNextDynamicForm.default.install(app); 
app.mount('#app');

const myFormMeta = {   
    controlSections: [
        {
            label: "General",
            description: "Select general model options:",
            controlGroups: [
                {
                    label: "Area scope",
                    controls: [
                        {
                            name: "area_scope",
                            type: "multiselect",
                            options: [{id: "MWI", label: "Malawi"}, {id: "MWI.1", label: "Central"}],
                            value: ["MWI","MWI.1"],
                            required: true
                        }]
                },
                {
                    label: "Area level",
                    controls: [
                        {
                            name: "area_level",
                            type: "multiselect",
                            options: [{id: "q1", label: "Apr - Jun 2015"}, {id: "q2", label: "Jul - Sep 2015"}]
                        }]
                }
            ]
        },
        {
            label: "ART",
            description: "Optionally select which quarter of data to use at time point 1 and 2",
            controlGroups: [
                {
                    label: "Number on ART",
                    controls: [
                        {
                            name: "art_t1",
                            label: "Time 1",
                            type: "select",
                            value: "q1",
                            helpText: "Quarter matching midpoint of survey",
                            options: [{id: "q1", label: "Jan - Mar 2015"}, {id: "q2", label: "Apr - Jun 2015"}],
                            required: true
                        },
                        {
                            name: "art_t2",
                            label: "Time 2",
                            type: "select",
                            helpText: "Quarter matching midpoint of survey",
                            options:  [{id: "q1", label: "Jan - Mar 2015"}, {id: "q2", label: "Apr - Jun 2015"}],
                            required: false
                        }
                    ]
                }
            ]
        },
        {
            label: "Advanced options",
            controlGroups: [
                {
                    label: "Number of simulations",
                    controls: [{
                        name: "num_sim",
                        type: "number",
                        required: true,
                        transform: "$/100"
                    }]
                },
                {
                    label: "Decimal parameter",
                    controls: [{
                        name: "dec_param",
                        type: "number",
                        required: true,
                        step: 0.1
                    }]
                }
            ]
        }
    ]
}

</script>

Note that the step value on number controls may be any valid value for HTML input elements of type number. However, in read-only mode the form displays numbers according to the default local Intl.NumberFormat, typically to three decimal places so will round any values with greater precision.

On user submission the form emits a submit event with a payload that contains the form data as JSON. In this example, the payload would be of the form:

{
    area_scope: ["MWI","MWI.1"],
    area_level: [],
    art_t1: "q1",
    art_t2: null,
    num_sim: null
}

If the optional transform property is present on any control, this string is interpreted as a jsonata transform and will be applied to the value emitted.

See this as a working example here

The form also emits a validate event when its valid state changes. The form is valid if all controls which are required have values. The valid event provides a single boolean parameter which is true is the form has become valid, false if it has become invalid. The validate event is also emitted when the form is mounted, indicating the initial valid state.

Example without v-model

If for some reason you don't want form values to be automatically updated, you can handle the change event yourself:

<template>
   <dynamic-form :form-meta="myFormMeta" 
                 @change="handleChange"
                 @submit="handleSubmit"></dynamic-form>
</template>
<script>
    export default {
        data() {
            return {
                myFormMeta: myFormMeta          
            }
        },
        methods: {
            handleChange(newFormMeta) {
                this.myFormMeta = newFormMeta            
            },
            handleSubmit(data) {
                ...
            }
        }
    }
</script>

Optional component props

As well as the required v-model or form-meta, you can optionally customize the submit button text by passing submit-text, and the HTML element's id by passing id:

    <dynamic-form id="my-dynamic-form" 
                  v-model="myFormMeta"
                  @submit="handleSubmit"
                  submit-text="Validate">
    </dynamic-form>

You can also hide the submit button:


    <dynamic-form v-model="myFormMeta"
                  @submit="handleSubmit"
                  :include-submit-button="false">
    </dynamic-form>

Similarly, you can customize the 'required' warning text and the placeholder text used by the select and multi-select inputs by passing strings to the required-text and select-text attributes, respectively.


    <dynamic-form v-model="myFormMeta"
                  @submit="handleSubmit"
                  :select-text="Select an option"
                  :required-text="Compulsory">
    </dynamic-form>

You can also put the form into read-only mode, by setting the readonly attribute, which defaults to false. In read-only mode, input controls are replaced by text fields, and no submit button or required indicators are displayed.


    <dynamic-form v-model="myFormMeta"
                  :readonly="true>
    </dynamic-form>

Control types

At the moment only 3 control types are supported:

  • select
  • multiselect
  • number

Testing, building and publishing

  • To run unit tests with jest: npm test
  • To build distribution files: npm run-script build
  • To publish to npm:
    • first iterate the version in package.json
    • create a user account if you don't have one
    • request organization membership to join reside-ic
    • log into your account : npm login
    • then npm publish --access public