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
52 changes: 30 additions & 22 deletions SlideButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {
Image,
TouchableHighlight,
PanResponder,
Dimensions,
Animated
} from 'react-native';

var SCREEN_WIDTH = Dimensions.get('window').width;
var SCREEN_HEIGHT = Dimensions.get('window').height;

export var SlideDirection = {
LEFT: "left",
RIGHT: "right",
Expand All @@ -31,18 +35,16 @@ export class SlideButton extends Component {
};
}

/* Button movement of > 40% is considered a successful slide by default*/
/* Button movement of > 40% is considered a successful slide */
isSlideSuccessful() {
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 > (this.buttonWidth * 0.4); // Defaults to right slide
} else if (this.props.slideDirection === SlideDirection.RIGHT) {
return this.state.dx > this.props.successfulSlideWidth;
return this.state.dx > (this.buttonWidth * 0.4);
} else if (this.props.slideDirection === SlideDirection.LEFT) {
return this.state.dx < (-1 * this.props.successfulSlideWidth);
return this.state.dx < -(this.buttonWidth * 0.4);
} else if (this.props.slideDirection === SlideDirection.BOTH) {
return Math.abs(this.state.dx) > this.props.successfulSlideWidth;
return Math.abs(this.state.dx) > (this.buttonWidth * 0.4);
}
}

Expand All @@ -52,7 +54,7 @@ export class SlideButton extends Component {
}
}

componentWillMount() {
UNSAFE_componentWillMount() {
var self = this;

// TODO: Raise error if slideDirection prop is invalid.
Expand Down Expand Up @@ -127,6 +129,16 @@ export class SlideButton extends Component {
}
}

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 All @@ -139,7 +151,8 @@ export class SlideButton extends Component {
}, () => {
Animated.timing(
self.state.animatedX,
{ toValue: endPos }
{ toValue: endPos,
useNativeDriver: false }
).start(onCompleteCallback);
});
}
Expand All @@ -155,7 +168,8 @@ export class SlideButton extends Component {
}, () => {
Animated.timing(
self.state.animatedX,
{ toValue: endPos }
{ toValue: endPos,
useNativeDriver: false }
).start(onCompleteCallback);
});
}
Expand All @@ -171,7 +185,8 @@ export class SlideButton extends Component {
}, () => {
Animated.timing(
self.state.animatedX,
{ toValue: endPos }
{ toValue: endPos,
useNativeDriver: false }
).start(onCompleteCallback);
});
}
Expand All @@ -187,7 +202,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,28 +212,21 @@ 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
};

const styles = StyleSheet.create({
container: {
position: 'relative'
Expand Down