forked from iamshadmirza/react-native-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckBox.js
89 lines (82 loc) · 2.4 KB
/
CheckBox.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
import React from 'react';
import { View, TouchableOpacity, TouchableNativeFeedback, Platform, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import { useThemeContext } from '../util/ThemeProvider';
const getTextStyle = ({ theme, size, textColor, iconRight }) => {
const textStyle = [{
fontSize: theme.fontSize[size],
color: theme.textColor[textColor],
marginLeft: 5,
}];
if (iconRight) {
textStyle.push({
marginLeft: 0,
marginRight: 5,
});
}
return textStyle;
};
const renderIcon = ({ style, theme, size, color, ...props }) => {
if (props.checked) {
return (
props.checkedIcon ||
<MaterialIcons
name="check-box"
size={theme.fontSize[size] * 1.5}
color={theme.brandColor[color]}
/>
);
} else {
return (
props.uncheckedIcon ||
<MaterialIcons
name="check-box-outline-blank"
size={theme.fontSize[size] * 1.5}
color={theme.brandColor[color]}
/>
);
}
};
const CheckBox = ({ style, textStyle, ...props }) => {
const theme = useThemeContext();
const propsWithTheme = { ...props, theme };
const TouchableElement =
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
return (
<TouchableElement {...props} disabled={props.disabled} onPress={props.onPress}>
<View style={StyleSheet.flatten([styles.container, style])}>
{!props.iconRight && renderIcon(propsWithTheme)}
<Text style={StyleSheet.flatten([getTextStyle(propsWithTheme), textStyle])}>
{props.children}
</Text>
{props.iconRight && renderIcon(propsWithTheme)}
</View>
</TouchableElement>
);
};
CheckBox.propTypes = {
style: PropTypes.object,
textStyle: PropTypes.object,
children: PropTypes.string.isRequired,
checked: PropTypes.bool,
iconRight: PropTypes.bool,
color: PropTypes.string,
textColor: PropTypes.string,
size: PropTypes.oneOf(['xxsmall', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge']),
onPress: PropTypes.func.isRequired,
checkedIcon: PropTypes.element,
uncheckedIcon: PropTypes.element,
};
CheckBox.defaultProps = {
size: 'medium',
color: 'primary',
textColor: 'default',
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
},
});
export default CheckBox;