Skip to content

Instantly share code, notes, and snippets.

@Maluen
Last active August 28, 2018 10:48
Show Gist options
  • Save Maluen/a11eb3b0bc3bb35d88fe0d526f43737b to your computer and use it in GitHub Desktop.
Save Maluen/a11eb3b0bc3bb35d88fe0d526f43737b to your computer and use it in GitHub Desktop.
import React from 'react';
import PropTypes from 'prop-types';
import { Text, View, ViewPropTypes } from 'react-native';
import { GestureHandler } from 'expo';
import styles from './styles';
const { LongPressGestureHandler, State, RectButton } = GestureHandler;
class Button extends React.Component {
static propTypes = {
title: PropTypes.string,
dark: PropTypes.bool,
longPressMinDuration: PropTypes.number,
containerStyle: ViewPropTypes.style,
buttonStyle: ViewPropTypes.style,
textStyle: ViewPropTypes.style,
onPress: PropTypes.func,
onLongPressStarted: PropTypes.func,
onLongPressCancelled: PropTypes.func,
onLongPressEnd: PropTypes.func,
onLongPress: PropTypes.func, // alias for onLongPressEnd
};
static defaultProps = {
title: '',
dark: false,
longPressMinDuration: 500,
containerStyle: undefined,
buttonStyle: undefined,
textStyle: undefined,
onPress: () => {},
onLongPressStarted: () => {},
onLongPressCancelled: () => {},
onLongPressEnd: () => {},
onLongPress: () => {},
};
onPress = () => {
this.props.onPress(this.props.title);
}
onLongPressStateChange = (event) => {
const { title } = this.props;
switch (event.nativeEvent.state) {
case State.ACTIVE:
this.props.onLongPressStarted(title);
break;
case State.CANCELLED:
this.props.onLongPressCancelled(title);
break;
case State.END:
this.props.onLongPressEnd(title);
this.props.onLongPress(title);
break;
default:
break;
}
}
render() {
const {
title, dark, longPressMinDuration,
containerStyle, buttonStyle, textStyle,
} = this.props;
return (
<View style={[styles.buttonContainer, containerStyle]}>
<LongPressGestureHandler
onHandlerStateChange={this.onLongPressStateChange}
minDurationMs={longPressMinDuration}
>
<RectButton
style={[styles.button, dark ? styles.buttonDark : undefined, buttonStyle]}
onPress={this.onPress}
>
<Text style={[styles.buttonText, textStyle]}>{title}</Text>
</RectButton>
</LongPressGestureHandler>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment