Git Product home page Git Product logo

vfg-field-array's Introduction

vfg-field-array

Rollup badge Jest Vue Storybook Commitizen semantic-release Npm badge Build Status

A vue-form-generator field to handle arrays

Generated using vue-cli-template-library.

Examples

Simple array

Simple

{
  model: {
    array: ["item1", "item2", "item3"]
  },
  schema: {
    fields: [
      {
        type: "array",
        label: "Array field",
        model: "array",
        showRemoveButton: true
      }
    ]
  }
}

With container component

With container

{
  model: {
    array: ["item1", "item2", "item3"]
  },
  schema: {
    fields: [
      {
        type: "array",
        label: "Array field",
        model: "array",
        itemContainerComponent: "Container"
      }
    ]
  }
}

Container.vue

<template>
  <div>
    <div>
      <div class="title">
        <a @click="contentVisible = !contentVisible">
          {{contentVisible ? "-" : "+"}} Container ({{model}})
        </a>
        <a @click="$emit('removeItem')">
          X
        </a>
      </div>
      <div class="content" v-if="contentVisible">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: ["model"],
    data() {
      return {
        contentVisible: false
      };
    }
  };
</script>
<style scoped>
  .title { background: whitesmoke; }
  .content { border: 2px solid whitesmoke; padding: 10px; }
  a { color: blue; text-decoration: underline; }
</style>

With container component and object as array item

With container and object

{
  model: {
    columns: [{
      "label": "Name",
      "field": "full_name",
      "template": ""
    }, {
      "label": "URL",
      "field": "html_url",
      "template": ""
    }, {
      "label": "Date",
      "field": "date",
      "template": ""
    }],
  },
  schema: {
    type: 'array',
    label: 'Columns',
    model: 'columns',
    itemContainerComponent: 'WidgetListColumnEditorContainer',
    showRemoveButton: false,
    fieldClasses: 'arrayEditor',
    newElementButtonLabelClasses: "button is-primary",
    items: {
      type: 'object',
      default: {},
      schema: {
        fields: [{
          type: 'input',
          inputType: 'text',
          label: 'Label',
          model: 'label',
        },{
          type: 'input',
          inputType: 'text',
          label: 'Field',
          model: 'field',
        },{
          type: 'sourcecode',
          label: 'Template',
          model: 'template',
        }]
      }
    }
  }
}

With bootstrap 4 container component, object as array item, validation and inputName

model: {
  columns: {}
},
schema: {
  fields:[
    {
      type: "array",
      label: 'Columns',
      model: 'columns',
      inputName: "columns",
      showRemoveButton: false,
      newElementButtonLabelClasses: "btn btn-outline-dark mt-2",
      itemContainerComponent: "field-array-bootstrap-accordion-item",
      newElementButtonLabel: "+ Add Column",
      itemContainerHeader: function(model, schema, index) {
        return "Column " + (index + 1) + (model && model.label ? " (" + model.label + ")": "");
      },
      items:{
        type: 'object',
        schema: {
          fields: [
              {
                  type: "input",
                  inputType: "text",
                  label: "Name",
                  model: "name",
                  inputName: "name",
                  required: true,
                  validator: "string",
              },
              {
                  type: "input",
                  inputType: "text",
                  label: "Description",
                  model: "description",
                  inputName: "description",
                  validator: "string"
              },
              {
                  type: "select",
                  label: "Field Type",
                  model: "type",
                  inputName: "type",
                  required: true,
                  validator: "string",
                  values: [
                      {id: "string", name: "Text Field"},
                      {id: "text", name: "Text Area"},
                      {id: "number", name: "Number"},
                      {id: "date", name: "Date"},
                      {id: "select", name: "Single selection"},
                      {id: "multiselect", name: "Multiple Selection"},
                      {id: "boolean", name: "True/False"}
                  ]
              },
              {
                  type: "array",
                  label: "Values",
                  model: "values",
                  inputName: "values",
                  validator: "array",
                  showRemoveButton: true,
                  showModeElementUpButton: true,
                  showModeElementDownButton: true,
                  itemFieldClasses: "form-control",
                  itemContainerClasses: "input-group pb-2",
                  newElementButtonLabelClasses: "btn btn-outline-dark",
                  removeElementButtonClasses: "btn btn-danger input-group-append",
                  moveElementUpButtonClasses: "btn btn-outline-dark input-group-append",
                  moveElementDownButtonClasses: "btn btn-outline-dark input-group-append",
                  newElementButtonLabel: "+ Add Value",
                  visible: function(model) {
                      return model && (model.type === "select" || model.type === "multiselect");
                  },
                  required: function(model) {
                      return model && (model.type === "select" || model.type === "multiselect");
                  }
              },
              {
                  type: "input",
                  inputType: "number",
                  label: "Rows (optional)",
                  model: "rows",
                  inputName: "rows",
                  validator: "integer",
                  visible: function(model) {
                      return model && model.type === "text";
                  }
              }
            ]
        }
      }
    }
  ]

},
formOptions: {
  validateAfterChanged: true
}

