Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V1] QInput type="number" returns string instead of number for model #3924

Closed
topvis opened this issue Apr 18, 2019 · 15 comments
Closed

[V1] QInput type="number" returns string instead of number for model #3924

topvis opened this issue Apr 18, 2019 · 15 comments

Comments

@topvis
Copy link

topvis commented Apr 18, 2019

codepen link

quasar: 1.0.0-beta.17,
@quasar/app: 1.0.0-beta.18,
Browser: Chrome
OS: Windows 7

QInput with type="number" returns string instead of number. Type in any number or change the value of the QInput by clicking the up/down button will return a string value.

There was a fix #2605 for V0.x. But seems V1 hasn't got this issue fixed. Or maybe this is intended by design?

@rstoenescu
Copy link
Member

Thanks for reporting this. Will get it fixed.

@rstoenescu
Copy link
Member

Prior to a thorough investigation, I recommend using:
<q-input type="number" v-model.number="model" ...

@topvis
Copy link
Author

topvis commented Apr 18, 2019

Oh, I didn't know that. I'll try it. Thanks.

@rstoenescu
Copy link
Member

This is the recommended usage going forward.
Docs will be updated along with the release of beta.18

@topvis
Copy link
Author

topvis commented Apr 19, 2019

What would be the best way in the case of one-way binding? E.g.,
<q-input type="number" :value="data" @input="updateData"/>
Should the type cast be done in the event callback function "updateData"?

@rstoenescu
Copy link
Member

@topvis That's not one-way binding. That's the rough equivalent of v-model.
If you use this equivalent, then yes, you need to do the cast in your updateData. But it's not just a matter of casting string to number.. But you can use v-model.number and still listen to the @input event to trigger your specific method.

@topvis
Copy link
Author

topvis commented Apr 19, 2019

@rstoenescu I had to use the @input event because the data is actually in a Vuex store and I need to call a Vuex mutation function to update the data. I assume this is a very common way when using Vuex?
I would propose to add a line or two in the Docs to explain that user needs to cast the string to number in the @input callback function.

@rstoenescu
Copy link
Member

Use a computed property with a setter and getter when dealing with a Vuex store.

// ...
computed: {
  message: {
    get () {
      return this.$store.state.obj.message
    },
    set (value) {
      this.$store.commit('updateMessage', value)
    }
  }
}

@lopugit
Copy link

lopugit commented Apr 22, 2019

@rstoenescu I had to use the @input event because the data is actually in a Vuex store and I need to call a Vuex mutation function to update the data. I assume this is a very common way when using Vuex?
I would propose to add a line or two in the Docs to explain that user needs to cast the string to number in the @input callback function.

yeah that's how I do it, :value + @input and @rstoenescu computed props aint an option because it's way to slow having to write boiler plate for every vuex store value you want to use, the :value + @input hack has been a real lifesaver

@rstoenescu
Copy link
Member

@lopugit this is the Vuex way. If you use the equivalent form of v-model (:value + @input) then you miss on v-model's handling of the number..

@lopugit
Copy link

lopugit commented Apr 22, 2019

@lopugit this is the Vuex way. If you use the equivalent form of v-model (:value + @input) then you miss on v-model's handling of the number..

all g, how do they handle numbers? just a parseInt or more sophisticated?

@rstoenescu
Copy link
Member

@lopugit very sophisticated. wish it were that easy to parseInt() only. Example: imagine parseInt() when user has typed "0." --> NaN. Or wanting to type (0.10004) --> typing "0.10" will turn this into "0.1" (you'll never be able to type full 0.10004). And the examples can go on and on and on and on.

@lopugit
Copy link

lopugit commented Apr 22, 2019

@lopugit very sophisticated. wish it were that easy to parseInt() only. Example: imagine parseInt() when user has typed "0." --> NaN. Or wanting to type (0.10004) --> typing "0.10" will turn this into "0.1" (you'll never be able to type full 0.10004). And the examples can go on and on and on and on.

Ahh very interesting, can always copy the handler code :P

@rstoenescu
Copy link
Member

Just thought you should be aware of what it does :)

@lopugit
Copy link

lopugit commented Apr 22, 2019

Just thought you should be aware of what it does :)

I know I'm very thankful, I've been wondering <3

Also v1 is sick ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants