From 8a636aaccca4cafc17b28dd6d31bc09df07dc9df Mon Sep 17 00:00:00 2001 From: AOB Date: Tue, 26 Sep 2023 08:31:38 +0200 Subject: [PATCH] fix: use lower or upper bound when changing from same value fixes #289 --- src/components/ReactSlider/ReactSlider.jsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/ReactSlider/ReactSlider.jsx b/src/components/ReactSlider/ReactSlider.jsx index e4c49ea..5d2defd 100644 --- a/src/components/ReactSlider/ReactSlider.jsx +++ b/src/components/ReactSlider/ReactSlider.jsx @@ -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); @@ -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