maska icon indicating copy to clipboard operation
maska copied to clipboard

Mask is not applied on a async update

Open michelveloso opened this issue 2 years ago • 3 comments

Hello, when I update a model via promise or setTimeout for example, the mask is not applied

<Input
    type="text"
    name="cnpj"
    class="form-control form-control-lg form-control-solid"
    placeholder="CNPJ"
    v-model="agentAccountDetailForm.cnpj"
    v-maska="'##.###.###/####-##'"
  />
const agentAccountDetailForm = ref<IAgentAccountDetailForm>({
      cnpj: "",
    });

It's Working using this way:

agentAccountDetailForm.value.cnpj = '11111111';

Not Working using this way:

setTimeout(() => {
      agentAccountDetailForm.value.cnpj = '11111111';
    }, 20)

Not working using this way:

watchEffect(()  => {
      const company = currentAgentAccountDetail.value.company;
      agentAccountDetailForm.value.cnpj = company?.cnpj;
    });

is it possible to force the update of an element's mask?

michelveloso avatar Jan 25 '22 14:01 michelveloso

@michelveloso did you find a solution to this?

lunt1k avatar May 05 '22 21:05 lunt1k

I wonder if it's because the 'input' event isn't triggered in this case? https://github.com/beholdr/maska/blob/master/src/maska.js#L42

in any case this answer might be a workaround for now

builder555 avatar Jul 08 '22 00:07 builder555

This is my solution in VUE composition API.

template

<input v-model="account.cpfCnpj" v-maska="'##.###.###/####-##'" class="form-control" id="cnpj" type="text" />

script

import { mask } from 'maska';
import { ref, watch } from 'vue';

const account = ref({
  name: '',
  phone: '',
  mobilePhone: '',
  address: '',
  addressNumber: '',
  complement: '',
  province: '',
  postalCode: '',
  cpfCnpj: '',
});

watch(() => account.value.cpfCnpj, (newValue) => {
  account.value.cpfCnpj = mask(newValue, '##.###.###/####-##');
});

I followed tip @beholdr in post mentioned by @builder555

Used Watch in account.cpfCnpj and solved!

oraculum avatar Aug 18 '22 20:08 oraculum