Giter VIP home page Giter VIP logo

vuetify-mask's Introduction

vuetify-mask

Project

vuetify-mask is a component for working with some of the main types of masks in the v-text-field.

Links

See Demo here.
GitHub.
npm.

Dependencies

  • vuejs
  • vuetify ($ npm install vuetify --save)
  • moment ($ npm install moment --save)
  • material design icon ($ npm install @mdi/font -D --save)

Install

$ npm install vuetify-mask --save

Register

1- Create a file src/plugins/vuetify-mask.js with:

import Vue from "vue";  
import VuetifyMask from "vuetify-mask";  
Vue.use(VuetifyMask);  
export default VuetifyMask;

2- Add in src/mains.js file:

import "./plugins/vuetify-mask.js";

Properties (v-bind:properties)

You can add any v-text-field property
v-text-field properties.

Properties that have hyphens (single-line, background-color...) should be changed as follows:

v-bind:properties="{  
    singleLine: true,  
    backgroundColor: 'red'  
}"  

or

v-bind:properties="{  
    'single-line': true,  
    'background-color': 'red'  
}"  

Options (v-bind:options)

Option Component Default Description
inputMask Money, Percent, Integer, DateTime, SimpleMask mask that will be applied in the v-text-field
outputMask Money, Percent, Integer, SimpleMask, CPF, CNPJ, CEP mask that will be applied in the v-model
empty Money, Percent, Integer, DateTime, SimpleMask, CPF, CNPJ, CEP, DotNumber "" Value in v-model when v-text-field is empty. Can be null, "" or other
applyAfter Integer, SimpleMask, CPF, CNPJ, CEP, DotNumber The value is masked only after all typing
alphanumeric SimpleMask false
lowerCase SimpleMask false
acceptFile FileBase64 image/* Sets the file type to convert to base64

Events

Event value Description
blur Event Emitted when the input is blurred
change any Emitted when the input is changed by user interaction
click MouseEvent Emitted when input is clicked
focus Event Emitted when component is focused
keydown KeyboardEvent Emitted when any key is pressed
mousedown MouseEvent Emitted when click is pressed
mouseup MouseEvent Event mouseup

How to use

- Money (v-text-field-money)

<template>
  <div>
    <v-text-field-money
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        prefix: 'R$',
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: ' ',
      }"
      v-bind:options="{
        locale: 'pt-BR',
        length: 11,
        precision: 6,
        empty: null,
      }"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "123456789.00",    // 123456789.00 or "123456789.00" or "" or null
    label: "Money",
    disabled: false,
  }),
};
</script>

- Percent (v-text-field-percent)

<template>
  <div>
    <v-text-field-percent
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        suffix: '%',
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        locale: 'pt-BR',
        length: 3,
        precision: 2,
        empty: null,
      }"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "12.34",        // 12.34 or "12.34" or "" or null
    label: "Percent",
    focus: false,
  }),
};
</script>

- Integer (v-text-field-integer)

<template>
  <div>
    <v-text-field-integer
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        inputMask: '#########',
        outputMask: '#########',
        empty: null,
        applyAfter: false,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "123456789", // 123456789 or "123456789" or "" or null
    label: "Integer",
    focus: false,
  }),
};
</script>

- DateTime (v-text-field-datetime)

  works in milliseconds

<template>
  <div>
    <v-text-field-datetime
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: 'YYYY-MM-DD HH:mm:ss',
        'prepend-icon': 'mdi-calendar',
      }"
      v-bind:options="{
        inputMask: 'YYYY-MM-DD HH:mm:ss',
        empty: null,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "1595386800000",    // Milliseconds
    label: "DateTime",
    focus: false,
  }),
};
</script>

- DateTimePicker (v-text-field-datetimepicker)

  works in milliseconds

<template>
  <div>
    <v-text-field-datetimepicker
      v-model="value"
      label="Date Time"
      v-bind:properties="{
        backgroundColor: '#EEE9E9',
        clearable: false,
        outlined: true,
        prependIcon: 'mdi-calendar',
        appendIcon: 'mdi-av-timer',
      }"
      v-bind:options="{
        tabDateTitle: 'Date',
        tabTimeTitle: 'Time',
        tabBackgroundColor: 'green',
        locale: 'en-US',
        format: 'YYYY-MM-DD',
        closeOnDateClick: false,
        useSeconds: false,
      }"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: 1558220700000,
  }),
};
</script>

- Credit Card (v-text-field-simplemask)

<template>
  <div>
    <v-text-field-simplemask
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        prefix: '',
        suffix: '',
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        inputMask: '#### #### #### ####',
        outputMask: '################',
        empty: null,
        applyAfter: false,
        alphanumeric: true,
        lowerCase: false,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "1234432112344321",
    label: "Credit Card",
    focus: false,
  }),
};
</script>

- Phone Number (v-text-field-simplemask)

<template>
  <div>
    <v-text-field-simplemask
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        prefix: '',
        suffix: '',
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        inputMask: '(##) #####-####',
        outputMask: '###########',
        empty: null,
        applyAfter: false,
        alphanumeric: true,
        lowerCase: false,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>

<script>
export default {
  data: () => ({
    value: "99999999999",
    label: "Phone Number",
    focus: false,
  }),
};
</script>

- Simple Mask (v-text-field-simplemask)

  You can create your masks.

<template>
  <div>
    <v-text-field-simplemask
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        prefix: '',
        suffix: '',
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        inputMask: '##-####-####-###',
        outputMask: '##-####-####-###',
        empty: null,
        applyAfter: false,
        alphanumeric: true,
        lowerCase: false,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "23-A568-B953-356", // "23-A568-B953-356" or "" or null
    label: "Simple Mask",
    focus: false,
  }),
};
</script>

- Files (v-text-field-filebase64)

 Convert files to base 64.

<template>
  <div>
    <v-text-field-filebase64
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        outlined: true,
        placeholder: ' ',
        appendIcon:'mdi-message-image-outline',
      }"
      v-bind:options="{
        acceptFile:'image/*',
      }"
      v-on:fileName="fileName = $event"
    />
  </div>
</template>

<script>
export default {
  data: () => ({
    value:"",
    fileName: "",
    label: "Select Image",
  }),
};
</script>

Other acceptFile options:
acceptFile:'image/*'
acceptFile:'application/pdf'
acceptFile:'image/jpeg,image/gif,image/png,application/pdf'
acceptFile:'image/jpeg,image/gif,image/png,application/pdf,image/x-eps'

- DotNumber (v-text-field-dotnumber)

 Accept only dot and numbers.

<template>
  <div>
    <v-text-field-dotnumber
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        readonly: false,
        disabled: false,
        outlined: false,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        length: 20,
        empty: null,
        applyAfter: false,
      }"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "1.23.456.789", // "" or null
    label: "Only Dot and Number",
  }),
};
</script>

- CPF (v-text-field-cpf)

 brazilian mask

<template>
  <div>
    <v-text-field-cpf
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        readonly: false,
        disabled: false,
        outlined: true,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        outputMask: '###########',
        empty: null,
        applyAfter: true,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "97702036028", // 97702036028 or "97702036028" or "" or null
    label: "CPF (Brazilian mask)",
    focus: false,
  }),
};
</script>

- CNPJ (v-text-field-cnpj)

 brazilian mask

<template>
  <div>
    <v-text-field-cnpj
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        disabled: false,
        outlined: true,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        outputMask: '##############',
        empty: null,
        applyAfter: true,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "50703512000192", // 50703512000192 or "50703512000192" or "" or null
    label: "CNPJ (Brazilian mask)",
    focus: false,
  }),
};
</script>

- CEP (v-text-field-cep)

 brazilian mask

<template>
  <div>
    <v-text-field-cep
      v-model="value"
      v-bind:label="label"
      v-bind:properties="{
        disabled: false,
        outlined: true,
        clearable: true,
        placeholder: '',
      }"
      v-bind:options="{
        outputMask: '########',
        empty: null,
        applyAfter: true,
      }"
      v-bind:focus="focus"
      v-on:focus="focus = false"
    />
  </div>
</template>

<script>
export default {
  data: () => ({
    value: "82515260", // 82515260 or "82515260" or "" or null
    label: "CEP (Brazilian mask)",
    focus: false,
  }),
};
</script>

vuetify-mask's People

Contributors

hebertalvesds avatar juareznasato avatar sdlins 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

Watchers

 avatar

vuetify-mask's Issues

using :value instead of v-model?

Great library! I'm trying to prevent my v-text-field from updating the model/input until the @change event actually fires.

  • v-model.lazy is preferred for this, but it is not supported by v-text-field
  • the next best option is to use a combination of :value & :change events but when I do this the masking doesn't work

Is v-model required for masking to work? If it is and :value can't be used, do you have any ideas how to accomplish what I'm looking to do?

v-mask in data-table

Hi, how to use v-mask in template tag data-table

<template v-slot:[item.usu_cpf]="{ item }"> <td> <span>{{ item.usu_cpf }}</span> </td> </template>

attribute not passing down through deepest child.

according to v-text-field API, any attribute not listing in API doc will be passed down through the child tag which, in this case, is the input tag. So, using <v-text-field inputmode="numeric"/> will results in

... some other components...
<input inputmode="numeric" type="text" ... >
... closing tags

however the behavior of v-text-field-simplemask just passing down inputmode to v-text-field tag which is not quite usable in my case.

How to set required property?

Hey, what's the best way to set rules in cnpj field?

<v-text-field-cnpj
    v-model="form.cnpj"
    label="CNPJ"
    class="ml-md-2"
    v-bind:properties='{
        rules: "[v => !!v || 'CNPJ é obrigatório']" // not working
        rules: "[rules.cnpj.required]" // not working
    }'
    v-bind:options="{
        outputMask: '##############',
    }"
></v-text-field-cnpj>

:error-messages directive doesn't work

Hi!
I use v-text-field-simplemask. I add a directive
:error-messages="check_val"
but when the function check_val returns some value (example, "Must be filled"), the text box does not display an error and does not turn red.

Números negativos e decimais

Juarez,
Tua lib é bem bacana, para mim falta poucos detalhes para ser excelente, falta usar diretamente as Rules ( pelo que vi já tem um PR que resolve isto) e aceitar números negativos, pois em aplicações financeiras e outras é bem usada.

Espero que logo resolva estes pequenos detalhes.

Forte abraço ai.

Possible to make precision mask optional on v-text-field-money

Great package!

One nice-to-have feature would be if you could dynamically allow the precision when a user types a dot. I think that would allow a better UX. Lots of times the decimals aren't needed and can be annoying to type 00 (or however many you set) before getting to a whole number. Is this possible?

v-text-field-integer on mobile

v-text-field-integer on mobile does not prevent entering comma and periods on mobile while on desktop only numbers are allowed

Using slots

Is there any way that I can use the component slots?

So far I've tried this:

<template>
  <div>
    <v-text-field-simplemask
      v-model="value"
      :label="computedLabel"
      :properties="computedProperties"
      :options="computedOptions"
      :focus="focus"
      @focus="focus = false"
    >
      <template #append>
        <v-btn icon>
          <v-icon>mdi-account</v-icon>
        </v-btn>
      </template>
    </v-text-field-simplemask>
  </div>
</template>

But no success with any of the available slot options in the v-text-field documentation.

Support focus through tabindex

When control is set on focus through tabindex, the entire control is highlighted. I think the correct behavior is to have the focus set such that its ready to accept user keyboard input.

image

<v-text-field-simplemask
label="Date of birth"
v-model="properties.dob"
v-bind:properties="{
readonly: false,
disabled: false,
outlined: true,
clearable: true,
placeholder: 'DD-MM-YYYY',
}"
v-bind:options="{
inputMask: '##-##-####',
outputMask: '##-##-####',
empty: null,
}"
v-on:keyup="keyup"
tabindex="39"

Unexpected 0 in mask

Good day,

I don't know why, but I got unexpected 0 in my phone number field

Screenshot

I don't have any 0 in my mask:

+49(####)###-#### - it's mask for screenshot

But I got +49(0 in field

<template>
  <v-card flat width="60%">
    <v-card-title class="font-weight-bold">Billing information</v-card-title>
    <v-card-subtitle class="text-body-2">This information must be kept current to ensure your account continues to be active</v-card-subtitle>
    <v-form>
      <v-card-text>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field type="text" placeholder="First name" v-model="settings['first_name']"></v-text-field>
          </v-col>
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field type="text" placeholder="Last Name" v-model="settings['last_name']"></v-text-field>
          </v-col>
        </v-row>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="12">
            <v-text-field placeholder="Company Name"></v-text-field>
          </v-col>
        </v-row>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="6">
            <v-select placeholder="Country"
                      :items="settings.countries"
                      v-model="settings.address.country"
                      item-text="name"
                      item-value="code"></v-select>
          </v-col>
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="State/Region"></v-text-field>
          </v-col>
        </v-row>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="City"></v-text-field>
          </v-col>
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="Postal Code"></v-text-field>
          </v-col>
        </v-row>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="Street Address"></v-text-field>
          </v-col>
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="Suite/Unit"></v-text-field>
          </v-col>
        </v-row>
        <v-row class="pa-0">
          <v-col class="py-0" cols="12" lg="6">
            <masked-input-field-component
                v-model="phoneNumber"
                label="Phone Number"
                placeholder="Phone number"
                v-bind:properties="{
                  readonly: false,
                  disabled: false,
                  outlined: false,
                  clearable: true,
                }"
                v-bind:options="{
                  humanMask: phoneMask,
                  machineMask: '###########',
                  empty: null,
                  applyAfter: false,
                  alphanumeric: true,
                  lowerCase: false,
                }"
            />
          </v-col>
          <v-col class="py-0" cols="12" lg="6">
            <v-text-field placeholder="VAT ID"></v-text-field>
          </v-col>
        </v-row>
      </v-card-text>
      <v-card-actions class="px-4">
        <v-btn type="submit" color="primary">Save Settings</v-btn>
      </v-card-actions>
    </v-form>
  </v-card>
</template>

<script>
import countries from "@/static/countries.json"
import MaskedInputFieldComponent from "../MaskedInputFieldComponent";

export default {
  name: "SettingsTabContent",
  components: {MaskedInputFieldComponent},
  props: {
    billingData: Object
  },
  data () {
    return {
      countries: countries
    }
  },
  mounted() {
  },
  methods: {
  },
  computed: {
    settings () {
      return this.billingData
    },
    phoneMask () {
      // eslint-disable-next-line no-prototype-builtins
      if (this.settings.address.country && this.countries['phoneMask'].hasOwnProperty(this.settings.address.country) ) {
        return this.countries['phoneMask'][this.settings.address.country]
      } else {
        return this.countries['phoneMask']['DE']
      }
    },
    phoneNumber: {
      get () {
        return this.settings.phone_number
      },
      set (value) {
        this.settings.phone_number = value
      }
    }
  }
}
</script>

<style scoped>

</style>


I use dynamic input mask based on country code that I choose in select field

What am I doing wrong?

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.