diff --git a/SlideButton.js b/SlideButton.js index e0b3fdb..72ac815 100644 --- a/SlideButton.js +++ b/SlideButton.js @@ -10,6 +10,7 @@ import { PanResponder, Animated } from 'react-native'; +import PropTypes from 'prop-types' export var SlideDirection = { LEFT: "left", @@ -36,13 +37,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; } } @@ -52,7 +53,12 @@ export class SlideButton extends Component { } } + componentWillUnmount() { + this.mounted = false + } + componentWillMount() { + this.mounted = true var self = this; // TODO: Raise error if slideDirection prop is invalid. @@ -78,17 +84,19 @@ export class SlideButton extends Component { // Move the button out this.moveButtonOut(() => { self.setState({ swiped: true }); - self.props.onSlideSuccess(); + self.onSlideSuccess(); }); // Slide it back in after 1 sec setTimeout(() => { - self.moveButtonIn(() => { - self.setState({ - released: false, - dx: self.state.initialX + if (self.mounted) { + self.moveButtonIn(() => { + self.setState({ + released: false, + dx: self.state.initialX + }); }); - }); + } }, 1000); } else { @@ -123,10 +131,31 @@ export class SlideButton extends Component { onSlideSuccess() { if (this.props.onSlideSuccess !== undefined) { - this.props.onSlideSuccess(); + + var direction = null + + if (this.props.slideDirection === SlideDirection.BOTH) { + if (this.state.dx > (this.buttonWidth * 0.4)) { + direction = SlideDirection.RIGHT + } else { + direction = SlideDirection.LEFT + } + } + + this.props.onSlideSuccess(direction); } } + measureButton() { + var self = this; + this.refs.button.measure((ox, oy, width, height) => { + self.setState({ + initialX: ox, + buttonWidth: width + }); + }); + } + moveButtonIn(onCompleteCallback) { var self = this; var startPos = this.state.dx < 0 ? this.state.initialX + this.buttonWidth : @@ -187,7 +216,7 @@ export class SlideButton extends Component { var style = [styles.button, this.props.style, {left: this.state.dx}]; if (this.state.released) { - style = [styles.button, this.props.style, {left: this.state.animatedX}]; + style = [styles.button, this.props.style, { left: this.state.animatedX }]; var button = ( {this.props.children} @@ -197,26 +226,23 @@ export class SlideButton extends Component { var button = ( - {this.props.children} + {this.props.children} ); } return ( - - + { button } - ); } } SlideButton.propTypes = { - width: React.PropTypes.number.isRequired, - height: React.PropTypes.number.isRequired, - successfulSlidePercent: React.PropTypes.number + successfulSlidePercent: PropTypes.number }; const styles = StyleSheet.create({