react-native-komect-uikit
Version:
React Native UI Toolkit
86 lines (74 loc) • 1.77 kB
JavaScript
import PropTypes from 'prop-types';
import React from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import colors from '../config/colors';
const Badge = props => {
const {
containerStyle,
textStyle,
wrapperStyle,
onPress,
component,
value,
children,
element,
...attributes
} = props;
if (element) return element;
let Component = View;
let childElement = (
<Text style={[styles.text, textStyle && textStyle]}>{value}</Text>
);
if (children) {
childElement = children;
}
if (children && value) {
console.error('Badge can only contain either child element or value');
}
if (!component && onPress) {
Component = TouchableOpacity;
}
if (React.isValidElement(component)) {
Component = component;
}
return (
<View style={[styles.container && wrapperStyle && wrapperStyle]}>
<Component
style={[styles.badge, containerStyle && containerStyle]}
onPress={onPress}
{...attributes}
>
{childElement}
</Component>
</View>
);
};
Badge.propTypes = {
containerStyle: View.propTypes.style,
wrapperStyle: View.propTypes.style,
textStyle: Text.propTypes.style,
children: PropTypes.element,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
onPress: PropTypes.func,
component: PropTypes.func,
element: PropTypes.element,
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
},
badge: {
padding: 12,
paddingTop: 3,
paddingBottom: 3,
backgroundColor: colors.grey1,
borderRadius: 20,
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 14,
color: 'white',
},
});
export default Badge;