RonaldJerez / vue-input-facade

A lightweight and dependency free input masking library created specific for Vue
https://ronaldjerez.github.io/vue-input-facade/latest/
183 stars 27 forks source link

Numeric masks do not work correctly with trailing zeros #22

Closed douglasg14b closed 4 years ago

douglasg14b commented 4 years ago

Describe the bug

Numeric masks do not work correctly when you can have a decimal point and you are filling the number with zeros, it erases and rewrites input that should not be changed.

Example : ['#.##', '##.##', '###.##', '####.##']

To Reproduce See above

Expected behavior See above

Screenshots N/A

Desktop

As you can see, typing in zeros does not have the same behavior as other numeric values, which is unexpected and seemingly incorrect behavior. This makes it practically impossible to actually write out numeric values that end in multiple zeros with a mask.

douglasg14b commented 4 years ago

Updated to v1.3.1 And the problem is actually worse.

Now zeros do not appear at all, in any fashion.

RonaldJerez commented 4 years ago

I just tried this on the demo page and its working as expected. Your bug report didn't follow the template and is missing key information for me to be able to reproduce it.

I tried this code in the demo page. in both chrome and safari.

let value = ''
let masked = false

<example label="Money">
  <input-facade :mask="['#.##', '##.##', '###.##', '####.##']" v-model="value" :masked="masked" />
</example>

<checkbox v-model="masked" />
<display :value="value" />
douglasg14b commented 4 years ago

More info:

Browser: Firefox Input Type: Vuetify v-text-field

Input markup:

    <v-text-field v-model.number="price" title="Price"
        class="elevate-input" label="Price"
        type="number" v-facade="['', '#','#.##', '##.##', '###.##', '####.##']">
    </v-text-field>    
douglasg14b commented 4 years ago

Maybe it has to do with v-model.number ?

douglasg14b commented 4 years ago

Managed to reproduce working vs non-working.

This occurs when the v-model binding is bound to a number, instead of a string.

So the question now is, how can this work when bound to a numeric value, or can it?

RonaldJerez commented 4 years ago

It shouldn't have a problem with numeric values. the code actually transforms any input to its string equivalent. However you have to realize that in javascript. 1.00 gets transformed to just "1" as an integer. So it really depends on what vuetify is doing to the value when transforming it to an int. If its floating integer then it shouldn't be a problem.

I did however just tried it. it seems to only be happening when using vuetify and in firefox. Vuetify in chrome doesnt have this problem. I'll put some debugging and see if I spot anything. But like you said I think this has to do with the number been transformed into a number and probably loosing the decimal place when transformed.

RonaldJerez commented 4 years ago

I tried again in the demo page without vuetify. just adding v-model.number, and it also has issues there. I then went to look at the vue's docs for v-model.number and it says its done using parseFloat. If you try parseFloat('1.00') on the browser console you'll notice that the browser removes the trailing 0's as 1.00 is the same as 1. So next time around the input will just be getting a 1 as input and loosing. So I dont think theres anything I can do to remediate that.

RonaldJerez commented 4 years ago

Closing as there's really nothing that can be done for this. I will put a comment in the docs to indicate that v-model.number does not work with this plugin as vue gets rid of the 0s when using number.