0.17.6 • Published 20 days ago

@ianicdev/dataforms v0.17.6

Weekly downloads
-
License
-
Repository
-
Last release
20 days ago

Installation

With NPM:

npm install --save @ianicdev/dataforms

With Yarn:

yarn add @ianicdev/dataforms

Inside main.js

import Dataforms from "dataforms";
import { extend } from "vee-validate";
import * as rules from "vee-validate/dist/rules";

Vue.use(Dataforms);

Object.keys(rules).forEach((rule) => {
  extend(rule, rules[rule]);
});

Intro

All the components must have some generic props such as label. The component structure is made of three parts:

  1. type (ref: Components)
  2. options (ref: Components Options)
  3. _validation
  4. _responsive

Feel free to use any component attributes you should use on vuetify components (such as dark theme, label, hints etc...) Hint: All the generic props that start with underscore (_) are special attributes and doesnt apply to the component. Its used from the library

Validation

All available validation rules can be found at https://vee-validate.logaretm.com/v3/guide/rules.html#rules. A custom validation callback is provided from the library using the _callback property

validation: {
    _callback() => {
        return {
            validate: (value) => {
                console.log(value);
                return true;
            },
            message: "{_field_} is not valid(?)",
        }
    }
}

Responsive

You can specify the grid system via the responsive properties

_responsive: {
  sm: 12,
  md: 6,
  lg: 4,
},

Generic options

label: "I am a label" hint: "Write something dude!", "persistent-hint": true,

Components

1. textField (default) 2. textArea 3. checkBoxes 4. radioButtons 5. fileInput 6. selects 7. selectsAutocomplete

1. textField (default)

Basic text field

name: {
  options: {
    label: "First name",
    hint: "Είσαι ο;",
    "persistent-hint": true,
  },
  validation: {
    required: true,
  },
},

2. textArea

Same with text field, a basic textarea component with validation

 message: {
      type: "textArea",
      options: {
        label: "Write your message...",
        hint: "Write something dude!",
        "persistent-hint": true,
      },
      validation: {
        min: 3,
        max: 200,
      },
},

3. checkBoxes

This component requires an items array with the available options. Also you can set a default value (optional).

terms: {
  type: "checkBoxes",
      options: {
        label: "Terms & conditions",
        default: "steal",
        hint: "write something here too!",
        "persistent-hint": true,
        items: [
          {
            label: "I aggree to steal all my data!",
            value: "steal",
          },
        ],
      },
      validation: {
        required: true,
      },
},

4. Radio

Radio selects msut have a list property (array) with the available values. You can also provide a default (optional) value

website: {
  type: "radioButtons",
  options: {
    label: "What the of website do you need?",
    default: "web_development",
    list: [
      {
        label: "Web design",
        value: "web_design",
      },
      {
        label: "Web development",
        value: "web_development",
      },
      {
        label: "Logo Design",
        value: "logo_design",
      },
      "Other",
    ],
  },
}

5. fileInput (not tested)

This input acts like normal file input (you should notice the multiple, counter and show-size params) but there are some special rules at validation like mimes or dimensions

yourImage: {
  type: "fileInput",
  options: {
    hint: "write something here too!",
    "persistent-hint": true,
    label: "Select your image...",
    counter: true,
    multiple: true,
    "show-size": true,
  },
  validation: {
    required: true,
    mimes: "image/*",
  },
}

6. selects

deadline: {
  type: "selects",
  options: {
    label: "When do you need this project?",
    default: "web_development",
    items: [
      {
        text: "In 1 - 2 Months",
        value: "normal_client",
      },
      {
        text: "Now!",
        value: "now",
      },
      {
        text: "Yesterday!!!",
        value: "yesterday",
      },
    ],
  },
},

7.1 selectsAutocomplete (with Items)

It behaves like a normal select but you can search inside the list

autocomplete: {
  type: "selectsAutocomplete",
  options: {
    label: "Autocomplete from list",
    default: "web_development",
    items: [
      {
        text: "In 1 - 2 Months",
        value: "normal_client",
      },
      {
        text: "Now!",
        value: "now",
      },
      {
        text: "Yesterday!!!",
        value: "yesterday",
      },
    ],
  },
  validation: {
    required: true,
  },
},

7.2 selectsAutocomplete (fetch Items from API - Once)

You should notice that this input doesnt have any Items property but it has an apiUrl. It will lazy load the Items. The response should be an array of objects like the given below { text: 'Foo', value: 'Bar' }

autocomplete: {
  type: "selectsAutocomplete",
  options: {
    label: "Autocomplete from api (fetch once)",
    default: "web_development",
    apiUrl: "https://api.publicapis.org/entries",
  },
  validation: {
    required: true,
  },
}

7.3 selectsAutocomplete (fetch Items from API - Live search)

