2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { StyleSheet, Text } from 'react-native';
|
2020-02-11 14:01:35 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2018-11-14 21:42:03 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2020-03-30 19:20:50 +00:00
|
|
|
paddingHorizontal: 14,
|
2018-08-10 17:26:36 +00:00
|
|
|
justifyContent: 'center',
|
2018-11-14 21:42:03 +00:00
|
|
|
height: 48,
|
|
|
|
borderRadius: 2,
|
2020-03-30 19:20:50 +00:00
|
|
|
marginBottom: 12
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
text: {
|
2020-03-30 19:20:50 +00:00
|
|
|
fontSize: 16,
|
|
|
|
textAlign: 'center',
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
opacity: 0.3
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class Button extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2018-11-14 21:42:03 +00:00
|
|
|
title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
|
2018-04-24 19:34:03 +00:00
|
|
|
type: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
2018-07-17 19:10:27 +00:00
|
|
|
disabled: PropTypes.bool,
|
2018-08-10 17:26:36 +00:00
|
|
|
backgroundColor: PropTypes.string,
|
2018-11-19 18:18:15 +00:00
|
|
|
loading: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2020-03-30 19:20:50 +00:00
|
|
|
color: PropTypes.string,
|
|
|
|
fontSize: PropTypes.string,
|
2018-11-19 18:18:15 +00:00
|
|
|
style: PropTypes.any
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
title: 'Press me!',
|
|
|
|
type: 'primary',
|
|
|
|
onPress: () => alert('It works!'),
|
2018-08-10 17:26:36 +00:00
|
|
|
disabled: false,
|
|
|
|
loading: false
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2020-03-30 19:20:50 +00:00
|
|
|
title, type, onPress, disabled, backgroundColor, color, loading, style, theme, fontSize, ...otherProps
|
2018-04-24 19:34:03 +00:00
|
|
|
} = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
const isPrimary = type === 'primary';
|
2020-03-30 19:20:50 +00:00
|
|
|
|
|
|
|
let textColor = isPrimary ? themes[theme].buttonText : themes[theme].bodyText;
|
|
|
|
if (color) {
|
|
|
|
textColor = color;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2020-02-11 14:01:35 +00:00
|
|
|
<Touchable
|
2018-04-24 19:34:03 +00:00
|
|
|
onPress={onPress}
|
2020-02-11 14:01:35 +00:00
|
|
|
disabled={disabled || loading}
|
2018-11-14 21:42:03 +00:00
|
|
|
style={[
|
|
|
|
styles.container,
|
2019-12-04 16:39:53 +00:00
|
|
|
backgroundColor
|
|
|
|
? { backgroundColor }
|
|
|
|
: { backgroundColor: isPrimary ? themes[theme].actionTintColor : themes[theme].backgroundColor },
|
2020-03-30 19:20:50 +00:00
|
|
|
disabled && styles.disabled,
|
2018-11-19 18:18:15 +00:00
|
|
|
style
|
2018-11-14 21:42:03 +00:00
|
|
|
]}
|
2018-05-23 13:39:18 +00:00
|
|
|
{...otherProps}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
2018-11-14 21:42:03 +00:00
|
|
|
{
|
|
|
|
loading
|
2020-03-30 19:20:50 +00:00
|
|
|
? <ActivityIndicator color={textColor} />
|
2019-12-04 16:39:53 +00:00
|
|
|
: (
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.text,
|
2020-03-30 19:20:50 +00:00
|
|
|
{ color: textColor },
|
|
|
|
fontSize && { fontSize }
|
2019-12-04 16:39:53 +00:00
|
|
|
]}
|
2020-10-30 18:31:04 +00:00
|
|
|
accessibilityLabel={title}
|
2019-12-04 16:39:53 +00:00
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
)
|
2018-11-14 21:42:03 +00:00
|
|
|
}
|
2020-02-11 14:01:35 +00:00
|
|
|
</Touchable>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|