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

✨ feat: Added Minimum Step Constraint to Prevent Thumb Overlap on #453

Open
anasmassnaoui opened this issue Aug 7, 2024 · 0 comments

Comments

@anasmassnaoui
Copy link

anasmassnaoui commented Aug 7, 2024

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @miblanchard/[email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@miblanchard/react-native-slider/lib/index.js b/node_modules/@miblanchard/react-native-slider/lib/index.js
index 92df57a..2cf34f2 100644
--- a/node_modules/@miblanchard/react-native-slider/lib/index.js
+++ b/node_modules/@miblanchard/react-native-slider/lib/index.js
@@ -196,10 +196,25 @@ export class Slider extends PureComponent {
         }
         this.props?.onSlidingStart?.(this._getRawValues(this.state.values), this._activeThumbIndex);
     };
+    _hasViolatedStepsBetweenThumbs = (gestureState) => {
+        const values = this._getRawValues(this.state.values); 
+        if (values.length > 1) {
+            const stepsBetweenThumbs = this.props.stepsBetweenThumbs;
+            const thumbIndex = this._activeThumbIndex;
+            const secondThumbIndex = thumbIndex === 0 ? 1 : 0;
+            const secondValue = values[secondThumbIndex];
+            const dist = Math.abs(this._getValue(gestureState) - secondValue)
+            if (stepsBetweenThumbs && dist <= stepsBetweenThumbs) {
+                return true;
+            }
+        }
+        return false;
+    }
     _handlePanResponderMove = (_e, gestureState) => {
-        if (this.props.disabled) {
+        if (this.props.disabled || this._hasViolatedStepsBetweenThumbs(gestureState)) {
             return;
         }
+
         this._setCurrentValue(this._getValue(gestureState), this._activeThumbIndex, () => {
             this.props?.onValueChange?.(this._getRawValues(this.state.values), this._activeThumbIndex);
         });
@@ -209,7 +224,7 @@ export class Slider extends PureComponent {
         return false;
     };
     _handlePanResponderEnd = (_e, gestureState) => {
-        if (this.props.disabled) {
+        if (this.props.disabled || this._hasViolatedStepsBetweenThumbs(gestureState)) {
             return;
         }
         this._setCurrentValue(this._getValue(gestureState), this._activeThumbIndex, () => {
diff --git a/node_modules/@miblanchard/react-native-slider/lib/types.d.ts b/node_modules/@miblanchard/react-native-slider/lib/types.d.ts
index e989509..e60bfb4 100644
--- a/node_modules/@miblanchard/react-native-slider/lib/types.d.ts
+++ b/node_modules/@miblanchard/react-native-slider/lib/types.d.ts
@@ -34,6 +34,7 @@ export declare type SliderProps = {
     renderMinimumTrackComponent?: () => React.ReactNode;
     renderTrackMarkComponent?: (index: number) => React.ReactNode;
     step?: number;
+    stepsBetweenThumbs?: number;
     thumbImage?: ImageSourcePropType;
     thumbStyle?: ViewStyle;
     thumbTintColor?: string;

This issue body was partially generated by patch-package.

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

1 participant