verdnatura-chat/app/containers/ListItem.js

103 lines
2.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
2019-12-04 16:39:53 +00:00
import Touch from '../utils/touch';
import { themes } from '../constants/colors';
import sharedStyles from '../views/Styles';
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
2019-12-04 16:39:53 +00:00
height: 46,
paddingHorizontal: 15
},
disabled: {
opacity: 0.3
},
textContainer: {
flex: 1,
justifyContent: 'center'
},
title: {
fontSize: 16,
...sharedStyles.textRegular
},
subtitle: {
fontSize: 14,
...sharedStyles.textRegular
}
});
const Content = React.memo(({
title, subtitle, disabled, testID, left, right, color, theme
}) => (
<View style={[styles.container, disabled && styles.disabled]} testID={testID}>
{left ? left() : null}
<View style={styles.textContainer}>
2019-12-04 16:39:53 +00:00
<Text style={[styles.title, { color: color || themes[theme].titleText }]}>{title}</Text>
{subtitle
2019-12-04 16:39:53 +00:00
? <Text style={[styles.subtitle, { color: themes[theme].bodyText }]}>{subtitle}</Text>
: null
}
</View>
{right ? right() : null}
</View>
));
const Button = React.memo(({
onPress, ...props
}) => (
2019-12-04 16:39:53 +00:00
<Touch
onPress={() => onPress(props.title)}
2019-12-04 16:39:53 +00:00
style={{ backgroundColor: themes[props.theme].backgroundColor }}
enabled={!props.disabled}
2019-12-04 16:39:53 +00:00
theme={props.theme}
>
<Content {...props} />
2019-12-04 16:39:53 +00:00
</Touch>
));
const Item = React.memo(({ ...props }) => {
if (props.onPress) {
return <Button {...props} />;
}
2019-12-04 16:39:53 +00:00
return (
<View style={{ backgroundColor: themes[props.theme].backgroundColor }}>
<Content {...props} />
</View>
);
});
Item.propTypes = {
2019-12-04 16:39:53 +00:00
onPress: PropTypes.func,
theme: PropTypes.string
};
Content.propTypes = {
title: PropTypes.string.isRequired,
subtitle: PropTypes.string,
left: PropTypes.func,
right: PropTypes.func,
disabled: PropTypes.bool,
2019-12-04 16:39:53 +00:00
testID: PropTypes.string,
theme: PropTypes.string,
color: PropTypes.string
};
Button.propTypes = {
title: PropTypes.string,
onPress: PropTypes.func,
2019-12-04 16:39:53 +00:00
disabled: PropTypes.bool,
theme: PropTypes.string
};
Button.defaultProps = {
disabled: false
};
export default Item;