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

@odyzeo/form-item

v2.8.0

Published

Odyzeo form item component with Vue

Downloads

99

Readme

@odyzeo/form-item

Simple input and textarea Vue.js component.

Demo

Installation

npm

npm install @odyzeo/form-item

Import and register component:

import FormItemPlugin, { FormErrors, FormItem, TextareaAutoresize }  from '@odyzeo/form-item';

Vue.use(FormItemPlugin, options);
// Vue.component('FormItem', FormItem); // Installed w/ FormItemPlugin
Vue.component('FormErrors', FormErrors);
Vue.directive('textarea-autoresize', TextareaAutoresize);

Import styles or make your own:

import '@odyzeo/form-item/dist/form-item.css';

Usage

<template>
  <div>
      <form-item
        :input="simple"
        v-model="simple.value"
      ></form-item>
      
      <form-item
        :input="full"
        v-model="full.value"      
        :form-errors="formErrors.full"
        class="form-item--group"
        :bind-to-input="{ 'data-hj-whitelist': true }"
        :trans="toUpperCase"
        input-class="custom-class-for-input"
        group-name="form-item-form-test"
      >
        <template slot="prepend">
          <div class="form-item__readonly">Your</div>
        </template>
        <template slot="append">
          <div class="form-item__readonly">Please</div>
        </template>
      </form-item>

      <form-item
          :ref="textarea.name"
          v-model="textarea.value"
          v-textarea-autoresize:window="{ max: 200 }"
          :input="textarea"
          :trans="customTranslate"
      ></form-item>

      <form-errors
          :form-errors="formErrors"
      ></form-errors>
  </div>
</template>
<script>
import FormItem from '@odyzeo/form-item'

export default {
    name: 'App',
    components: {
        FormItem,
    },
    data() {
        return {
            simple: {
                label: 'Simple',
                value: '',
            },
            full: {
                type: 'email',
                name: 'email',
                required: true,
                readonly: false,
                placeholder: '[email protected]',
                accept: '', // Just for input type 'file'
                validators: [
                    {
                        validator: 'email',
                    },
                    {
                        validator: 'required',
                        message: 'Povinné',
                    },
                ],
                rows: 0, // Just for input type 'textarea'
                autocomplete: 'username email',
                label: 'E-mail',
                value: '',
            },
            requiredMessage: 'Povinné',
            formErrors: {
                email: ['E-mail is required'],
            },
            textarea: {
                name: 'textarea',
                type: 'textarea',
                label: 'Textarea',
                placeholder: 'Textarea placeholder',
                rows: 3,
                value: 'Some text just for textarea',
            },
        };
    },
    methods: {
        toUpperCase(key) {
            return key.toUpperCase();
        },
    },
};
</script>

Plugin options

| Property name | Type | Default value | Description | | -------------- | -------- | -------------- | -------------------------------------------------------- | | className | String | 'form-item' | Set component block className (set B in BEM) | | componentName| String | 'form-item' | Set component name | | trans | function | (key) => key | Enable translating or modifying labels and placeholders. |

Example using vui-i18n

import Vue from 'vue';
import VueI18n from 'vue-i18n';
import App from './App';
import FormItem from './plugin/FormItem';

Vue.use(VueI18n);

new Vue({
    i18n,
    render: h => h(App),
    created() {
        Vue.use(FormItem, {
            trans: this.$t,
        });
    }
}).$mount('#app');

Props

input {Object} - required

| Property name | Type | Default value | Description | | ------------- | ---- | ------------- | ----------- | | type | string | text | Supports 'textarea' and all html5 input types supports | | name | string | | Textarea or input `name` attribute | | `label` | string | `''` | Label name for input | | `required` | boolean | `false` | If value is required | | `disabled` | boolean | `false` | Set input to disabled state, validator would skip this field | | `readonly` | boolean | `false` | If field is read only | | `placeholder` | string | | Native placeholder attribute for input/textarea | | accept | string | | Which file types should be accepted if type is file | | validators | array | null | Array holding objects with validator {string} - name of validator and message {string} - error message text properties | | validatorEvent | string | none | Pick validator events which will be used for frontend validation. Choose from: onBlurThenOnInput: validate field on blur first, then on input periodically onBlur onInput| | rows | number, string | | Number of rows textarea should have | | `autocomplete` | string | `off` | HTML5 autocomplete attribute, check [docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) for more info and possible values | | `pattern` | string | | Pattern attribute specifies a regular expression that the element's value is checked against on form submission | | id | string | `` | Id for input or textarea and for respective label |

className {string = ''} - optional

Set component block className (set B in BEM), has priority over className option in plugin.

groupName {string = ''} - optional

Set if you need to target one or more form item components with global methods

value {string} - optional

This is the initial value of the form input/textarea. Use v-model for reactivity.

trans {Function} - optional

Custom function to translate or modify input placeholder and label.

formErrors {array} - optional

Array of errors to display. Will take priority before FE validator errors until value is changed.

Available validators

  • email
  • zip
  • tel : phone number
  • max : length of string - Use $0 in validator message to replace number.
  • min : length of string - Use $0 in validator message to replace number.
  • confirmed : confirmed passwords
  • regex : your custom regex
  • required : for custom required message

Custom validator

It is possible to use your own validator instead of predefined one.

The custom validator has to be a valid return function with value parameter. You can also make use of a custom message property as per predefined validators.

validators: {
    validator: value => value.indexOf(`joseph`) !== -1,
    message: 'My custom error message',
}

Custom validator with parameters

If your validator needs to provide parameters, you can simply create a higher order function that returns the actual validator, like in between builtin validator.

const myFunc = param => value => value.indexOf(`${param} joseph`) !== -1;

// ...

validators: {
    validator: myFunc('some text'),
    message: 'My custom error message',
}

bindToInput {Object} - optional

Used for generating custom attributes to input/textarea element.

inputClass {string} - optional

Used for adding custom class to input/textarea element.

Global methods

Methods called on $formItem object installed on main Vue instance

$formItem.validate(name)

  • Arguments:

    • {string} name Group name of one or multiple form items
  • Usage:

    Trigger validation of all form items corresponding to group name argument

$formItem.clear(name)

  • Arguments:

    • {string} name Group name of one or multiple form items
  • Usage:

    Clear inputs and errors on all form items corresponding to group name argument

$formItem.getErrors(name)

  • Arguments:

    • {string} name Group name of one or multiple form items
  • Returns:

    • Array of errors
  • Usage:

    Get all current FE errors of all form items corresponding to group name argument

$formItem.hasErrors(name)

  • Arguments:
    • {string} name Group name of one or multiple form items
  • Returns:
    • Boolean indicating whether group of items got errors

Events

Component emits these events:

  • @blur
  • @eager - Emits event on 'input' event (get value from event.currentTarget.value)
  • @focus
  • @input - Emits the value of the element (not working w/ suggestions)
  • @keydown
  • @keyup

Development

npm run serve