2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
StyleSheet, View, Text, Platform, ActivityIndicator
|
|
|
|
} from 'react-native';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
import { COLOR_BUTTON_PRIMARY, COLOR_TEXT } from '../../constants/colors';
|
|
|
|
import Touch from '../../utils/touch';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { scale, moderateScale, verticalScale } from '../../utils/scaling';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
const colors = {
|
2018-08-10 17:26:36 +00:00
|
|
|
background_primary: COLOR_BUTTON_PRIMARY,
|
|
|
|
background_secondary: 'white',
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-08-10 17:26:36 +00:00
|
|
|
text_color_primary: 'white',
|
|
|
|
text_color_secondary: COLOR_TEXT
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
/* eslint-disable react-native/no-unused-styles */
|
2018-04-24 19:34:03 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2018-08-10 17:26:36 +00:00
|
|
|
paddingHorizontal: scale(15),
|
|
|
|
justifyContent: 'center',
|
|
|
|
height: scale(48)
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
text: {
|
2018-08-10 17:26:36 +00:00
|
|
|
fontSize: moderateScale(18),
|
|
|
|
height: verticalScale(20),
|
|
|
|
lineHeight: verticalScale(20),
|
2018-04-24 19:34:03 +00:00
|
|
|
textAlign: 'center',
|
2018-08-10 17:26:36 +00:00
|
|
|
fontWeight: '500'
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
background_primary: {
|
2018-08-10 17:26:36 +00:00
|
|
|
backgroundColor: colors.background_primary
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
background_secondary: {
|
2018-08-10 17:26:36 +00:00
|
|
|
backgroundColor: colors.background_secondary
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
text_color_primary: {
|
2018-08-10 17:26:36 +00:00
|
|
|
color: colors.text_color_primary
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
text_color_secondary: {
|
2018-08-10 17:26:36 +00:00
|
|
|
color: colors.text_color_secondary
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
margin: {
|
2018-08-10 17:26:36 +00:00
|
|
|
marginBottom: verticalScale(10)
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
opacity: 0.5
|
2018-07-17 19:10:27 +00:00
|
|
|
},
|
|
|
|
border: {
|
2018-08-10 17:26:36 +00:00
|
|
|
borderRadius: scale(2)
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class Button extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
title: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
onPress: PropTypes.func,
|
2018-07-17 19:10:27 +00:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
margin: PropTypes.any,
|
2018-08-10 17:26:36 +00:00
|
|
|
backgroundColor: PropTypes.string,
|
|
|
|
loading: PropTypes.bool
|
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 {
|
2018-08-10 17:26:36 +00:00
|
|
|
title, type, onPress, disabled, margin, backgroundColor, loading, ...otherProps
|
2018-04-24 19:34:03 +00:00
|
|
|
} = this.props;
|
|
|
|
return (
|
|
|
|
<Touch
|
|
|
|
onPress={onPress}
|
|
|
|
accessibilityTraits='button'
|
2018-07-17 19:10:27 +00:00
|
|
|
style={Platform.OS === 'ios' && [(margin || styles.margin), styles.border]}
|
2018-08-10 17:26:36 +00:00
|
|
|
disabled={disabled || loading}
|
2018-05-23 13:39:18 +00:00
|
|
|
{...otherProps}
|
2018-04-24 19:34:03 +00:00
|
|
|
>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.container,
|
2018-07-17 19:10:27 +00:00
|
|
|
styles.border,
|
|
|
|
backgroundColor ? { backgroundColor } : styles[`background_${ type }`],
|
|
|
|
Platform.OS === 'android' && (margin || styles.margin),
|
2018-04-24 19:34:03 +00:00
|
|
|
disabled && styles.disabled
|
|
|
|
]}
|
|
|
|
>
|
2018-08-10 17:26:36 +00:00
|
|
|
{
|
2018-09-25 19:28:42 +00:00
|
|
|
loading
|
|
|
|
? <ActivityIndicator color={colors[`text_color_${ type }`]} />
|
|
|
|
: <Text style={[styles.text, styles[`text_color_${ type }`]]}>{title}</Text>
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|