You should notice the below properties

  • isSearchable: enables the type-search feature
  • liveSearch: enables the type-search feature
  • searchDelay: time in ms that will wait before send the request (usefull when user is typing)
autocomplete: {
  type: "selectsAutocomplete",
  options: {
    label: "Autocomplete from api (live fetching)",
    default: "web_development",
    apiUrl: "https://api.publicapis.org/entries",
    isSearchable: true,
    liveSearch: true,
    searchDelay: 300,
  },
  validation: {
    required: true,
  },
},

After the API Syntax, you should provide a submit property with a click function

submit: {
  title: "Send Message",
  color: "primary",
  click: (formData) => {
    this.updateFormResult(formData);
    fetch("http://localhost:8080/api/test", requestOptions)
      .then((response) => response.json())
      .then((data) => console.log(data))
      .catch((err) => console.log(err));

    Object.keys(formData).forEach((formDataKey) => {
      body.append(formDataKey, formData[formDataKey]);
    });
    alert("Form sent!");
  },
},

Basic example:

<template>
  <v-app>
    <v-main>
      <v-container> <Form :api="api" /> </v-container>
      <v-container>
        <pre>{{ formResult }}</pre>
      </v-container>
    </v-main>
  </v-app>
</template>

<script>
import Form from "@/components/form.vue";
export default {
  name: "App",

  components: { Form },

  methods: {
    updateFormResult(formData) {
      this.formResult = formData;
    },
  },

  data() {
    return {
      formResult: {},
      api: {
        rows: [
          {
            input: [
              {
                name: {
                  options: {
                    label: "First name",
                    hint: "Είσαι ο;",
                    "persistent-hint": true,
                  },
                  validation: {
                    required: true,
                  },
                },
                email: {
                  options: {
                    default: "support@ianic.gr",
                    label: "Email",
                    prependIcon: "mdi-camera",
                  },
                  validation: {
                    required: true,
                    min: 4,
                    max: 24,
                    type: "email",
                    _callback: () => {
                      return {
                        validate: (value) => {
                          console.log(value);
                          return true;
                        },
                        message: "{_field_} is not valid(?)",
                      };
                    },
                  },
                  tooltip: {
                    position: "bottom",
                    text: "lorem ipsun",
                  },
                },
                _responsive: {
                  sm: 12,
                  md: 6,
                  lg: 4,
                },
              },
              {
                surname: {
                  options: {
                    readonly: true,
                    label: "Last name",
                  },
                },
                phone: {
                  options: {
                    loading: true,
                    label: "Phone",
                    hint: "Write 'hello' to proceed",
                    "persistent-hint": true,
                  },
                  validation: {
                    required: true,
                    is: "hello",
                  },
                  tooltip: {
                    text: "lorem ipsun",
                  },
                },
                _responsive: {
                  sm: 12,
                  md: 6,
                  lg: 4,
                },
              },
            ],
          },
          {
            input: [
              {
                deadline: {
                  type: "selects",
                  options: {
                    label: "When do you need this project?",
                    default: "web_development",
                    items: [
                      {
                        text: "In 1 - 2 Months",
                        value: "normal_client",
                      },
                      {
                        text: "Now!",
                        value: "now",
                      },
                      {
                        text: "Yesterday!!!",
                        value: "yesterday",
                      },
                    ],
                  },
                },
              },
              {
                autocomplete: {
                  type: "selectsAutocomplete",

                  options: {
                    label: "Autocomplete from list",
                    default: "web_development",
                    apiUrl: "https://api.publicapis.org/entries",
                    items: [
                      {
                        text: "In 1 - 2 Months",
                        value: "normal_client",
                      },
                      {
                        text: "Now!",
                        value: "now",
                      },
                      {
                        text: "Yesterday!!!",
                        value: "yesterday",
                      },
                    ],
                  },
                  validation: {
                    required: true,
                  },
                },
              },
            ],
          },
          {
            input: [
              {
                autocomplete: {
                  type: "selectsAutocomplete",

                  options: {
                    label: "Autocomplete from api (fetch once)",
                    default: "web_development",
                    apiUrl: "https://api.publicapis.org/entries",
                  },
                  validation: {
                    required: true,
                  },
                },
              },
              {
                autocomplete: {
                  type: "selectsAutocomplete",

                  options: {
                    label: "Autocomplete from api (live fetching)",
                    default: "web_development",
                    apiUrl: "https://api.publicapis.org/entries",
                    isSearchable: true,
                    liveSearch: true,
                    searchDelay: 300,
                  },
                  validation: {
                    required: true,
                  },
                },
              },
            ],
          },
          {
            input: [
              {
                website: {
                  responsive: {},
                  type: "radioButtons",
                  options: {
                    label: "What the of website do you need?",
                    default: "web_development",
                    list: [
                      {
                        label: "Web design",
                        value: "web_design",
                      },
                      {
                        label: "Web development",
                        value: "web_development",
                      },
                      {
                        label: "Logo Design",
                        value: "logo_design",
                      },
                      "Other",
                    ],
                  },
                },
              },
            ],
          },
          {
            input: [
              {
                message: {
                  type: "textArea",
                  options: {
                    label: "Write your message...",
                    hint: "Write something dude!",
                    "persistent-hint": true,
                  },
                  validation: {
                    min: 3,
                    max: 20,
                  },
                },
              },
            ],
          },
          {
            input: [
              {
                terms: {
                  type: "checkBoxes",
                  options: {
                    label: "Terms & conditions",
                    default: "steal",
                    hint: "write something here too!",
                    "persistent-hint": true,
                    items: [
                      {
                        label: "I aggree to steal all my data!",
                        value: "steal",
                      },
                    ],
                  },
                  validation: {
                    required: true,
                  },
                },
              },
            ],
          },
          {
            input: [
              {
                yourImage: {
                  type: "fileInput",
                  options: {
                    hint: "write something here too!",
                    "persistent-hint": true,
                    label: "Select your image...",
                    counter: true,
                    multiple: true,
                    "show-size": true,
                  },
                  validation: {
                    required: true,
                    mimes: "image/*",
                  },
                },
              },
            ],
          },
        ],
        submit: {
          title: "Send Message",
          color: "primary",
          click: (formData) => {
            this.updateFormResult(formData);

            /**
             * Post in general
             */

            const requestOptions = {
              method: "POST",
              headers: { "Content-Type": "multipart/form-data" },
              body: { ...formData },
            };

            fetch("http://localhost:8080/api/test", requestOptions)
              .then((response) => response.json())
              .then((data) => console.log("here", data))
              .catch((err) => console.log(err));

            /**
             *  Post the form with multipart/form-data or application/x-www-form-urlencoded;charset=UTF-8'
             *

            let body = new FormData();

            Object.keys(formData).forEach((formDataKey) => {
              body.append(formDataKey, formData[formDataKey]);
            });

            const requestOptions = {
              method: "POST",
              headers: { "Content-Type": "multipart/form-data" },
              body,
            };


            fetch("http://localhost:8080/api/test", requestOptions)
              .then((response) => response.json())
              .then((data) => console.log("here", data))
              .catch((err) => console.log(err));
            alert("Form sent!");
          },
          */

            alert("Form sent!");
          },
        },
      },
      value: "",
    };
  },
};
</script>
0.17.6

