From 9f4421a7b166417b6080bf386ab6db5d98e425e4 Mon Sep 17 00:00:00 2001 From: Shanaka Fernando Date: Fri, 29 Sep 2017 21:30:35 +0530 Subject: [PATCH 1/2] * New Features -Direction of the button slided by user. Useful when using `SlideDirection.BOTH` *Bug Fixes - Fixed the issue in not using the this.props.successfulSlidePercent for calculating the slide movement --- README.md | 8 ++++++++ SlideButton.js | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bcef045..e945440 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,17 @@ Usage ``` import { SlideButton, SlideDirection } from 'react-native-slide-button'; +getEndDirection(direction){ + this.setState({ + endDirection:direction + }) +} + @@ -42,6 +49,7 @@ API | onSlideSuccess | function | | Fired when slide succeeds | onSlide | function | | Fired on every movement. Distance of movement is passed as argument. | successfulSlidePercent | number | | Percent of total button width needed to slide before movement is seen as a successful slide. Default is 40. +| endDirection | function | | Direction of the button slided by user. Useful when using `SlideDirection.BOTH`. TODO diff --git a/SlideButton.js b/SlideButton.js index e0b3fdb..1179abb 100644 --- a/SlideButton.js +++ b/SlideButton.js @@ -27,7 +27,7 @@ export class SlideButton extends Component { dx: 0, animatedX: new Animated.Value(0), released: false, - swiped: true, + swiped: true }; } @@ -36,13 +36,13 @@ export class SlideButton extends Component { var slidePercent = this.props.successfulSlidePercent || 40; var successfulSlideWidth = this.buttonWidth * slidePercent / 100; if (!this.props.slideDirection) { - return this.state.dx > this.props.successfulSlideWidth; // Defaults to right slide + return this.state.dx > successfulSlideWidth; // Defaults to right slide } else if (this.props.slideDirection === SlideDirection.RIGHT) { - return this.state.dx > this.props.successfulSlideWidth; + return this.state.dx > successfulSlideWidth; } else if (this.props.slideDirection === SlideDirection.LEFT) { - return this.state.dx < (-1 * this.props.successfulSlideWidth); + return this.state.dx < (-1 * successfulSlideWidth); } else if (this.props.slideDirection === SlideDirection.BOTH) { - return Math.abs(this.state.dx) > this.props.successfulSlideWidth; + return Math.abs(this.state.dx) > successfulSlideWidth; } } @@ -78,6 +78,14 @@ export class SlideButton extends Component { // Move the button out this.moveButtonOut(() => { self.setState({ swiped: true }); + //Set swipe ditrection details : usable for SlideDirection.BOTH + if(self.props.endDirection !== undefined){ + if(self.state.dx > 0){ + self.props.endDirection(SlideDirection.RIGHT) + }else{ + self.props.endDirection(SlideDirection.LEFT) + } + } self.props.onSlideSuccess(); }); From be64771efb687641317f3efc47e3c51366400a7e Mon Sep 17 00:00:00 2001 From: Shanaka Fernando Date: Sat, 30 Sep 2017 14:28:12 +0530 Subject: [PATCH 2/2] *Feature - slideBackToOriginal is added to check whether to slide back the button back to it's original position after 1 second --- README.md | 3 ++- SlideButton.js | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e945440..a87989b 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ getEndDirection(direction){ onSlideSuccess={this.onSlide.bind(this)} slideDirection={SlideDirection.LEFT} endDirection ={this.getEndDirection.bind(this)} + slideBackToOriginal={false} width={500} height={50}> @@ -50,7 +51,7 @@ API | onSlide | function | | Fired on every movement. Distance of movement is passed as argument. | successfulSlidePercent | number | | Percent of total button width needed to slide before movement is seen as a successful slide. Default is 40. | endDirection | function | | Direction of the button slided by user. Useful when using `SlideDirection.BOTH`. - +| slideBackToOriginal | boolean | | Default to true. Whether to Slide back the button back to it's original position after 1 second TODO --- diff --git a/SlideButton.js b/SlideButton.js index 1179abb..fdea65b 100644 --- a/SlideButton.js +++ b/SlideButton.js @@ -89,15 +89,18 @@ export class SlideButton extends Component { self.props.onSlideSuccess(); }); - // Slide it back in after 1 sec - setTimeout(() => { - self.moveButtonIn(() => { - self.setState({ - released: false, - dx: self.state.initialX + // Slide it back in after 1 sec depending on the user preference + var isSlideBack = self.props.slideBackToOriginal === undefined ? true : self.props.slideBackToOriginal + if(isSlideBack){ + setTimeout(() => { + self.moveButtonIn(() => { + self.setState({ + released: false, + dx: self.state.initialX + }); }); - }); - }, 1000); + }, 1000); + } } else { this.snapToPosition(() => {