import Vue from "vue"

// 只能输入数字
const leaveNumOnly = Vue.directive('leaveNumOnly',function(el, binding, vnode){
  const element = el.getElementsByTagName('input')[0]
  let val = element.value
  element.addEventListener('blur', function(){
    vnode.data.model.callback(val.replace(/[^\d^\.]+/g,''))
  })
})

// 只能输入两位小数
const leaveTwoNumber  = Vue.directive('leaveTwoNumber',function(el, binding, vnode){
  const element = el.getElementsByTagName('input')[0]
  let val = vnode.data.model.value
  element.addEventListener('blur',function(){
    let num = val.replace(/[^\d^\.]+/g,'')
    if(num){
      vnode.data.model.callback(Number.parseFloat(num).toFixed(2))
    }else{
      vnode.data.model.callback('')
    }
  })
})
export default {
  leaveNumOnly,
  leaveTwoNumber
}