20 days ago

0.17.3

1 month ago

0.17.4

1 month ago

0.17.5

1 month ago

0.17.2

2 months ago

0.17.1

2 months ago

0.17.0

3 months ago

0.16.19

3 months ago

0.16.18

3 months ago

0.16.17

3 months ago

0.16.16

3 months ago

0.16.10

8 months ago

0.16.11

7 months ago

0.16.14

7 months ago

0.16.15

6 months ago

0.16.12

7 months ago

0.16.13

7 months ago

0.16.6

10 months ago

0.16.7

10 months ago

0.16.8

10 months ago

0.16.9

8 months ago

0.15.4

11 months ago

0.15.0

11 months ago

0.15.1

11 months ago

0.15.2

11 months ago

0.15.3

11 months ago

0.16.3

11 months ago

0.16.4

11 months ago

0.16.5

11 months ago

0.14.1

12 months ago

0.16.0

11 months ago

0.14.2

12 months ago

0.16.1

11 months ago

0.16.2

11 months ago

0.13.0

1 year ago

0.13.1

1 year ago

0.13.2

1 year ago

0.13.3

1 year ago

0.12.0

1 year ago

0.12.1

1 year ago

0.14.0

1 year ago

0.11.15

1 year ago

0.11.12

1 year ago

0.11.13

1 year ago

0.11.14

1 year ago

0.11.8

1 year ago

0.11.9

1 year ago

0.11.4

1 year ago

0.11.5

1 year ago

0.11.6

1 year ago

0.11.7

1 year ago

0.11.10

1 year ago

0.11.11

1 year ago

0.7.5

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.11.2

1 year ago

0.9.0

1 year ago

0.8.1

1 year ago

0.7.2

1 year ago

0.5.4

1 year ago

0.8.0

1 year ago

0.7.1

1 year ago

0.9.2

1 year ago

0.8.3

1 year ago

0.7.4

1 year ago

0.9.1

1 year ago

0.8.2

1 year ago

0.7.3

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.3

2 years ago

0.5.2

2 years ago

0.3.0

2 years ago

0.5.0

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.2.5

2 years ago

0.5.1

2 years ago

0.3.3

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.1.0

2 years ago