Skip to content

Commit

Permalink
fix: use lower or upper bound when changing from same value
Browse files Browse the repository at this point in the history
  • Loading branch information
AOB committed Sep 26, 2023
1 parent d9f71fe commit 8a636aa
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/ReactSlider/ReactSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,21 @@ class ReactSlider extends React.Component {
return prepareOutValue(this.state.value);
}

getClosestIndex(pixelOffset) {
getClosestIndex(pixelOffset, nextValue) {
let minDist = Number.MAX_VALUE;
let closestIndex = -1;

const { value } = this.state;
const l = value.length;

if (new Set(value).size === 1) {
if (value[0] > nextValue) {
return 0;
}

return value.length - 1;
}

for (let i = 0; i < l; i += 1) {
const offset = this.calcOffset(value[i]);
const dist = Math.abs(pixelOffset - offset);
Expand Down Expand Up @@ -771,8 +779,8 @@ class ReactSlider extends React.Component {
// and calls `callback` with that thumb's index.
forceValueFromPosition(position, callback) {
const pixelOffset = this.calcOffsetFromPosition(position);
const closestIndex = this.getClosestIndex(pixelOffset);
const nextValue = trimAlignValue(this.calcValue(pixelOffset), this.props);
const closestIndex = this.getClosestIndex(pixelOffset, nextValue);

// Clone this.state.value since we'll modify it temporarily
// eslint-disable-next-line zillow/react/no-access-state-in-setstate
Expand Down

0 comments on commit 8a636aa

Please sign in to comment.