0.0.9 • Published 6 years ago

@haakma-org/molgenis-vue-forms v0.0.9

Weekly downloads
-
License
-
Repository
github
Last release
6 years ago

MOLGENIS Vue forms

Vue library for generating web forms

TODO

  • Handle compounds
  • Option schema for providing options or to specify an asynch target to fetch data on search
  • Unit tests
  • Validation expressions
  • Validators pointing to data from other fields

Usage

<template>
    <molgenis-vue-form 
        id="example-form"
        :fields="fields"
        :data="data"
        :options="options">
    </molgenis-vue-form>
</template>
  
<script>
    import MolgenisVueForm from '@molgenis/molgenis-vue-form'
    
    export default {
      name: 'molgenis-vue-form-example',
      data () {
        return {
          fields: [
            {...}
          ],
          data: {
            ...
          },
          options: {
            required: false,
            onSubmit: (formdata) => ...,
            onCancel: () => ...
          }
        }
      },
      components: {
        MolgenisVueForm
      }
    } 
</script>

Specifications

The molgenis-vue-form component accepts the following properties

parameterdescriptionrequireddefault
idAn ID used for the HTML elementYesN/A
fieldsAn Array of field objects. See the specifications and the example.YesN/A
dataA key value map for preselected data in form fields. See the example.No{}
optionsAn option object. See the specifications and the example.YesN/A

Option specifications

parameterdescriptionrequireddefault
readonlySet form to readonlyNoFALSE
onSubmitFunction for what to do on submitYesN/A
onCancelFunction for what to do on cancelYesN/A

Field specifications

parameterdescription
typeHTML input type. Used to render the correct input. See supported types
labelLabel used as a label for the input field.
descriptionDescription placed below the input field. Hidden if description is empty.
requiredA boolean or a function determining whether a field is required.
disabledA boolean or a function determining whether a field is disabled.
readonlyA boolean or a function determining whether a field is readonly (similar to disabled).
visibleA boolean or a function determining whether a field is visible.
validatorsA list of functions which determine whether a field is valid on submit.
optionsAn object containing options for select, radios, and checkboxes typed fields. See examples

Functions in any of the parameters mentioned above should accept a data object containing the data from the form.

Functions should always return true or false.

See the field object examples for code examples.

Supported types

The following types are supported

typerenders
radiosA list of radio buttons
selectA Vue Multiselect dropdown which supports asynchronous and synchronous option lists
numberA HTML5 number input
text-areaA textarea HTML element
dateA Vue Flatpickr Date component
date-timeA Vue Flatpickr Date component with 'enableTime = true'
checkboxesA list of checkboxes
textA HTML5 text input
urlA HTML5 text url
emailA HTML5 text email
passwordA HTML5 password input
fileA HTML5 file input

Examples

Example field object

/**
 * An example of a username field, a password field, and a confirm password field.
 * Because our user is a funny guy, the username should be funny_guy_101. If not, the form will not be valid.
 * The second password field should match the first password field, else the form will not be valid
 */
const fields = [
  {
    type: 'text',
    id: 'username',
    label: 'Username',
    required: true,
    disabled: false,
    visible: true,
    validators: [
      (formdata) => formdata['username'] === 'funny_guy_101' 
    ]
  },
  {
    type: 'password',
    id: 'password',
    label: 'Password',
    required: true,
    disabled: false,
    visible: true
  },
  {
    type: 'password',
    id: 'password-confirm',
    label: 'Confirm password',
    required: true,
    disabled: false,
    visible: true,
    validators: [
      (formdata) => formdata['password'] === formdata['password-confirm']
    ]
  }
]

Example data object

/**
* The following data object contains data for user form which contains an input field for:
* - username
* - country
* - organisation
* - bio
*/
const data = {
  username: 'User',
  country: 'Netherlands',
  organisation: 'Github repositories',
  bio: 'A software developer who loves Vue'
}

Example options object

const options = {
  readonly: true,
  onSubmit: (formdata) => console.log("Nice data: " + formdata),
  onCancel: () => console.log("Why did you close my form :'(")
}

Multiple option fields example

const fields = [
  {
    type: 'select',
    id: 'select-field',
    label: 'Who is it?',
    required: true,
    options: {
      uri: '/api/for/fetching/data',
      options: [],
      multiple: false,
    }
  },
  {
    type: 'radios',
    id: 'radios-field',
    label: 'Who did it',
    description: 'there can be only one',
    required: true,
    options: {
      options: [
        {id: 'captain-yellow', value: 'captain-yellow', label: 'Captain Yellow'},
        {id: 'admiral-blue', value: 'admiral-blue', label: 'Admiral Blue'},
        {id: 'general-red', value: 'general-red', label: 'General Red'}
      ]
    }
  },
  {
    type: 'checkboxes',
    id: 'checkboxes-field',
    label: 'Who are awesome',
    description: 'Everyone can be awesome',
    required: true,
    options: {
      options: [
        {id: 'captain-yellow', value: 'captain-yellow', label: 'Captain Yellow'},
        {id: 'admiral-blue', value: 'admiral-blue', label: 'Admiral Blue'},
        {id: 'general-red', value: 'general-red', label: 'General Red'},
      ]
    }
  }
]

Build setup

yarn - recommended

# Install dependencies
yarn install
  
# Server with hot reload at localhost:3000
yarn run dev
  
# Build for production with minification
yarn run build
  
# Run tests
yarn run test

npm

# Install dependencies
npm install
  
# Server with hot reload at localhost:3000
npm run dev
  
# Build for production with minification
npm run build
  
# Run tests
npm run test

License

GNU GPLv3 © Mark-de-Haan markdehaan90@gmail.com