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

UiSlider: Support for float step #443

Open
mradionov opened this issue Apr 13, 2019 · 1 comment
Open

UiSlider: Support for float step #443

mradionov opened this issue Apr 13, 2019 · 1 comment

Comments

@mradionov
Copy link

When I pass a float number to UiSlider step prop, the value is always rounded up to integer therefore "1" is the least possible step.

<ui-slider :step="0.5"></ui-slider>
<ui-slider :min="0" :max="1" :step="0.01"></ui-slider>

I think it happens because of a Math.round call in the dragUpdate method:

 this.setValue(Math.round(value)); // line 337

When I remove the Math.round and use integer step - everything seems good. But when I pass a float number, values become a bit messy, because they are not rounded in any way. With step = 0.5 I would expect to receive values like 0.05, 0.1, 0.15, 0.2, etc, but because there is no rounding for float values, I receive some random increments:

image

I usually see in other libraries that step is used to round the value to the nearest correct value, like this for example:

const remainder = value % step;
value -= remainder;
if (remainder > step / 2) {
  value += step;
}

but I've noticed in the docs that you use step for keyboard and when snapToSteps is on and it is only included in calculations when flags are on. I wonder if you have any ideas how to round the value without using step.

I was able to workaround it and wrap UiSlider in custom component which normalizes the values, but I think it would be a nice thing to support out of the box, because native HTML5 supports it too.

@hyvyys
Copy link
Contributor

hyvyys commented Sep 6, 2019

I found that a great and easy way to perform snapping floats to multiples of a fraction is the Decimal.js library and its toNearest method. I used it myself for my implementation of UiNumber. Maybe it would be overkill to add a dependency just to support floats in UiSlider, but this is just a pointer to the fact that there are feasible ways of dealing with this.

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

2 participants