maska icon indicating copy to clipboard operation
maska copied to clipboard

mask.completed works incorrect with optional tokens

Open maxmaxme opened this issue 1 year ago • 2 comments

Describe the bug

mask.completed returns false if the mask contains optional tokens

I inserted the optional token 0 and expect the method returns true

Steps to reproduce

const mask = new Mask({
    mask: "#-#0",
    tokens: {
      "0": { pattern: /[0-9]/, optional: true },
    },
})

mask.completed("12") // = false

Reproduction link

https://stackblitz.com/edit/js-maska-k9z3pf?file=index.js

maxmaxme avatar May 29 '23 07:05 maxmaxme

Yeah I'm facing the same issue trying to implement this:

 v-maska
 data-maska="(##)########0"
 data-maska-tokens="0:[0-9]:optional"

The completed event only fires with all the values inserted. Are we misunderstanding the optional usage @beholdr?

mateuswetah avatar Nov 14 '23 14:11 mateuswetah

Same happening here but with an array of masks. It only returns completed after filling the larger one.

Reproduction link:

https://stackblitz.com/edit/vue-maska-kwct9s?file=src%2FApp.vue

fsblemos avatar Apr 25 '24 15:04 fsblemos

I'm afraid it's too complicated logic to determine which token is optional. It's just checking mask length.

beholdr avatar Jun 02 '24 15:06 beholdr

@beholdr isn't this behavior different from the expected? Why not to consider completed as soon the first mask matches?

fsblemos avatar Jul 04 '24 17:07 fsblemos

Hmm, I'm not sure that the mask should be marked as completed when it still has some characters to input. That was the initial logic.

beholdr avatar Jul 04 '24 18:07 beholdr

Just to point out my use case, if I have the masks ['##', '####'], I would expect completed to be true only if it is informed 00 or 0000. If the value is 0 or 000 I expect it to be false.

I was considering the completed to be true when "informed value fullfills one of the masks", is that right?

fsblemos avatar Jul 04 '24 18:07 fsblemos

After further consideration, I think your logic is valid for dynamic masks. I'll try to implement this.

beholdr avatar Jul 07 '24 07:07 beholdr

@fsblemos implemented in 3.0.0-beta5, could you please test it?

beholdr avatar Jul 07 '24 14:07 beholdr

thank you @beholdr! just tested here and it works :)

https://stackblitz.com/edit/vue-maska-kwct9s?file=src%2FApp.vue

Just a question, in v2 I was able to track both completed and unmasked in the boundedObject. How can I do this with args and modifiers on v3? Maybe it worths to add an example of multiple args in the docs.

fsblemos avatar Jul 09 '24 14:07 fsblemos

With new directive syntax, in v3 you can use only one modifier, so if you need both completed and unmasked values you can use @maska event. Example here: https://stackblitz.com/edit/vue-maska-g52giw?file=src%2FApp.vue

beholdr avatar Jul 09 '24 14:07 beholdr

Got it, didn't know about this event. Maybe I'll simply use <input v-maska="options" @maska="e => boundObject = e.detail" /> then. Thank you

fsblemos avatar Jul 09 '24 14:07 fsblemos