Installation

npm install vfg-field-array

vfg-field-array can be used as a module in both CommonJS and ES modular environments.

When in non-modular environment, vfg-field-array will register all the components to vue by itself.

ES6

//
// You can register a component manually
//
import { FieldArray } from 'vfg-field-array';

export default {
  ...
  components: {
    FieldArray
  },
  ...
};

//
// or register the whole module with vue
//
import ModuleLibrary from 'vfg-field-array';

// Install this library
Vue.use(ModuleLibrary);

CommonJS

//
// You can register a component manually
//
var Vue = require('vue');
var ModuleLibrary = require('vfg-field-array');

var YourComponent = Vue.extend({
  ...
  components: {
    'field-array': ModuleLibrary.FieldArray
  },
  ...
});

//
// or register the whole module with vue
//
var Vue = require('vue');
var ModuleLibrary = require('vfg-field-array');

// Install this library
Vue.use(ModuleLibrary);

Browser

<script src="path/to/vue/vue.min.js"></script>
<script src="path/to/vfg-field-array/dist/vfg-field-array.min.js"></script>
<!-- Components are registered globally -->

After that, you can use it with Vue Form Generator:

  schema: {
    fields: [
      {
        type: "array",
        label: "My array",
        model: "myArray"
      }
    ]
  }

Changelog

See the GitHub release history.

Contributing

See CONTRIBUTING.md.

vfg-field-array's People

Contributors

goatandsheep avatar gwenaelp avatar iurquiza avatar mix359 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vfg-field-array's Issues

[ERROR] Error during usage bootstrap accordion item

Dear @gwenaelp,
I'm having some issues during usage of the component that uses bootstrap accordion item.
here the console log:

Unknown custom element: <field-array-bootstrap-accordion-item> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

here some dependencies:
"vue": "^2.5.2",
"vue-form-generator": "^2.2.2",
"bootstrap": "^4.1.3",
"jquery": "^3.3.1",
"vfg-field-array": "0.0.0-development"

here the snippet of the code:

<template>

  <vue-form-generator :model="model"
                      :schema="schema"
                      :options="formOptions"
                      ref="form">
  </vue-form-generator>

</template>

