GithubHelp home page GithubHelp logo

Property 'Values' in conflict about react-range HOT 3 OPEN

tajo avatar tajo commented on May 22, 2024
Property 'Values' in conflict

from react-range.

Comments (3)

tajo avatar tajo commented on May 22, 2024

Something's broken about multiple thumbs and decimals.

from react-range.

raptor3790 avatar raptor3790 commented on May 22, 2024

Any news on this issue?

from react-range.

swashata avatar swashata commented on May 22, 2024

I can say that this is due to floating point arithmetic in JavaScript.

Let's say, my config is like this

{
	min: 97,
	max: 108,
	step: 0.1,
	values: [98.6],
}

During the calculation at

values.forEach((value) => {
if (!isStepDivisible(min, value, step)) {
console.warn(
'The `values` property is in conflict with the current `step`, `min`, and `max` properties. Please provide values that are accessible using the min, max, and step values.'
);
}
});

It becomes

react-range/src/utils.ts

Lines 17 to 20 in 57345c3

export function isStepDivisible(min: number, max: number, step: number): boolean {
const res = (max - min) / step;
return parseInt(res.toString(), 10) === res;
}

const res = (max - min) / step;
// (98.6 - 97) / 0.1
// 15.999999999999943
return parseInt(res.toString(), 10) === res;
// 15 === 15.999999999999943
// false

One possible solution is to take a precision prop from userland and multiply the numbers to convert floats to integer.

Like

const multiplier = 10 ** precision;
const normalizedMax = Math.round(max * multiplier);
const normalizedMin = Math.round(min * multiplier);
const normalizedStep = Math.round(step * multiplier);
const res = (max - min) / step;
// (986 - 970) / 1
// 16
return parseInt(res.toString(), 10) === res;
// 16 === 16
// true

I have forked this library and made such changes. I cannot think of any other method where I can reliably extract number of decimal points from given values.

from react-range.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.