2

I am using Vue.js 2 with Element and I would like to make use of Cleave.js for masking.

I understand how to make a basic masker in Vue, using data and computed properties. However I don't want to redo all the good work done in Cleave.

I have also discovered vue-cleave. That seems like a good way of using Cleave with Vue. Although vue-cleave ads a standard input element to the page. I am using Element so I would need a way of using el-input.

This is a common problem with most of the Vue maskers, they seem to add a standard input element to the page.

So, my question is: what is the Vue way of integrating Cleave.js when using Element?

References:

Element: http://element.eleme.io

Cleave.js: https://github.com/nosir/cleave.js

Vue-Cleave: https://github.com/vue-bulma/cleave

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Daryn
  • 3,394
  • 5
  • 30
  • 41

1 Answers1

2

I found this article: https://vuejsdevelopers.com/2017/05/20/vue-js-safely-jquery-plugin/

Which explained how to use JQuery with Vue. I followed that pattern and integrated Vue with Cleave as follows:

SmartCleave.vue

<template>
    <input />
</template>

<script>
/* eslint-disable no-new */
import Cleave from 'cleave.js'

export default {
  mounted () {
    new Cleave(this.$el, {
      date: true,
      datePattern: ['d', 'm', 'Y']
    })

    this.$el.oninput = (e) => {
      console.log('oninput the field', e.target.value)
      this.$emit('oninput', e.target.value)
    }
  }
}
</script>

App.vue

<template>
  <div id="app">
    <smart-cleave @oninput="logIt"></smart-cleave>
    <div>{{date}}</div>
  </div>
</template>

<script>

/* eslint-disable no-new */
import Cleave from 'cleave.js'
import SmartCleave from './components/SmartCleave'

new Cleave('#plain-input', {
  date: true,
  datePattern: ['d', 'm', 'Y']
})

export default {
  name: 'app',
  components: {
    SmartCleave
  },
  data () {
    return {
      date: ''
    }
  },
  methods: {
    logIt (val) {
      console.log('cahnged', val)
      this.date = val
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* text-align: center; */
  color: #2c3e50;
  margin-top: 60px;
}
</style>
Daryn
  • 3,394
  • 5
  • 30
  • 41