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

Slider - Submitted Form Value is not correct #228

Closed
petewins opened this issue Mar 17, 2024 · 5 comments
Closed

Slider - Submitted Form Value is not correct #228

petewins opened this issue Mar 17, 2024 · 5 comments

Comments

@petewins
Copy link

petewins commented Mar 17, 2024

Describe the bug
Upon form submit, the form action's value of the Slider is not correct.

To Reproduce

  1. Create a Slider with step={.01} inside <form action={onAction}> element
  2. Drag slider and submit the form
  3. TheformAction value returned from the form's action is not correct. The value does the match the Slider's value.
<form
    action={(formData) => {
      console.log(Array.from(formData.entries()));
      // output: ['reshaped-slider', '50'], 
     //  expected output: ['reshaped-slider', '50.25']
    }} 
 >
   <Slider name="reshaped-slider" defaultValue={50.25} step={0.01} />
   <Button type="submit">Submit</Button>
</form>

CodeSandBox
https://codesandbox.io/p/devbox/reshaped-slider-decimal-vwtxvc?file=%2Fapp%2Fpage.tsx%3A11%2C26

Expected behavior
Expect the value of Slider to match the sliders value (does not work for uncontrolled as well).
On submit of native HTML slider it shows the decimal values

Proposed solution

I believe the fix is to pass the step attribute to the native input element. Currently the value is correct in the DOM element but is missing the step attribute so the browser is rounding to the default step.

image image

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/range#step

Environment (please complete the following information):

  • Chrome LTS
  • Mac

Additional Context:
In addition, when step={0.01} and User is dragging the slider, value can sometimes show a long number
image

@petewins petewins changed the title Slider - support for decimals Slider - Submitted Form Value is not correct Mar 17, 2024
@blvdmitry blvdmitry moved this to 2.11.0 in Reshaped Mar 22, 2024
@blvdmitry blvdmitry moved this from 2.11.0 to In Progress in Reshaped Mar 22, 2024
@blvdmitry
Copy link
Contributor

Finally got to this issue, thanks for reporting.
I don't think applying step to the input will solve the problem in this case (even though it's a good improvement) since it's more about the issues of how Javascript handles float numbers. In case you're looking for an immediate fix (I understand that a week has passed 😅), you can also work with values multiplied by 100 and divide them back when submitting the form or on the back-end side. That's also why you would see libraries like stripe-js always working with integers instead of floats, so their representation of 1usd is a value of 100.

I will try to fix it internally meanwhile by detecting the precision passed in the step and handling the step calculations in a different way

@petewins
Copy link
Author

Thanks for looking into this and providing alternative, I agree with you on the javascript and its quirks with float. Though I'd prefer using using ints, I'd also expect it to behave somewhat similar to the native input range element so in this case, i'd expect 30.00 to be 30

@blvdmitry
Copy link
Contributor

Writing tests and releasing a patch early this week :)

Area.mp4

@blvdmitry blvdmitry moved this from In Progress to Waiting for the release in Reshaped Mar 24, 2024
@blvdmitry
Copy link
Contributor

You can try it out in 2.10.8 🚀

@petewins
Copy link
Author

Thank you @blvdmitry, confirmed is working as expected in 2.10.8 🔥

@blvdmitry blvdmitry moved this from Waiting for the release to Recently completed in Reshaped Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Recently completed
Development

No branches or pull requests

2 participants