forked from iamshadmirza/react-native-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBadge.js
99 lines (92 loc) · 2.45 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
import React from 'react';
import { View, TouchableOpacity, TouchableNativeFeedback, Platform, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import { useThemeContext } from '../util/ThemeProvider';
const getContainerStyle = ({ theme, size, mini, color, square }) => {
const badgeStyle = [styles.container];
if (color) {
badgeStyle.push({
backgroundColor: theme.brandColor[color],
});
}
if (square) {
badgeStyle.push({
borderRadius: 3,
});
}
if (mini) {
badgeStyle.push({
width: theme.miniBadgeSize[size],
height: theme.miniBadgeSize[size],
});
}
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.object,
textStyle: PropTypes.object,
children: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
size: PropTypes.oneOf(['xxsmall', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge']),
mini: PropTypes.bool,
onPress: PropTypes.func,
square: PropTypes.bool,
};
Badge.defaultProps = {
children: 0,
color: 'primary',
size: 'small',
};
const styles = StyleSheet.create({
container: {
alignSelf: 'flex-start',
borderRadius: 30,
justifyContent: 'center',
alignItems: 'center',
...Platform.select({
android: {
elevation: 1,
},
ios: {
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3,
},
web: {
// boxShadow: `${offsetWidth}px ${offsetHeight}px ${radius}px ${rgba}`
boxShadow: '0 3px 5px rgba(0,0,0,0.10), 1px 2px 5px rgba(0,0,0,0.10)',
},
}),
},
});
export default Badge;