and at the script part I just copied the code at:
(https://github.com/gwenaelp/vfg-field-array#with-bootstrap-4-container-component-object-as-array-item-validation-and-inputname)

and the main.js:

...
import VueFormGenerator from 'vue-form-generator'
import 'vue-form-generator/dist/vfg.css'
import FieldArray from 'vfg-field-array'

...
Vue.use(VueFormGenerator)
Vue.use(FieldArray)

what I'm doing wrong?

Console for my project now shows multiple warnings for running in development mode

When I added vfg-field-array and vfg-field-object to my vue project I get warning for each of these libraries that I'm running in development mode. The warnings show the files are vfg-field-array.esm.js:9392 and vfg-field-object.esm.js:9128.

This happens when I'm running my project in development mode. Seems like it should only happen if I'm running one of these libraries in development mode, not when I'm running my project in development mode.

I just thought you should know. I like these libraries and plan on using them in multiple projects.

Browser

Hi,

First, thanks a lot for your Vue Form Generator plugin!

The thing is that I need this code to work on one page I'm using in a Node project.
I ran the built script, copied the files in my public folder and now I get an error when including the vfg-field-array.min.js. It looks like VueFormGenerator is not defined:
"Uncaught TypeError: Cannot read property 'abstractField' of undefined"

What am I missing? Thanks!

required validation always returns true in the object as array item example

I'm using the example provided in _With bootstrap 4 container component, object as array item, validation and inputName
_ section inside a Vue Form Wizard, looking at the validated event emitted, it always returns true even before adding the first item in the array which causes the logic of my form to break, is there any way to propagate the "required" validation rule all the way up to the top level object?

Unable to install the component

I'm getting these two errors:

Unknown custom element: <field-array> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

and

TypeError: this.$refs.child.clearValidationErrors is not a function

My component looks like this:

import VueFormGenerator from "vue-form-generator";
import { FieldArray } from 'vfg-field-array';
import sections from "~/static/sections.json";
export default {
  layout: 'dashboard',
  components: {
    "vue-form-generator": VueFormGenerator.component,
    FieldArray 
  },
  data() {
    return {
      selectedSection: 0,
        selectedSubSection: null,
        sections: sections,
        formOptions: {
          validateAfterLoad: false,
          validateAfterChanged: false
    }
    }
  },

Install both vfg-field-array and vfg-field-object

In the following example:
With container component and object as array item

You are using vfg-field-array and vfg-field-object. How do you install both? The docs say to do this in the main.js file

import ModuleLibrary from 'vfg-field-array'
Vue.use(ModuleLibrary);

and
import ModuleLibrary from 'vfg-field-object';
Vue.use(ModuleLibrary);

How do you use both?

Default object passed as references to all the components

Hi,
I'm trying this component for one of my project.
I've followed one of the example that use this component in conjunction with the field-object, and following that example, in my schema I've an "items.default: {}".

Doing that, I've found a strange behavior: When I have more than once instance of the object, they are all referring to the same default object, so the similar fields are compiled with the same values in all the instances of the component. For example, if you have a text input in the schema of the field-object, all those text input will share the same values.

Looking at the code of the field-array.vue, I see that you're assigning the same object and pushing it to the value array (around line 89), shouldn't be deep cloned? So that every time there's a new instance of the object and not a reference to the same.

Thanks for your work!
Daniele

newElementButton and removeElementButton props/schema

Hi,
I'm trying to figure out how to set the removeElementButtonLabel and newElementButtonLabel props from the field schema. There's any way? Why they aren't schema properties, like the showRemoveButton?

Shouldn't exist a removeElementButtonLabelClasses like the newElementButtonLabelClasses?

If I make a standard boostrap accordion item container, could I make a PR, to be included? Or a way to structure the component in a way that is compatible with those html/css class structure required by bootstrap & co.
That can be an extra component, shipped with this packages, that one can import and assign to the itemContainerComponent.

Thanks
Daniele

Custom element fix

I had an issue with this config

data () {
            return {
                valid: false,
                success: false,
                loading: true,
                message: "",
                model: {
                    tournamentManagers: []
                },
                schema: {
                    fields: [
                        {
                            type: "array",
                            model: "tournamentManagers",
                            items: {
                                type: "select",
                                values: []
                            }
                        }
                    ]
                },
                formOptions: {
                    validateAfterLoad: true,
                    validateAfterChanged: true,
                    validateAsync: true
                }
            }
        },

It told me it couldnt find the element "field-select", which comes from

getFieldType(fieldSchema) {
        return "field-" + fieldSchema.type;
      },

Thats becuase the components property in the array element is not there, fixed by by ovveriding that at import

import fieldArray from "vfg-field-array/src/components/field-array.vue";

import fieldComponents from "vue-form-generator/src/utils/fieldsLoader.js";
fieldArray.components = fieldComponents

Vue.component("fieldArray", fieldArray);

But of course it would be better if this could be fixed in the module itself

array of arrays

by using this package, is it possible to have two fields on each row in the array? I mean when I click on "add new", it should add two text boxes on the same line(say, "name" and "value").
if this is doable, please let me know how to.
Thanks.
@gwenaelp any idea how to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.