-
Notifications
You must be signed in to change notification settings - Fork 31
/
Badge.js
100 lines (93 loc) · 2.31 KB
/
Badge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React from 'react';
import {
View,
TouchableOpacity,
TouchableNativeFeedback,
Platform,
Text,
StyleSheet,
} from 'react-native';
import PropTypes from 'prop-types';
import {useThemeContext} from '../util/ThemeProvider';
import {radii, sizes} from '../util/prop-types';
const getContainerStyle = ({theme, size, mini, color, square, radius}) => {
const badgeStyle = [styles.container];
if (color) {
badgeStyle.push({
backgroundColor: theme.colors[color],
borderRadius: theme.radius.full,
});
}
if (square) {
badgeStyle.push({
borderRadius: theme.radius.none,
});
}
if (radius) {
badgeStyle.push({
borderRadius: theme.radius[radius],
});
}
if (mini) {
badgeStyle.push({
width: theme.miniBadgeSize[size],
height: theme.miniBadgeSize[size],
borderRadius: theme.radius.full,
});
}
return badgeStyle;
};
const getTextStyle = ({theme, size}) => {
return {
color: '#fff',
fontSize: theme.badgeSize[size],
marginVertical: 5,
marginHorizontal: 10,
};
};
const Badge = ({children, onPress, style, textStyle, ...props}) => {
const theme = useThemeContext();
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
return (
<TouchableElement {...props} onPress={onPress} disabled={!onPress}>
<View
style={StyleSheet.flatten(
StyleSheet.flatten([getContainerStyle({...props, theme}), style]),
)}>
{props.mini ? null : (
<Text
style={StyleSheet.flatten([
getTextStyle({...props, theme}),
textStyle,
])}>
{children}
</Text>
)}
</View>
</TouchableElement>
);
};
Badge.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
children: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
size: sizes,
mini: PropTypes.bool,
onPress: PropTypes.func,
square: PropTypes.bool,
radius: radii,
};
Badge.defaultProps = {
children: 0,
color: 'primary',
size: 'sm',
};
const styles = StyleSheet.create({
container: {
alignSelf: 'flex-start',
justifyContent: 'center',
alignItems: 'center',
},
});
export default Badge;