Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions SlideButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PanResponder,
Animated
} from 'react-native';
import PropTypes from 'prop-types'

export var SlideDirection = {
LEFT: "left",
Expand All @@ -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;
}
}

Expand All @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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 :
Expand Down Expand Up @@ -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 = (
<Animated.View style={style}>
{this.props.children}
Expand All @@ -197,26 +226,23 @@ export class SlideButton extends Component {
var button = (
<View style={style}>
<View onLayout={this.onLayout.bind(this)}>
{this.props.children}
{this.props.children}
</View>
</View>
);
}

return (
<View style={{width: this.props.width, height: this.props.height, overflow: 'hidden'}}>
<View style={styles.container} {...this.panResponder.panHandlers}>
<View ref="button" style={styles.container}
{...this.panResponder.panHandlers}>
{ button }
</View>
</View>
);
}
}

SlideButton.propTypes = {
width: React.PropTypes.number.isRequired,
height: React.PropTypes.number.isRequired,
successfulSlidePercent: React.PropTypes.number
successfulSlidePercent: PropTypes.number
};

const styles = StyleSheet.create({
Expand Down